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

5/4/2018

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?

-- BostonHiker
kubernetes
mongodb

1 Answer

12/21/2019

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"
-- Weihang Lo
Source: StackOverflow