简体   繁体   中英

Initiate MongoDB replica set in docker swarm

I have created two with replica set name in file deployed as . 部署文件中创建了两个副本集名称为

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

I Forgot to mention cfg2-r1 while initializing. 我在初始化时忘记提及cfg2-r1。 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"
}

I think the problem is cfg1-r1 and cfg2-r2 services are not in same network. 我认为问题是cfg1-r1和cfg2-r2服务不在同一网络中。 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.

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

networks:
  mongoshard:
    driver: overlay

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 }}});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM