skip to Main Content

I deployed a MongoDB replica set in my Kubernetes cluster. The MongoDB replica set can be easily connected with the help of internal ClusterIP within the cluster. I even connect it to my mongo-express client.

//Successfull and working fine internally
mongodb://db-mongodb-0.mycompany-mongodb-headless:27017/db

But I have to establish a remote connection for local testing and other services. I’m using the MongoDB helm chart provided by Bitnami. I opened a NodePort for both of my replica set at 30001 and 30002.

My first attempt to establish a remote connection was:

I tried to connect with my previously opened NodePort. I’m currently using Mongoose Client. The available node ports of my two replica sets are – 30001, 30002.

//Unsuccessful
mongoose
  .connect(
    `mongodb://username:password@<EXTERNAL_IP>:30001,<EXTERNAL_IP>:30002/mydb`,
    {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    }
  )

Then my second attempt was –

I tried port forwarding and tried to connect with my localhost like this –

kubectl port-forward svc/database-mongodb-0-external 27017:27017

Then I tried to establish a connection like this and failed too

//Unsuccessful 
//MongoError: Authentication failed.
mongodb://username:password@localhost:27017/database

I tried ?replicaSet=rs0 query parameters in both of the cases too. But no luck.

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!

But finally, my only question is, how can I establish or expose a remote connection with my MongoDB replica set which is set up at a Kubernetes Cluster?

My latest Bitnami/Mongodb values are

architecture: replicaset
auth:
  rootPassword: 12341234
  username: abcd
  password: 1234234
  database: abcd
  replicaSetKey: abcd
persistence:
  enabled: true
  size: 3Gi
externalAccess:
  enabled: true
  autoDiscovery:
    enabled: true
  service:
    type: NodePort
    nodePorts: ['30001', '30002']
rbac:
  create: true

2

Answers


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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search