0

我在部署为sudo docker stack deploy --with-registry-auth --compose-file docker-compose.yml test 的 docker -compose.yml文件中创建了两个副本集名称为rs0配置服务器

version: "3.3"
services:
    cfg1-r1:
        image: mongo:3.2
        deploy:
            placement:
                constraints: [node.hostname == ram-ThinkPad-E470]
            restart_policy:
                condition: on-failure
        volumes:
            - /opt/mongoshard/tmp:/var/lib/mongodb
            - /opt/mongoshard/mongod.conf:/etc/mongod.conf
        networks:
            - mongoshard  
        command: ["mongod", "--configsvr", "--replSet", "rs0", "--dbpath", "/var/lib/mongodb", "--port", "27017"]

    cfg2-r1:
        image: mongo:3.2
        deploy:
            placement:
                constraints: [node.hostname == prasanna-ThinkPad-E470]
            restart_policy:
                condition: on-failure
        volumes:
            - /opt/mongoshard/tmp:/var/lib/mongodb
            - /opt/mongoshard/mongod.conf:/etc/mongod.conf
        networks:
            - mongoshard
        command: ["mongod", "--configsvr", "--replSet", "rs0", "--dbpath", "/var/lib/mongodb", "--port", "27017"]
networks:
    mongoshard:

mongod.conf文件中的内容

storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
net:
  port: 27017

当我尝试使用服务名称启动时

sudo docker exec -it $(sudo docker ps -qf label=com.docker.swarm.service.name=test_cfg1-r1) mongo --eval 'rs.initiate({ _id: "rs0", members: [{ _id: 1,主机:“cfg1-r1:27017”},{_id:2,主机:“cfg2-r1:27017”}],设置:{getLastErrorDefaults:{w:“多数”,wtimeout:30000}}})'

编辑我在初始化时忘记提及cfg2-r1。现在我添加了它。

它给出错误

MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
{
        "ok" : 0,
        "errmsg" : "No host described in new configuration 1 for replica set rs0 maps to this node",
        "code" : 93,
        "codeName" : "InvalidReplicaSetConfig"
}

编辑:我认为问题是 cfg1-r1 和 cfg2-r2 服务不在同一个网络中。当我部署堆栈文件时,会创建一个名为 mongo_mongoshard(overlay) 的网络,当我检查它时,它仅将 cfg1-r1 显示为容器列表中的容器。

4

2 回答 2

1

在 docker-compose.yml 文件中添加driver: overlay到网络:mongoshard:

networks:
  mongoshard:
    driver: overlay
于 2018-08-01T22:50:19.483 回答
0

您的网络必须处于覆盖模式,以允许在多个主机之间共享专用网络:

docker network create --driver overlay --attachable mongoshard

然后在您的撰写中:

networks:
  mongoshard:
    external: true

为了复制作品,您需要添加至少两个成员:

rs.initiate({ _id: "rs0", members: [{ _id: 1, host: "cfg1-r1:27017" }, { _id: 2, host: "cfg1-r2:27017" }], settings: { getLastErrorDefaults: { w: "majority", wtimeout: 30000 }}});
于 2017-10-16T10:39:07.903 回答