0

I'm setting up a MySQL service on an Openshift container, but I can't access it from the rest of the services.

According to the documentation (https://docs.openshift.com/online/dev_guide/environment_variables.html#automatically-added-environment-variables), when a service is created, the X_SERVICE_HOST and X__SERVICE_PORT environment vbles are populated to the rest of the services, but I can't seem to be able to access them from my Java service (using the ${MYSQL_SERVICE_HOST} placeholder on a .properties file).

Previously I had been able to connect to a MySQL db using the Cluster IP value on the service definition, but it doesn't work either, I have tried to import the mysql secret on my Java service, which is supposed to import the env variables such as username, pwd, etc. but it doesn't import the hostname and port.

As stated in this thread (https://github.com/openshift/origin/issues/10401) I have tried to create a MySQL service named MYSQL, but it fails due to the upper casing.

How should I propagate the env vbles to other services?

Thank you.

Edit: The issue was finally solved, the MySQL URL connection format was wrong, and it contained an extra / somewhere, after I fixed that the vbles propagated correctly.

GCarbajosa
  • 236
  • 1
  • 4
  • 13

3 Answers3

1

Ordering may be the issue here. I believe the service will need to be created before any of the deployment configs, replication controllers etc that consume them as env vars.

PhilipGough
  • 1,709
  • 1
  • 13
  • 18
  • The database container was the first thing I created, before the Java one, if that's what you mean. – GCarbajosa May 12 '18 at 18:36
  • 1
    I don't mean the container I mean the actual mysql service api object. At what point was that created? Have a read of https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/#environment-variables for more explanation. If this was the problem then scaling the pods down and back up should resolve – PhilipGough May 12 '18 at 18:39
  • I just tried recreating the entire project from scratch, I also tried scaling everything down and starting the mysql pod first, but it didn't work... The name of the variable should be `${MYSQL_HOST_NAME}` right? Another question: why did it use to be possible to just use the `Cluster IP` value to connect to the service and it doesn't work now? Is there another way to get the ip? even if it means hardcoding it in the `.properties` file? – GCarbajosa May 12 '18 at 19:30
  • 1
    When you run "oc get svc" it will output the service name. Do this first to ensure the service that you expect to exist does. In the running containers the reference to this svc should be capitalised service name _SERVICE_HOST. You should rsh into the java terminal and run the command "env" to check that this has been correctly populated – PhilipGough May 12 '18 at 19:43
  • 1
    "Is there another way to get the ip" - yes you should be able to use the internal dns. So if your svc name is mysql and service port is 3306 for example as default then it should be reachable at mysql:3306.svc.cluster.local – PhilipGough May 12 '18 at 19:54
  • Surprisingly, it actually has, I can see a `MYSQL_SERVICE_HOST` and a `MYSQL_SERVICE_PORT` vbles. Which makes it very confusing for 2 reasons: My Java app doesn't pick up the vble correctly and/or even if I hardcode the DB's IP, it still can't connect – GCarbajosa May 12 '18 at 20:35
1

These environment variables will only exist on the pod where the service manifest is selecting. For example if the service manifest selector matches the pod labels, then only those pods will have the environment variables. Other pods will not see those environment variables.

From your java application simply refer to the mysql service name and the port it exposes, and you should be good to go. But make sure that your java client and mysql are on the same namespace / project.

Bal Chua
  • 1,134
  • 9
  • 10
0

The issue was finally solved, the MySQL URL connection format was wrong, and it contained an extra / somewhere, after I fixed that the vbles propagated correctly.

GCarbajosa
  • 236
  • 1
  • 4
  • 13