5

I have deployed mongodb as a StatefulSets on the K8S. It is not connecting, when I try to connect the DB using connection string URI(Ex: mongodb://mongo-0.mongo:27017,mongo-1.mongo:27017/cool_db), but it is connecting and getting the results when I use Endpoint IP Address.

# kubectl get sts
NAME    READY   AGE
mongo   2/2     7h33m

#kubectl get pods
NAME                                      READY   STATUS    RESTARTS   AGE
mongo-0                                   2/2     Running   0          7h48m
mongo-1                                   2/2     Running   2          7h47m

# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)     AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP     10h
mongo        ClusterIP   None         <none>        27017/TCP   7h48m

I'm trying to test the connection using connection string URI in the python using the below process but it fails.

>>> import pymongo
>>> client = pymongo.MongoClient("mongodb://mongo-0.mongo:27017,mongo-1.mongo:27017/cool_db")
>>> db = client.cool_db
>>> print db.cool_collection.count()
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/usr/lib64/python2.7/site-packages/pymongo/collection.py", line 1800, in count
   return self._count(cmd, collation, session)
 File "/usr/lib64/python2.7/site-packages/pymongo/collection.py", line 1600, in _count
   _cmd, self._read_preference_for(session), session)
 File "/usr/lib64/python2.7/site-packages/pymongo/mongo_client.py", line 1454, in _retryable_read
   read_pref, session, address=address)
 File "/usr/lib64/python2.7/site-packages/pymongo/mongo_client.py", line 1253, in _select_server
   server = topology.select_server(server_selector)
 File "/usr/lib64/python2.7/site-packages/pymongo/topology.py", line 235, in select_server
   address))
 File "/usr/lib64/python2.7/site-packages/pymongo/topology.py", line 193, in select_servers
   selector, server_timeout, address)
 File "/usr/lib64/python2.7/site-packages/pymongo/topology.py", line 209, in _select_servers_loop
   self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: mongo-0.mongo:27017: [Errno -2] Name or service not known,mongo-1.mongo:27017: [Errno -2] Name or service not known

If we use the Endpoint IP Address, then we will get a response from DB.

>>> import pymongo
>>> client = pymongo.MongoClient("mongodb://10.244.1.8,10.244.2.9:27017/cool_db")
>>> db = client.cool_db
>>> print db.cool_collection.count()
0
>>> 

I have tried with different URI's like (client = pymongo.MongoClient("mongodb://mongo-0.mongo:27017/cool_db") )but not working. Could anyone please help me?

2
  • You have only one mongo service running? Commented Dec 17, 2019 at 16:57
  • any luck in this issue? Commented Feb 17, 2021 at 15:49

4 Answers 4

3

From my previous similar answer:

From within the cluster you should reference the MongoDB Pod using <service-name>.<namespace-name>.svc.cluster.local.

Sign up to request clarification or add additional context in comments.

Comments

3

Had the same issue in a spring java connection when I used the mongodb-community-operator. Solution was, don't use the aggregated String, just use the service. So, in your case, instead of using a concatinated String of the pods like this

mongodb://mongo-0.mongo:27017,mongo-1.mongo:27017/cool_db

I used the service, which also was generated as a secret in my namespace and what was written under the key connectionString.standardSrv

mongodb+srv://<username>:<password>@mongo-svc.mongo.svc.cluster.local/cool_db?ssl=false

-> I'm assuming your sts is named mongo and your namespace is named mongo too. In that case, I could not set the username and password as extra parameters, only this one uri. But that worked.

Comments

0

I think using mongodb://mongo-0:27017 should work

6 Comments

Hi Udav, Thanks for your response, Tried it but getting the same error.
@udev, pymongo.errors.ServerSelectionTimeoutError: mongo-0:27017: [Errno -2] Name or service not known
Is your pymongo app running in the same k8s cluster?
@BellyBuster, pymongo was installed using pip on the K8S master.
Could you please tell us are you trying to connect within the same namespace?
|
0

just fresh from debugging this issue. Make sure that your mini-kube is using coredns. Mine was not and once I did a minikube delete and minikube start, things worked.

You need to see this

$ kubectl get deploy -n kube-system
NAME      READY   UP-TO-DATE   AVAILABLE   AGE
coredns   1/1     1            1           31s

or this

$ kubectl get po -n kube-system
NAME                               READY   STATUS    RESTARTS   AGE
coredns-66bff467f8-9h6qb           1/1     Running   0          46s
etcd-minikube                      1/1     Running   0          46s
kube-apiserver-minikube            1/1     Running   0          46s
kube-controller-manager-minikube   1/1     Running   0          45s
kube-proxy-d99lp                   1/1     Running   0          46s
kube-scheduler-minikube            1/1     Running   0          46s
storage-provisioner                1/1     Running   0          49s

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.