Connection to MongoDB ReplicaSet on Kubernetes Connection to MongoDB ReplicaSet on Kubernetes kubernetes kubernetes

Connection to MongoDB ReplicaSet on Kubernetes


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.


The drivers will discover all nodes of a replica set automatically. If you are only changing the total number but the first one always has the same address, you can specify just the first node's address in the connection string.

Depending on the driver this may or may not connect to the replica set (as opposed directly to that one node), which generally has two solutions:

  1. Add replicaSet option to the URI.
  2. Provide first two node addresses.

Alternatively, you can set up SRV records and use an SRV URI which is how MongoDB Atlas works. This applies to all topologies (including sharded clusters, which replica set discovery does not apply to).