Connect from localhost to a mongodb replicaset into a kubernetes cluster

6/12/2019

I have set up a mongo database into a kubernetes cluster. The mongo have two instances mongo-0 and mongo-1

I would like to connect my instance via Studio T3, Compass, NodeJs script but I have some problems :D

First I am forwarding the two ports doing

kubectl port-forward --namespace=staging mongo-0 27017:27017

and

kubectl port-forward --namespace=staging mongo-1 27018:27017

Then I am trying to connect via:

Studio T3 I have the following logs on my mongo instance

2019-06-11T23:16:13.535+0000 I NETWORK  [listener] connection accepted from 127.0.0.1:50002 #38 (2 connections now open)
2019-06-11T23:16:13.850+0000 I NETWORK  [conn38] received client metadata from 127.0.0.1:50002 conn38: { driver: { name: "mongo-java-driver|legacy", version: "019.1.0-FINAL-1091-g366aa8c04a" }, os: { type: "Darwin", name: "Mac OS X", architecture: "x86_64", version: "10.14.5" }, platform: "Java/Oracle Corporation/1.8.0_202-b08" }
2019-06-11T23:30:09.037+0000 I NETWORK  [conn38] end connection 127.0.0.1:50002 (2 connections now open)

Logs on the GUI

Connection failed.

SERVER [x.x.x.x:27017] (Type: UNKNOWN) 
|_/ Connection error (MongoSocketOpenException): Exception opening socket
|____/ I/O error: connect timed out

SERVER [x.x.x.x:27017] (Type: UNKNOWN) 
|_/ Connection error (MongoSocketOpenException): Exception opening socket
|____/ I/O error: connect timed out

Details:
Timed out after 30000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1@318b89b8. Client view of cluster state is {type=REPLICA_SET, servers=[{address=x.x.x.x:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}, {address=x.x.x.x:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}]

Now with a basic NodeJs script

2019-06-12T01:09:04.692+0000 I NETWORK  [listener] connection accepted from 127.0.0.1:41320 #1419 (3 connections now open)
2019-06-12T01:09:04.980+0000 I NETWORK  [conn1419] received client metadata from 127.0.0.1:41320 conn1419: { driver: { name: "nodejs", version: "3.2.7" }, os: { type: "Darwin", name: "darwin", architecture: "x64", version: "18.6.0" }, platform: "Node.js v10.15.0, LE, mongodb-core: 3.2.7" }
2019-06-12T01:09:05.100+0000 I NETWORK  [conn1419] end connection 127.0.0.1:41320 (2 connections now open)

Node is returning mi this error

{ MongoNetworkError: failed to connect to server [x.x.x.x:27017] on first connect [MongoNetworkError: connection timed out]
    at Pool.<anonymous> (/Volumes/Work/test/mongo/node_modules/mongodb-core/lib/topologies/server.js:431:11)
    at Pool.emit (events.js:182:13)
    at connect (/Volumes/Work/test/mongo/node_modules/mongodb-core/lib/connection/pool.js:557:14)
    at makeConnection (/Volumes/Work/test/mongo/node_modules/mongodb-core/lib/connection/connect.js:39:11)
    at callback (/Volumes/Work/test/mongo/node_modules/mongodb-core/lib/connection/connect.js:261:5)
    at Socket.err (/Volumes/Work/test/mongo/node_modules/mongodb-core/lib/connection/connect.js:286:7)
    at Object.onceWrapper (events.js:273:13)
    at Socket.emit (events.js:182:13)
    at Socket._onTimeout (net.js:453:8)
    at ontimeout (timers.js:436:11)
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }

I do not really understand why I have those timeouts. Maybe because after the first connection my local connector is trying to reach via the public mongo ip, the one I replaced by x.x.x.x and the connection can not be made.

-- Ajouve
kubernetes
mongodb

0 Answers