连接到远程dockerized MongoDB副本集

时间:2019-03-04 15:38:52

标签: mongodb docker replicaset

在我的远程机器上,我设置了一个我使用docker-compose管理的docker容器机器。 我为副本集中的每个MongoDB实例创建了3个Docker容器

mongodb_01:
    image: mvertes/alpine-mongo
    entrypoint: ['/usr/bin/mongod', '--bind_ip_all', '--replSet', 'rs0']
    restart: always
    ports:
      - 10001:27017
    volumes:
      - ./mongodb/01:/data/db

mongodb_02:
    image: mvertes/alpine-mongo
    entrypoint: ['/usr/bin/mongod', '--bind_ip_all', '--replSet', 'rs0']
    restart: always
    depends_on:
       - mongodb_01
    ports:
      - 10002:27017
    volumes:
      - ./mongodb/02:/data/db

mongodb_03:
    image: mvertes/alpine-mongo
    entrypoint: ['/usr/bin/mongod', '--bind_ip_all', '--replSet', 'rs0']
    restart: always
    depends_on:
      - mongodb_02
    ports:
      - 10003:27017
    volumes:
      - ./mongodb/03:/data/db

我还配置了副本集。这是摘录:

"_id" : "rs0",
...
"members" : [
    {
        "_id" : 0,
        "host" : "mongodb_01:27017",
        ...
    },
    {
        "_id" : 1,
        "host" : "mongodb_02:27017",
        ...
    },
    {
        "_id" : 2,
        "host" : "mongodb_03:27017",
        ...
    }
],
...
}

一切正常,使用连接字符串,其他docker映像与该副本集之间的内部通信正常运行

mongodb://mongodb_01:27017,mongodb_02:27017,mongodb_03:27017/<database>?replicaSet=rs0

问题是当我需要将远程客户端连接到此副本集时。 例如,通过开发机器上的节点使用猫鼬,我得到:

MongoNetworkError: failed to connect to server [mongodb_02:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND mongodb_02 mongodb_02:27017]

有时在mongodb_03上失败。

编辑:如前所述,这是我来自远程计算机的连接字符串:

mongodb://<remote-host>:10001,<remote-host>:10002,<remote-host>:10003/<database>?replicaSet=rs0

编辑2 :使用Mongodb Compass之类的客户端,我可以成功地正确连接到单个实例。当我添加副本集时,出现错误。 因此,我尝试使用mongodb创建一个虚拟容器(使用mongo:latest)。

$ docker run -it mongo:latest bash

并运行

mongo mongodb://<remote-host>:10001,<remote-host>:10002,<remote-host>:10003/<database>?replicaSet=rs0

我明白了

MongoDB shell version v4.0.6
connecting to: mongodb://<remote-host>:10001,<remote-host>:10002,<remote-host>:10003/<database>?gssapiServiceName=mongodb&replicaSet=rs0
2019-03-04T16:12:54.375+0000 I NETWORK  [js] Starting new replica set monitor for rs0/<remote-host>:10001,<remote-host>:10002,<remote-host>:10003
2019-03-04T16:12:54.377+0000 I NETWORK  [ReplicaSetMonitor-TaskExecutor] Successfully connected to <remote-host>:10003 (1 connections now open to <remote-host>:10003 with a 5 second timeout)
2019-03-04T16:12:54.377+0000 I NETWORK  [js] Successfully connected to <remote-host>:10001 (1 connections now open to <remote-host>:10001 with a 5 second timeout)
2019-03-04T16:12:54.378+0000 I NETWORK  [js] changing hosts to rs0/mongodb_01:27017,mongodb_02:27017,mongodb_03:27017 from rs0/<remote-host>:10001,<remote-host>:10002,<remote-host>:10003
2019-03-04T16:12:54.882+0000 W NETWORK  [js] Unable to reach primary for set rs0
2019-03-04T16:12:54.882+0000 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.

以此类推。

感谢您的帮助和建议!

3 个答案:

答案 0 :(得分:2)

我遇到了与您完全相同的问题,我已经解决了。

