3

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
Md Fazlul Karim
  • 355
  • 7
  • 15
  • Can you provide your values.yaml of your helm chart deployment? I am using the bitnami helm chart and I'm able to connect it from outside. – Giovanni Patruno Mar 27 '21 at 09:57
  • 1
    I have updated the question with my values YAML for your reference. Thanks for replying. I really appreciate it. – Md Fazlul Karim Mar 27 '21 at 13:30
  • I'm repeatedly getting MongoError: Authentication Failed where connection with [ mongodb://db-mongodb-0.db-mongodb-headless:27017/db ] is successful internally. – Md Fazlul Karim Mar 27 '21 at 13:59
  • Do you need to sue the NodePorts? I think is better to keep load balancer. Is your cluster managed? Furthermore, if you need to apply the new manifest make sure to do a fresh install and make sure to delete the PVS because they are storing some configs and they are not automatically deleted using the helm delete command! – Giovanni Patruno Mar 27 '21 at 14:27
  • Yes, my cluster is managed. You have identified a major issue. I didn't think about that. I'll do a fresh install and give an update here. Thanks again. – Md Fazlul Karim Mar 27 '21 at 18:53

2 Answers2

4

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.

Giovanni Patruno
  • 651
  • 9
  • 15
  • I tried setting autoDiscovery and externalAccess true. I tried without mentioning the database too. But no luck. Can you please share your current bitnami/mongodb values with me? I have added mine in the question. – Md Fazlul Karim Mar 27 '21 at 13:49
  • Sure: replicaCount: 2, architecture: replicaset, arbiter.enabled: false, rbac.create: true, persistence.enabled: true, persistence.size: 8Gi. As you can see I'm not exposing the cluster externally but with the port-forward is working fine. – Giovanni Patruno Mar 27 '21 at 14:26
1

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

miguelaeh
  • 91
  • 2