how to access database service remotely in openshift origin

5.1k views Asked by At

I have an Openshift Origin 3.* cluster up an running. It works great, and I have a few applications running there, composed of their own app server, database server, and ldap server Openshift services/deployments/pods.

These web applications are exposed via the HA2-router. All of this works nicely, and we can access the webapps using the Openshift Routes we set up via the HA2-router. However, we'd like to to get access to the Postgresql database services running in Openshift with tools like 'psql' (a command line Postgresql client), or PgAdmin (a GUI that connects to Postgresql).

I'm stumped as to how to accomplish this. Services or pods within Openshift have no trouble connecting to the Postgresql pods -- because they Openshift performs its own DNS and routing between those pods. However, the only way I'm familiar with that communication can happen from the outside is via a router like HA2-router. This doesn't work, because the HA2-router exposes services in Postgresql via HTTP... We need to connect to a Postgresql pod via TCP, on port 5432.

Thanks.

2

There are 2 answers

1
SarthAk On

You can use

port-forward

In order to take full advantage of the “rhc port-forward” tool, make sure that you have the latest version of the client tools. On systems with the client tools installed via ruby gem, enter the following command:

$ sudo yum update rhc

Step 2

Now that you have the latest client tools installed, you should have access to the “rhc port-forward” command. Once the command is invoked, the client tools will scan your deployed application and look for any embedded services available to forward. The client tools will then configure your local system to be able to connect to the available services for your remote application.

$ rhc port-forward -a exampleapp


Forwarding ports ...

To connect to a service running on OpenShift, use the Local address

Service    Local               OpenShift
---------- -------------- ---- ----------------
httpd      127.0.0.1:8080  =>  19.66.2.6:8080
postgresql 127.0.0.1:5432  =>  19.66.2.7:5432

Press CTRL-C to terminate port forwarding

Step 3

You can see the command has now mapped port 5432 in the application to port 5432 on the local machine. You can now connect to the local loopback address (127.0.0.1) on port 5433 to connect to PostgreSQL running on the gear.

$ psql -h 127.0.0.1 -p 5433 -U adminm4rvN42 exampleapp
Password for user adminm4rvN42:
psql (9.3.2, server 9.2.4)
Type "help" for help.

exampleapp=# <emphasis role="strong">\dt</emphasis>
                List of relations
 Schema |      Name       | Type  |    Owner
--------+-----------------+-------+--------------
 public | long_adjective  | table | adminm4rvN42
 public | noun            | table | adminm4rvN42
 public | short_adjective | table | adminm4rvN42
(3 rows)

exampleapp=#

or can connect to pgadmin tool too.

You can visit the link for further clarifications

https://blog.openshift.com/getting-started-with-port-forwarding-on-openshift/ https://developers.openshift.com/managing-your-applications/port-forwarding.html https://docs.openshift.com/enterprise/3.0/dev_guide/port_forwarding.html

0
Lev On

Found it. Openshift v3.* has a similar command to the "rhc" command SathAk posted above (thanks!) for v2.*.

after logging into your Openshift project with the "oc" command, you can run:

oc get pods | grep Running

which will show you a list of your Running pods. Find the Postgresql (or any other pod you're interested in connecting directly to via its exposed port), and run this:

oc port-forward [name of postgresql pod] 5432:5432

That will forward all traffic from the pod's 5432 port to your local 5432 port, and you can connect to the Postgresql instance in the pod with your GUI PgAdmin, Valentina, etc. tools, or with command line psql like so:

 psql -h localhost -U user -d database -p 5432 -W