这是因为您的远程客户端不知道'mongo1:27017'主机。仅在docker网络内部使用。

解释我如何解决此问题有点棘手。我将首先显示docker-compose.yml。

version: "3.3"
services:
  mongo-primary:
    container_name: mongo-primary
    hostname: mongo-primary
    image: mongo:4.0.11
    volumes:
      - $HOME/.dockerMongoRepl/primary/data/db:/data/db
      - $HOME/.dockerMongoRepl/keyfile:/data/keyfile
    extra_hosts:
      - "address.whichCanAccess.yourServer:192.168.1.xx"
    networks:
      - mongo-cluster
    ports:
      - 27017:27017
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: changeme
    command: --bind_ip_all --auth --keyFile /data/keyfile/mongo-cluster-key --replSet rs0 --enableMajorityReadConcern false
  mongo-secondary:
    container_name: mongo-secondary
    hostname: mongo-secondary
    image: mongo:4.0.11
    volumes:
      - $HOME/.dockerMongoRepl/secondary/data/db:/data/db
      - $HOME/.dockerMongoRepl/keyfile:/data/keyfile
    depends_on:
      - mongo-primary
    extra_hosts:
      - ""address.whichCanAccess.yourServer:192.168.1.xx""
    networks:
      - mongo-cluster
    ports:
      - 27018:27017
    restart: always
    command: --bind_ip_all -auth --keyFile /data/keyfile/mongo-cluster-key --replSet rs0 --enableMajorityReadConcern false
  mongo-arbiter:
    container_name: mongo-arbiter
    hostname: mongo-arbiter
    image: mongo:4.0.11
    volumes:
      - $HOME/.dockerMongoRepl/arbiter/data/arb:/data/arb
      - $HOME/.dockerMongoRepl/keyfile:/data/keyfile
    depends_on:
      - mongo-primary
    extra_hosts:
      - ""address.whichCanAccess.yourServer:192.168.1.xx""
    networks:
      - mongo-cluster
    ports:
      - 27019:27017
    restart: always
    command: --bind_ip_all --auth --keyFile /data/keyfile/mongo-cluster-key --replSet rs0 --enableMajorityReadConcern false

networks:
  mongo-cluster:

!重要的部分是“额外主机”!它可以使容器访问主机。

“ address.WhichCanAccess.yourServer” <-在我的情况下,我的asus路由器已设置为asus ddns,因此它将为XXX.asuscomm.com

“ 192.168.1.xx” <-华硕路由器为主机分配的IP地址

也许不需要那些撰写文件的某些配置。

使用docker-compose运行3个容器。

接下来,输入主要对象的mongo shell,如下所示设置副本

config = {
  "_id": "rs0",
  "members": [{
    "_id": 0,
    "host": "address.whichCanAccess.yourServer:27017"
  }, {
    "_id": 1,
    "host": "address.whichCanAccess.yourServer:27018"
  }, {
    "_id": 2,
    "host": "address.whichCanAccess.yourServer:27019",
    arbiterOnly: true
  }]
}

rs.initiate(config)

通过这种方式,mongo容器将通过docker的主机网络相互通信,并且可以从远程IP进行访问。

整个过程可以在here中找到。

答案 1 :(得分:1)

您可以为此使用localhost。撰写内容大致如下:

version: "3"
services:
  mongodb:
    image: mongo:4.0.11
    ports:
      - "27017:27017"
    extra_hosts:
      - "localhost:0.0.0.0"
    volumes:
      - "./lambda/docker/mongod.conf:/etc/mongod.conf"

然后在初始化副本集时,请确保将主机设置为localhost。示例:

{
    // ........
    "members" : [ 
        {
            "_id" : 0,
            "host" : "localhost:27017"
            // .........
        }
    ]
    // ...........
}

在OSX上测试。在linux / windows上,它可能具有不同的行为。

答案 2 :(得分:0)

我最终使用了免费版本的Atlas进行测试和集成。工作完成后,我将在服务器上使用自己的副本集。

这有点麻烦,在没有开发工具和调试器支持的情况下,我需要在生产前对所有内容进行三重检查。