0

TL;DR

Trying to connect from service in default namespace to mongodb ops manager replica-set service in mongodb namespace. I tried with all below connection string formats. But, connection getting closed.

  • "mongodb://mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017/user"
  • "mongodb://mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017/user?replicaSet=mongo-replica-set"
  • "mongodb://mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017,mongo-replica-set-1.mongo-replica-set-svc.mongodb.svc.cluster.local:27017,mongo-replica-set-2.mongo-replica-set-svc.mongodb.svc.cluster.local:27017/user"
  • "mongodb://mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017,mongo-replica-set-1.mongo-replica-set-svc.mongodb.svc.cluster.local:27017,mongo-replica-set-2.mongo-replica-set-svc.mongodb.svc.cluster.local:27017/user?replicaSet=mongo-replica-set"
  • "mongodb://mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017/user?tls=false&ssl=false"
  • "mongodb://mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017,mongo-replica-set-1.mongo-replica-set-svc.mongodb.svc.cluster.local:27017,mongo-replica-set-2.mongo-replica-set-svc.mongodb.svc.cluster.local:27017/user?tls=false&ssl=false"
  • "mongodb+srv://mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017/?tls=false&ssl=false"
  • "mongodb://mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017/user?replicaSet=rs0"

Longer Version

I created replicaset deployment in "MongoDB Ops Manager" ( mongodb namespace ) . I am trying to connect to replicaset from another service in default namespace

Replica Set - mongo-replica-set

Number of Nodes - 3

I tried with different connect string format. But, always connection getting closed. I whitelisted multiple IPs I could get from the service.

1.

const MONGO_URI = "mongodb://mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017/user"
    
    const options = {
        useUnifiedTopology: true,
        useNewUrlParser: true,
        useCreateIndex: true,
        keepAlive: true
    };
    
    mongoose.connect(MONGO_URI, options);
    
    
    MongooseServerSelectionError: connection <monitor> to 10.20.1.5:27017 closed
        at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:830:32)
        at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:335:15)
        at /app/src/index.ts:52:20
        at step (/app/src/index.ts:33:23)
        at Object.next (/app/src/index.ts:14:53)
        at /app/src/index.ts:8:71
        at new Promise (<anonymous>)
        at __awaiter (/app/src/index.ts:4:12)
        at start (/app/src/index.ts:5:15)
        at Object.<anonymous> (/app/src/index.ts:63:1)
        at Module._compile (internal/modules/cjs/loader.js:1201:30)
        at Module._compile (/app/node_modules/source-map-support/source-map-support.js:547:25)
        at Module.m._compile (/tmp/ts-node-dev-hook-19384996922428388.js:60:25)
        at Module._extensions..js (internal/modules/cjs/loader.js:1221:10)
        at require.extensions.<computed> (/tmp/ts-node-dev-hook-19384996922428388.js:62:14)
        at Object.nodeDevHook [as .ts] (/app/node_modules/ts-node-dev/lib/hook.js:61:7) {
      reason: TopologyDescription {
        type: 'Single',
        setName: null,
        maxSetVersion: null,
        maxElectionId: null,
        servers: Map(1) {
          'mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017' => [ServerDescription]
        },
        stale: false,
        compatible: true,
        compatibilityError: null,
        logicalSessionTimeoutMinutes: null,
        heartbeatFrequencyMS: 10000,
        localThresholdMS: 15,
        commonWireVersion: null
      }
    }
const MONGO_URI = "mongodb://mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017/user?replicaSet=mongo-replica-set"
    
    MongooseServerSelectionError: connection <monitor> to 10.20.1.5:27017 closed
        at NativeConnection.Connection.openUri 
        ...
{
      reason: TopologyDescription {
        type: 'ReplicaSetNoPrimary',
        setName: null,
        maxSetVersion: null,
        maxElectionId: null,
        servers: Map(1) {
          'mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017' => [ServerDescription]
        },
        stale: false,
        compatible: true,
        compatibilityError: null,
        logicalSessionTimeoutMinutes: null,
        heartbeatFrequencyMS: 10000,
        localThresholdMS: 15,
        commonWireVersion: null
      }
    }
const MONGO_URI = "mongodb://mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017,mongo-replica-set-1.mongo-replica-set-svc.mongodb.svc.cluster.local:27017,mongo-replica-set-2.mongo-replica-set-svc.mongodb.svc.cluster.local:27017/user"
    
    MongooseServerSelectionError: connection <monitor> to 10.20.1.5:27017 closed
....        
{
      reason: TopologyDescription {
        type: 'ReplicaSetNoPrimary',
        setName: null,
        maxSetVersion: null,
        maxElectionId: null,
        servers: Map(3) {
          'mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017' => [ServerDescription],
          'mongo-replica-set-1.mongo-replica-set-svc.mongodb.svc.cluster.local:27017' => [ServerDescription],
          'mongo-replica-set-2.mongo-replica-set-svc.mongodb.svc.cluster.local:27017' => [ServerDescription]
        },
        stale: false,
        compatible: true,
        compatibilityError: null,
        logicalSessionTimeoutMinutes: null,
        heartbeatFrequencyMS: 10000,
        localThresholdMS: 15,
        commonWireVersion: null
      }
    }
