Initiate MongoDB replica set in docker swarm

依然范特西╮ 提交于 2019-12-13 04:19:11

问题


I have created two config servers with replica set name rs0 in docker-compose.yml file deployed as sudo docker stack deploy --with-registry-auth --compose-file docker-compose.yml test.

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:

Content in mongod.conf file

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

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

When I try to initiate using service name as

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, host: "cfg1-r1:27017" }, { _id: 2, host: "cfg2-r1:27017" }], settings: { getLastErrorDefaults: { w: "majority", wtimeout: 30000 }}})'

Edit I Forgot to mention cfg2-r1 while initializing. Now I added it.

It's giving error

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"
}

EDIT: I think the problem is cfg1-r1 and cfg2-r2 services are not in same network. When I deploy stack file, a network called mongo_mongoshard(overlay) is created and when I inspect it, it is showing only cfg1-r1 as containers in it's container list.


回答1:


Your network has to be on overlay mode, to allow private network sharing between multiple hosts :

docker network create --driver overlay --attachable mongoshard

Then in your compose :

networks:
  mongoshard:
    external: true

In order for replica works, you need to add at least two members :

rs.initiate({ _id: "rs0", members: [{ _id: 1, host: "cfg1-r1:27017" }, { _id: 2, host: "cfg1-r2:27017" }], settings: { getLastErrorDefaults: { w: "majority", wtimeout: 30000 }}});



回答2:


Add the driver: overlay to the mongoshard: network in docker-compose.yml file:

networks:
  mongoshard:
    driver: overlay


来源:https://stackoverflow.com/questions/46743591/initiate-mongodb-replica-set-in-docker-swarm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!