Pymongo doesn't use MongoClient defined IP addresses in Replica Set

8/9/2016

I'm running a Kubernetes Cluster of MongoDB ReplicaSets.

I've initialized the replicaset just fine and everything is syncing with one another.

members" : [
            {
                    "_id" : 1,
                    "host" : "10.244.7.3:27017",
                    "arbiterOnly" : false,
                    "buildIndexes" : true,
                    "hidden" : false,
                    "priority" : 1,
                    "tags" : {

                    },
                    "slaveDelay" : 0,
                    "votes" : 1
            },
            {
                    "_id" : 2,
                    "host" : "10.244.6.4:27017",
                    "arbiterOnly" : false,
                    "buildIndexes" : true,
                    "hidden" : false,
                    "priority" : 1,
                    "tags" : {

                    },
                    "slaveDelay" : 0,
                    "votes" : 1
            },
            {
                    "_id" : 3,
                    "host" : "10.244.7.5:27017",
                    "arbiterOnly" : false,
                    "buildIndexes" : true,
                    "hidden" : false,
                    "priority" : 1,
                    "tags" : {

                    },
                    "slaveDelay" : 0,
                    "votes" : 1
            }
    ]

But when I attempt to use pymongo to connect using the privately exposed ip addresses (in the range of 172.20.x), running a query attempts to connect to the replicaset using the ip that was given by the mongo replication servers.

import pymongo                                                                                          
from pymongo import MongoClient                                                                         


mongo_uri = "mongodb://172.20.0.19:31892,172.20.0.80:31892,172.20.0.49:31892,172.20.0.147:31892/a-dev?replicaSet=rs0"

c = MongoClient(mongo_uri)
c['test_db'].tab.find_one()

# Returns a
*** pymongo.errors.ServerSelectionTimeoutError: 10.244.6.4:27017: timed out,10.244.7.5:27017: timed out,10.244.7.3:27017: timed out

This shows that pymongo is using the values provided from the replicaset's configuration instead of using the ip addresses which I've defined. Is there anyway to disable this default behavior?

-- David C
kubernetes
mongodb
mongodb-replica-set
pymongo

0 Answers