const MONGO_URI = "mongodb://mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017,mongo-replica-set-1.mongo-replica-set-svc.mongodb.svc.cluster.local:27017,mongo-replica-set-2.mongo-replica-set-svc.mongodb.svc.cluster.local:27017/user?replicaSet=mongo-replica-set"

    MongooseServerSelectionError: connection <monitor> to 10.20.1.5:27017 closed
       ...
{
      reason: TopologyDescription {
        type: 'ReplicaSetNoPrimary',
        setName: null,
        maxSetVersion: null,
        maxElectionId: null,
        servers: Map(3) {
          'mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017' => [ServerDescription],
          'mongo-replica-set-1.mongo-replica-set-svc.mongodb.svc.cluster.local:27017' => [ServerDescription],
          'mongo-replica-set-2.mongo-replica-set-svc.mongodb.svc.cluster.local:27017' => [ServerDescription]
        },
        stale: false,
        compatible: true,
        compatibilityError: null,
        logicalSessionTimeoutMinutes: null,
        heartbeatFrequencyMS: 10000,
        localThresholdMS: 15,
        commonWireVersion: null
      }
    }
const MONGO_URI = "mongodb://mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017/user?tls=false&ssl=false"
    
    MongooseServerSelectionError: connection <monitor> to 10.20.1.5:27017 closed
        at NativeConnection.Connection.openUri 
....
{
      reason: TopologyDescription {
        type: 'Single',
        setName: null,
        maxSetVersion: null,
        maxElectionId: null,
        servers: Map(1) {
          'mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017' => [ServerDescription]
        },
        stale: false,
        compatible: true,
        compatibilityError: null,
        logicalSessionTimeoutMinutes: null,
        heartbeatFrequencyMS: 10000,
        localThresholdMS: 15,
        commonWireVersion: null
      }
    }
const MONGO_URI =  "mongodb://mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017,mongo-replica-set-1.mongo-replica-set-svc.mongodb.svc.cluster.local:27017,mongo-replica-set-2.mongo-replica-set-svc.mongodb.svc.cluster.local:27017/user?tls=false&ssl=false"
    
    MongooseServerSelectionError: connection <monitor> to 10.20.1.5:27017 closed
        at NativeConnection.Connection.openUri 
...
 {
      reason: TopologyDescription {
        type: 'ReplicaSetNoPrimary',
        setName: null,
        maxSetVersion: null,
        maxElectionId: null,
        servers: Map(3) {
          'mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017' => [ServerDescription],
          'mongo-replica-set-1.mongo-replica-set-svc.mongodb.svc.cluster.local:27017' => [ServerDescription],
          'mongo-replica-set-2.mongo-replica-set-svc.mongodb.svc.cluster.local:27017' => [ServerDescription]
        },
        stale: false,
        compatible: true,
        compatibilityError: null,
        logicalSessionTimeoutMinutes: null,
        heartbeatFrequencyMS: 10000,
        localThresholdMS: 15,
        commonWireVersion: null
      }
    }

const MONGO_URI =  "mongodb+srv://mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017/?tls=false&ssl=false"
    
    MongoParseError: Ports not accepted with 'mongodb+srv' URIs
        at parseSrvConnectionString (/app/node_modules/mongodb/lib/core/uri_parser.js:50:21)
        at parseConnectionString (/app/node_modules/mongodb/lib/core/uri_parser.js:556:12)
        at connect (/app/node_modules/mongodb/lib/operations/connect.js:277:3)
        at /app/node_modules/mongodb/lib/mongo_client.js:222:5
        at maybePromise (/app/node_modules/mongodb/lib/utils.js:719:3)
        at MongoClient.connect (/app/node_modules/mongodb/lib/mongo_client.js:218:10)
        at /app/node_modules/mongoose/lib/connection.js:716:12
        at new Promise (<anonymous>)
        at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:709:19)
        at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:335:15)
        at /app/src/index.ts:53:20
        at step (/app/src/index.ts:33:23)
        at Object.next (/app/src/index.ts:14:53)
        at /app/src/index.ts:8:71
        at new Promise (<anonymous>)
        at __awaiter (/app/src/index.ts:4:12)
const MONGO_URI = mongodb://mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017/user?replicaSet=rs0
    
    MongooseServerSelectionError: connection <monitor> to 10.20.1.5:27017 closed
...       
 {
      reason: TopologyDescription {
        type: 'ReplicaSetNoPrimary',
        setName: null,
        maxSetVersion: null,
        maxElectionId: null,
        servers: Map(1) {
          'mongo-replica-set-0.mongo-replica-set-svc.mongodb.svc.cluster.local:27017' => [ServerDescription]
        },
        stale: false,
        compatible: true,
        compatibilityError: null,
        logicalSessionTimeoutMinutes: null,
        heartbeatFrequencyMS: 10000,
        localThresholdMS: 15,
        commonWireVersion: null
      }
    }

What am I missing here? Thanks for any help

11
  • That's an awful lot of exceptions to go through, mate. Commented Jul 11, 2020 at 17:12
  • I tried to run Mongo as a ReplicaSet in Kubernetes like 6 months ago - so it's been a while..I am no Kubernetes expert... I believe what I had to do was add a DNS record to internal Kubernetes DNS to get this to work. If you exec into one of the containers, and try to ping that hostname, does it resolve? (or you could try using the IP instead of hostname). Again, I it's been a while since I messed with Kube + Mongo, but I do remember having issues getting it stood up. Commented Jul 11, 2020 at 17:30
  • Can you connect with the mongo shell? This may also be helpful Commented Jul 11, 2020 at 17:32
  • @MattOestreich: I am able to connect using Compass when I do port-forward of the primary replica-set to my local. In that, I used this connection string mongodb://localhost:27017/users Commented Jul 11, 2020 at 18:21
  • 1
    @ParthShah: I have removed the similar stack trace lines to reduce the size of the question. Mainly the change is on the connection string Commented Jul 11, 2020 at 18:28

1 Answer 1

0

You can try setting useUnifiedTopology: false and only port-forwarding the replicaset primary pod. Then in the connection string, only pass one host, and no replicaset name.

This is the only thing that worked for me. You don't even need to port-forward the service, only the replicaset primary pod.

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

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.