How do i provide a mysql instance hosted outside the namespace to a pod that is in the namespace?

7/15/2019

I need to make available a mysql database hosted in namespace A to an application that is deployed in namespace 'B'.

Thus far, I've tried a few methods, most promising of which was using a combination of Endpoint and service like so:

kind: Service
apiVersion: v1
metadata:
  name: mysql
spec:
  ports:
    - port: 3306
      targetPort: 31234
---
kind: Endpoints
apiVersion: v1
metadata:
  name: mysql
subsets:
  - addresses:
      - ip: 12.34.567.8
    ports:
      - port: 31234

While one instance of a mysql container has already been spinned off in namespace 'A', and exposed at 31234 via NodePort configuration.

The Application has an init container, init-mysql that pings the mysql instance, with host name 'mysql' and correct credentials. I expected the Application to start up as usual, but it was stuck in pod initializing state. When I tried to check the logs of the init-mysql, I got only the following:

Warning: Using a password on the command line interface can be insecure.

The commands that are in use for the initContainer 'init-mysql' are:

          command:
            - sh
            - -c
            - 'mysqladmin ping -hmysql -P3306 -uusername -ppassword'
-- hisdudeness
kubernetes
kubernetes-helm

1 Answer

7/16/2019

This question was asked here

I am posting the accepted answer from Paul (community wiki) for better visibility:

I stumbled over the same issue and found a nice solution which does not need any static ip configuration:

You can access a service via it's DNS name (as mentioned by you): servicename.namespace.svc.cluster.local

You can use that DNS name to reference it in another namespace via a local service:

kind: Service
apiVersion: v1
metadata:
  name: service-y
  namespace: namespace-a
spec:
  type: ExternalName
  externalName: service-x.namespace-b.svc.cluster.local
  ports:
  - port: 80
-- OhHiMark
Source: StackOverflow