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:
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.