从 Kubernetes 集群中的另一个服务连接到 MongoDB Ops Manager 上的 ReplicaSet,给出 MongooseServerSelectionError
Posted
技术标签:
【中文标题】从 Kubernetes 集群中的另一个服务连接到 MongoDB Ops Manager 上的 ReplicaSet,给出 MongooseServerSelectionError【英文标题】:Connecting to ReplicaSet on MongoDB Ops Manager from another service in the Kubernetes cluster giving MongooseServerSelectionError 【发布时间】:2020-11-01 06:05:15 【问题描述】:TL;DR
尝试从默认命名空间中的服务连接到 mongodb 命名空间中的 mongodb ops manager 副本集服务。我尝试了以下所有连接字符串格式。但是,连接正在关闭。
"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”加长版
我在“MongoDB Ops Manager”(mongodb 命名空间)中创建了副本集部署。我正在尝试从默认命名空间中的另一个服务连接到副本集
副本集 - mongo-replica-set
节点数 - 3
我尝试了不同的连接字符串格式。但是,总是连接关闭。我将可以从该服务获得的多个 IP 列入白名单。
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
我在这里缺少什么?感谢您的帮助
【问题讨论】:
这有很多例外,伙计。 我在 6 个月前尝试在 Kubernetes 中将 Mongo 作为 ReplicaSet 运行 - 所以已经有一段时间了......我不是 Kubernetes 专家......我相信我必须做的是添加 DNS 记录到内部 Kubernetes DNS 以使其正常工作。如果您执行到其中一个容器中,并尝试 ping 该主机名,它会解决吗? (或者您可以尝试使用 IP 而不是主机名)。再说一次,我已经有一段时间没有搞乱 Kube + Mongo 了,但我确实记得在让它站起来时遇到了问题。 你能连接mongo shell吗? This may also be helpful @MattOestreich:当我将主副本集端口转发到本地时,我可以使用 Compass 进行连接。在那,我使用了这个连接字符串 mongodb://localhost:27017/users @ParthShah:我已经删除了类似的堆栈跟踪行以减少问题的大小。主要是连接字符串上的变化 【参考方案1】:您可以尝试设置 useUnifiedTopology: false
并仅端口转发副本集主 pod。然后在连接字符串中,只传一个主机,不传replicaset名称。
这是唯一对我有用的东西。您甚至不需要端口转发服务,只需复制集主 pod。
【讨论】:
以上是关于从 Kubernetes 集群中的另一个服务连接到 MongoDB Ops Manager 上的 ReplicaSet,给出 MongooseServerSelectionError的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 SSH 连接到 Kubernetes 集群中的 docker 容器? [关闭]
当图像从 Kubernetes 集群中的另一个微服务提供时,如何使用下一个 js 图像优化?