Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection to MongoDB ReplicaSet on Kubernetes

I have deployed MongoDB ReplicaSet on Kubernetes using Helm and the chart stable/mongodb-replicaset

On Kubernetes, I can connect to MongoDB using the connection string which is something of the sort

mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl

In the event I change the number of replicas, the connection string would change as well, which also means that every application connecting to the database would need to be updated.

Is there a workaround to this?

I thought of creating a Service, so that only this would need to be changed, however the connection string does not pass regex validation.

Any help on this is appreciated.

like image 942
GZZ Avatar asked Oct 15 '25 04:10

GZZ


1 Answers

The Helm chart stable/mongodb-replicaset deploys also 2 headless services:

  1. <release name>-mongodb-replicaset
  2. <release name>-mongodb-replicaset-client

The DNS record of <release name>-mongodb-replicaset returns the address of all the replicas, so, in order to connect to the replicaset, the connection string is

"mongodb+srv://<release name>-mongodb-replicaset.namespace.svc.cluster.local/?tls=false&ssl=false"

Note that tls and ssl have been set to false for testing as they were enabled by default.

like image 143
GZZ Avatar answered Oct 17 '25 16:10

GZZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!