Kubernetes, replicaset, no sharding - where is the service to access the replicaset


I am running the a replicaset (3) and it works very nice. But the problem is how to access it with port-forward.

As I can see there is a service available but it seems it is auto generated

service/mongodb-cluster-rs03 ClusterIP None 27017/TCP 80m

Since it is has ClusterIP, it can’t be accessed via port-forward. Therefore this has to be set …

  enabled: true
  exposeType: LoadBalancer

in order to make the pods accessible, which is not really great (is there a load balancing in place at all ?)

service/mongodb-cluster-rs03-0 LoadBalancer 27017:32190/TCP 46m
service/mongodb-cluster-rs03-1 LoadBalancer 27017:30792/TCP 46m
service/mongodb-cluster-rs03-2 LoadBalancer 27017:31063/TCP 46m

Question: How can the service get a exposeType = LoadBalancer ?



This is may be not a great setup - what is needed is a service which makes sure that requests are somehow load balanced. So my hope is that the existing Service does that :


1 Like

Hey @jamoser ,

mongodb-cluster-rs03 is a headless service. It does not have any load balancing behind it and it just creates endpoint slices and domain names. You can use SRV then to connect to MongoDB within k8s cluster:

# dig SRV my-cluster-name-rs0.default.svc.cluster.local

; <<>> DiG 9.18.1-1ubuntu1.2-Ubuntu <<>> SRV my-cluster-name-rs0.default.svc.cluster.local
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 3691
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 3

;my-cluster-name-rs0.default.svc.cluster.local. IN SRV

my-cluster-name-rs0.default.svc.cluster.local. 30 IN SRV 10 33 0 my-cluster-name-rs0-2.my-cluster-name-rs0.default.svc.cluster.local.
my-cluster-name-rs0.default.svc.cluster.local. 30 IN SRV 10 33 0 my-cluster-name-rs0-1.my-cluster-name-rs0.default.svc.cluster.local.
my-cluster-name-rs0.default.svc.cluster.local. 30 IN SRV 10 33 0 my-cluster-name-rs0-0.my-cluster-name-rs0.default.svc.cluster.local.

my-cluster-name-rs0-2.my-cluster-name-rs0.default.svc.cluster.local. 30 IN A
my-cluster-name-rs0-1.my-cluster-name-rs0.default.svc.cluster.local. 30 IN A
my-cluster-name-rs0-0.my-cluster-name-rs0.default.svc.cluster.local. 30 IN A

;; Query time: 1 msec
;; WHEN: Mon Nov 14 10:13:04 UTC 2022
;; MSG SIZE  rcvd: 372

So your mongoDB URI will be:

mongo "mongodb+srv://databaseAdmin:databaseAdminPassword@mongodb-cluster-rs03.<namespace name>.svc.cluster.local/admin?replicaSet=rs03&ssl=false"

Note the +srv!

For external connection you will need to specify all the nodes in your connection string and let MongoDB driver do the balancing.

In your case it will be:

mongo "mongodb+srv://databaseAdmin:databaseAdminPassword@,,"

Please let me know if it helps.