mongo-k8s-sidecar icon indicating copy to clipboard operation
mongo-k8s-sidecar copied to clipboard

Connect to database from outside kubernetes

Open ajouve opened this issue 5 years ago • 3 comments

I have created a mongodb replicaset on kubernetes.

This is the result of rs.status() :

{
	"set" : "rs0",
	"date" : ISODate("2019-06-12T18:12:50.606Z"),
	"myState" : 1,
	"term" : NumberLong(2),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1560363168, 1),
			"t" : NumberLong(2)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1560363168, 1),
			"t" : NumberLong(2)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1560363168, 1),
			"t" : NumberLong(2)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1560363168, 1),
			"t" : NumberLong(2)
		}
	},
	"lastStableCheckpointTimestamp" : Timestamp(1560363118, 1),
	"members" : [
		{
			"_id" : 0,
			"name" : "x.x.x.x:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 68328,
			"optime" : {
				"ts" : Timestamp(1560363168, 1),
				"t" : NumberLong(2)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1560363168, 1),
				"t" : NumberLong(2)
			},
			"optimeDate" : ISODate("2019-06-12T18:12:48Z"),
			"optimeDurableDate" : ISODate("2019-06-12T18:12:48Z"),
			"lastHeartbeat" : ISODate("2019-06-12T18:12:49.613Z"),
			"lastHeartbeatRecv" : ISODate("2019-06-12T18:12:49.697Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "y.y.y.y:27017",
			"syncSourceHost" : "y.y.y.y:27017",
			"syncSourceId" : 2,
			"infoMessage" : "",
			"configVersion" : 260577
		},
		{
			"_id" : 2,
			"name" : "y.y.y.y:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 68413,
			"optime" : {
				"ts" : Timestamp(1560363168, 1),
				"t" : NumberLong(2)
			},
			"optimeDate" : ISODate("2019-06-12T18:12:48Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1560294784, 1),
			"electionDate" : ISODate("2019-06-11T23:13:04Z"),
			"configVersion" : 260577,
			"self" : true,
			"lastHeartbeatMessage" : ""
		}
	],
	"ok" : 1,
	"operationTime" : Timestamp(1560363168, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1560363168, 1),
		"signature" : {
			"hash" : BinData(0,"rL34zviUDYPBOvAthmOgEYregkU="),
			"keyId" : NumberLong("6701412681397567489")
		}
	}
}

For example if I try to establish a connection with nodejs:

const url = 'mongodb://admin:[email protected]:27017';
const dbName = 'admin';
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
  console.log(err);
  client.close();
});

it works prefectly

const url = 'mongodb://admin:[email protected]:27017';
const dbName = 'admin';
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
  console.log(err);
  client.close();
});

This works as well

const url = 'mongodb://admin:[email protected]:27017,y.y.y.y:27017?replicaSet=rs0';
const dbName = 'admin';
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
  console.log(err);
  client.close();
});

This case does not work, and I don't really understand why:

{ MongoNetworkError: failed to connect to server [y.y.y.y: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 have exactly the same issue with Studio 3T: it works fine on one server but fails when I add two servers (timeout)

I have some more clues doing it with command line:

mongo "mongodb://proxy:27017,proxy:27018/?replicaSet=rs0"

The result is

MongoDB shell version v4.0.3
connecting to: mongodb://proxy:27017,proxy:27018/?replicaSet=rs0
2019-06-12T14:31:36.272-0400 I NETWORK  [js] Starting new replica set monitor for rs0/proxy:27017,proxy:27018
2019-06-12T14:31:36.848-0400 I NETWORK  [ReplicaSetMonitor-TaskExecutor] Successfully connected to proxy:27017 (1 connections now open to proxy:27017 with a 5 second timeout)
2019-06-12T14:31:36.855-0400 I NETWORK  [js] Successfully connected to proxy:27018 (1 connections now open to proxy:27018 with a 5 second timeout)
2019-06-12T14:31:37.079-0400 I NETWORK  [js] changing hosts to rs0/x.x.x.x:27017,y.y.y.y:27017 from rs0/proxy:27017,proxy:27018
2019-06-12T14:31:52.590-0400 W NETWORK  [js] Unable to reach primary for set rs0
2019-06-12T14:31:52.590-0400 I NETWORK  [js] Cannot reach any nodes for set rs0. Please check network connectivity and the status of the set. This has happened for 1 checks in a row.
2019-06-12T14:31:52.599-0400 E QUERY    [js] Error: connect failed to replica set rs0/proxy:27017,proxy:27018 :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6
exception: connect failed

The problem is that the connection is changing hosts to IP addresses that are not reachable directly (internal kubernetes ips) .

ajouve avatar Jul 08 '19 10:07 ajouve

Hi @ajouve, any updates for this? Did you wind up finding an alternative to this sidecar?

unfazing avatar Apr 09 '21 02:04 unfazing

I am still having the issue and connecting to the master

ajouve avatar Apr 26 '21 11:04 ajouve

+1

you can set replicaSetHorizons in mongo operator, but i don't know the parameter in this sidecar or it's not supported @ajouve @unfazing @cvallance

https://docs.mongodb.com/kubernetes-operator/master/reference/k8s-operator-specification/ image

rayshen3 avatar Mar 18 '22 06:03 rayshen3