Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using mongodb+srv to connect to mongodb deployed as a statefulset

I have deployed a 3 pod mongodb statefulset in kubernetes and I am attempting to use the new mongodb+srv connection string (mongodb 3.6) to connect to the headless k8s service that has the SRV records for the cluster members.

However, the connection is failing as follows (the mongo command is being executed on the first pod in the satefulset):

root@mongodb-0:/# mongo "mongodb+srv://mongodb-headless.mongo.svc.cluster.local"
FailedToParse: Hostname mongodb-0.mongodb-headless.mongo.svc.cluster.local. is not within the domain mongo.svc.cluster.local
try 'mongo --help' for more information

Here is the headless service configuration:

kubectl describe svc/mongodb-headless -n mongo
Name:              mongodb-headless
Namespace:         mongo
Labels:            app=mongodb-headless
                   chart=mongodb-1.0.1
                   heritage=Tiller
                   release=mongo
Annotations:       service.alpha.kubernetes.io/tolerate-unready-endpoints=true
Selector:          app=mongodb,release=mongo
Type:              ClusterIP
IP:                None
Port:              mongodb  27017/TCP
TargetPort:        27017/TCP
Endpoints:         
192.168.16.8:27017,192.168.208.3:27017,192.168.64.9:27017
Session Affinity:  None
Events:            <none>

The mongodb cluster is functional and I can connect to the members over localhost or using a separate (non-headless) service (e.g. mongo "mongodb://mongodb.mongo.svc.cluster.local").

Am I missing something in the mongodb+srv requirements/implementation or do I need to adjust something in my k8s deployment?

like image 292
BostonHiker Avatar asked May 04 '18 16:05

BostonHiker


People also ask

What is SRV connection in MongoDB?

The use of SRV records eliminates the requirement for every client to pass in a complete set of state information for the cluster. Instead, a single SRV record identifies all the nodes associated with the cluster (and their port numbers) and an associated TXT record defines the options for the URI.


1 Answers

The connections of mongodb+srv:// use SSL/TLS by default. You need to disable them manually by adding tls=false or ssl=false.

The following connection URI works for me on a MongoDB 4.2 three members replica set on GKE.

mongo "mongodb+srv://svc-headless.my-namespace.cluster.local/?tls=false&ssl=false"

Add replicaSet query option may help to connect to the right replica set.

mongo "mongodb+srv://svc-headless.my-namespace.cluster.local/?tls=false&ssl=false&replicaSet=rs0"
like image 56
Weihang Lo Avatar answered Nov 15 '22 03:11

Weihang Lo