<p>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.</p>

<p>I have questions below:</p>

<ol>
<li>How do I get to know the the Mongo pod IP address so that I can configure this in my application.</li>
<li>My application will run on some IP &amp; 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?</li>
</ol>

<p>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.</p>

<p>Reference: <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/service/</a></p>

<p>An example using mysql:</p>

<pre><code>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
</code></pre>

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