容器中的MongoDb副本集,可在网桥和主机网络上访问

时间:2020-07-22 10:31:06

标签: mongodb docker docker-compose

我正在名为mongodb-local的容器中运行3个MongoDB实例,并使用以下命令将它们作为副本集启动:

mongo --port 27020 --eval 'rs.initiate({_id: "rs1", members: [ { _id: 0, host: "mongodb-local:27018"}, { _id: 1, host: "mongodb-local:27019"}, { _id: 2, host: "mongodb-local:27020"} ] })'

这允许MongoDB实例以mongodb://mongodb-local:27018,mongodb-local:27019,mongodb-local:27020/?replicaSet=rs1的身份与桥接网络上的其他服务进行对话

但是,我还想从主机/公共连接到MongoDB副本集。我已经在此处发布了3个端口,但是尝试连接和使用mongodb://localhost:27018,localhost:27019,localhost:27020/?replicaSet=rs1失败了,因为这些副本将彼此称为mongodb-local,这对于主机是无法解决的。

例如c#驱动程序中的错误,例如:

One or more errors occurred. (A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/localhost:27018" }", EndPoint: "Unspecified/localhost:27018", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.

或在外壳中

...>mongo mongodb://localhost:27020,localhost:27019,localhost:27018/?replicaSet=rs1

MongoDB shell version v4.0.2
connecting to: mongodb://localhost:27020,localhost:27019,localhost:27018/?replicaSet=rs1
2020-07-22T11:24:23.036+0100 I NETWORK  [js] Starting new replica set monitor for rs1/localhost:27020,localhost:27019,localhost:27018
2020-07-22T11:24:23.042+0100 I NETWORK  [js] Successfully connected to localhost:27018 (1 connections now open to localhost:27018 with a 5 second timeout)
2020-07-22T11:24:23.042+0100 I NETWORK  [ReplicaSetMonitor-TaskExecutor] Successfully connected to localhost:27019 (1 connections now open to localhost:27019 with a 5 second timeout)
2020-07-22T11:24:23.046+0100 I NETWORK  [ReplicaSetMonitor-TaskExecutor] Successfully connected to localhost:27020 (1 connections now open to localhost:27020 with a 5 second timeout)
2020-07-22T11:24:23.048+0100 I NETWORK  [ReplicaSetMonitor-TaskExecutor] changing hosts to rs1/mongodb-local:27018,mongodb-local:27019,mongodb-local:27020 from rs1/localhost:27018,localhost:27019,localhost:27020
2020-07-22T11:24:28.563+0100 W NETWORK  [js] Unable to reach primary for set rs1
2020-07-22T11:24:28.563+0100 I NETWORK  [js] Cannot reach any nodes for set rs1. Please check network connectivity and the status of the set. This has happened for 1 checks in a row.
2020-07-22T11:24:32.072+0100 W NETWORK  [js] Unable to reach primary for set rs1
2020-07-22T11:24:32.072+0100 I NETWORK  [js] Cannot reach any nodes for set rs1. Please check network connectivity and the status of the set. This has happened for 2 checks in a row.
2020-07-22T11:24:35.581+0100 W NETWORK  [js] Unable to reach primary for set rs1
2020-07-22T11:24:35.581+0100 I NETWORK  [js] Cannot reach any nodes for set rs1. Please check network connectivity and the status of the set. This has happened for 3 checks in a row.
2020-07-22T11:24:39.091+0100 W NETWORK  [js] Unable to reach primary for set rs1
2020-07-22T11:24:39.092+0100 I NETWORK  [js] Cannot reach any nodes for set rs1. Please check network connectivity and the status of the set. This has happened for 4 checks in a row.
2020-07-22T11:24:39.097+0100 E QUERY    [js] Error: connect failed to replica set rs1/localhost:27020,localhost:27019,localhost:27018 :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6
exception: connect failed

相反,我可以将副本集初始化为:

mongo --port 27020 --eval 'rs.initiate({_id: "rs1", members: [ { _id: 0, host: "localhost:27018"}, { _id: 1, host: "localhost:27019"}, { _id: 2, host: "localhost:27020"} ] })'

这将允许来自主机/公共的连接,并且由于巧合,因为副本都在同一容器上运行,因此它们也可以彼此通信。

但是,其他服务无法与mongodb://mongodb-local:27018,mongodb-local:27019,mongodb-local:27020/?replicaSet=rs1连接,并且出现类似的错误。

如果我想将每个副本放在单独的容器中,这也会失败。

我相信这是因为客户端在连接后会收到副本集记录,并希望该记录在其连接的网络上可用吗?如果是这样,这不是一个很好的假设。

如何在网桥网络上启动副本集,并允许来自网桥网络外部的连接?

0 个答案:

没有答案