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

Hello

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 …

expose:
  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 10.16.4.251 34.65.82.238 27017:32190/TCP 46m
service/mongodb-cluster-rs03-1 LoadBalancer 10.16.14.56 34.65.90.245 27017:30792/TCP 46m
service/mongodb-cluster-rs03-2 LoadBalancer 10.16.14.13 34.65.83.237 27017:31063/TCP 46m

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

Regards
John`

PS:

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 :

image

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

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

;; ANSWER SECTION:
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.

;; ADDITIONAL SECTION:
my-cluster-name-rs0-2.my-cluster-name-rs0.default.svc.cluster.local. 30 IN A 10.88.0.10
my-cluster-name-rs0-1.my-cluster-name-rs0.default.svc.cluster.local. 30 IN A 10.88.1.4
my-cluster-name-rs0-0.my-cluster-name-rs0.default.svc.cluster.local. 30 IN A 10.88.2.5

;; Query time: 1 msec
;; SERVER: 10.92.0.10#53(10.92.0.10) (UDP)
;; 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@34.65.82.238,34.65.90.245,34.65.83.237/admin?replicaSet=rs03&ssl=false"

Please let me know if it helps.

2 Likes