How to expose remote connection of MongoDB replica set within Kubernetes Cluster How to expose remote connection of MongoDB replica set within Kubernetes Cluster kubernetes kubernetes

How to expose remote connection of MongoDB replica set within Kubernetes Cluster


If I understood correctly, you want to expose your MongoDB replicaset to a remote connection.

I am actively working with the Bitnami MongoDB helm chart and I can easly connect locally to the helm chart using your first attempt:

  1. I port-forward the headless service mapping the ports 27017:27017
  2. I simply connect to the localhost using username and password, the only difference with my connection string is that I do not specify the database: mongodb://<user>:<password>@localhost:27017

To answer your final question, you need to look up in the Official documentation page, there are few values for the helm chart to expose your mongodb:

  • service.type=LoadBalancer
  • externalAccess.enabled=true
  • externalAccess.autoDiscovery.enabled

You can play with those variables to find the most suitable way of exposing your MongoDB remotely.

Please, note that if you set LoadBalancer and autoDiscovery enabled, you need to make sure that your k8s cluster has a LoadBalancer in front of it (ELB in case of Amazon EKS).Do not worry about this last point if you are using a cloud managed kubernetes service since you will have this feature out of the box.


To access MongoDB deployed as replicaset from outside the K8s cluster it is needed to deploy a service per pod. I recommend you to read this section of the docs to understand the topology: https://github.com/bitnami/charts/tree/master/bitnami/mongodb#architecture

To deploy it you can follow this: https://github.com/bitnami/charts/tree/master/bitnami/mongodb#replicaset-accessing-mongodb-nodes-from-outside-the-cluster

I didn't try opening a LoadBalancer. Is there any way to establish a remote connection with my Ingress-Nginx controller to my MongoDB services? I don't know if it is possible but I tried to forward my headless services to a subdomain. But that subdomain is saying that - It looks like you are trying to access MongoDB over HTTP on the native driver port. I am not sure if it's possible!

About this, it is not possible because Ingress serve only HTTP traffic while MongoDB does not operate over HTTP