'How to connect to the remote mongodb pod running in a kubernetes cluster without its IP using mongo client

I got several replicas of mongodb pods into my cluster. And got a bastion server through which I can connect to each mongodb pod running in a private subnet. I can do that by mongodb pod IP into the connection string.

mongodb://username:[email protected]:27017/

But I want to connect to the database using a pod name / service name instead of a dynamic pod IP which gets changed every time I recreate the pod. Using the pod default DNS with service name doesn't work in this case ( e.g. mongodb-0.mongo.default.svc.cluster.local)

Any idea how to connect to the mongodb pods using mongo client without using their IPs?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source