IP Address assignment for the application running inside the pod in kubernetes

7/22/2018

I run my application in one pod and the Mongo database in other pod. For my application successful startup, it needs to know the IP address where the Mongo is running.

I have questions below:

  1. How do I get to know the the Mongo pod IP address so that I can configure this in my application.
  2. My application will run on some IP & port, and this is provided as part of some configuration file. But as these are containerized and Kubernetes assigns the Pod IP address, how can my application pick this IP address as it own IP?
-- user3628417
kubernetes

1 Answer

7/22/2018

You need to expose mongodb using Kubernetes Services. With the help of Services there is no need for an application to know the actual IP address of the Pod, you can use the service name to resolve mongodb.

Reference: https://kubernetes.io/docs/concepts/services-networking/service/

An example using mysql:

apiVersion: v1
kind: Service
metadata:
  labels:
    name: mysql
  name: mysql
spec:
  ports:
    - port: 3306
  selector:
    name: mysql
---
apiVersion: v1
kind: Pod
metadata:
  name: mysql
  labels:
    name: mysql
spec:
  containers:
      image: mysql
      name: mysql
      env:
        - name: MYSQL_ROOT_PASSWORD
          value: wppassword
      ports:
        - containerPort: 3306
          name: mysql

If there is application container, in the same namespace, trying to use the mysql container, it can directly use mysql:3306 to connect with out using the POD IP address. And mysql.namespace_name:3306 if the app is in a different namespace.

-- leodotcloud
Source: StackOverflow