How Cloud Batch Applications Connect to Cloud Database
cfang Oct 26, 2017 11:18 PMIt's a very common setup for an application deployment to require a database server, and cloud batch application is no exception. In the sample JBeret batch application deployed in OpenShift Online (see previous post), the OpenShift project contains 2 PODS: 1 for the batch application itself including WildFly, and the other is PostgreSQL database.
So the question is, how my application code can access the database server, both are running as 2 PODs in the same OpenShift project? Shouldn't it be the same old jdbc approach, with connection url, username, password, database name, and additional connection properties?
Yes, and actually OpenShift makes it a bit easier than that. This is the connection URL to be used by your application code to connect PostgreSQL server in the same OpenShift project:
jdbc:postgresql://postgresql/sampledb
In a more general form:
jdbc:postgresql://<database-service-name>/<database-name>
As your can see, we don't need to find out the real ip address of the database POD. The service name for the database, ie., postgresql, will be added as a hostname to the internal DNS. As a result any other application is the same project can just use postgresql as a hostname to talk to the database. The port number, which is the default port number 5432, is omitted here.
If you really want to find out the ip address of the PostgreSQL POD, it's also easy to do. Go to OpenShift web console, then go to PostgreSQL POD, then click Terminal to open its web-based POD terminal, run command
sh-4.2$ env | grep POST POSTGRESQL_PORT_5432_TCP_ADDR=172.30.245.228 POSTGRESQL_PORT=tcp://172.30.245.228:5432 POSTGRESQL_SERVICE_PORT_POSTGRESQL=5432 POSTGRESQL_PORT_5432_TCP=tcp://172.30.245.228:5432 POSTGRESQL_SERVICE_HOST=172.30.245.228 POSTGRESQL_DATABASE=sampledb POSTGRESQL_PASSWORD=jberet POSTGRESQL_PORT_5432_TCP_PORT=5432 POSTGRESQL_VERSION=9.5 POSTGRESQL_SERVICE_PORT=5432 POSTGRESQL_USER=jberet POSTGRESQL_PORT_5432_TCP_PROTO=tcp sh-4.2$