swarm中设置的MongoDB副本集

swarm中设置的MongoDB副本集

本文介绍了启动docker swarm中设置的MongoDB副本集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 docker-compose.yml 文件中创建了两个副本集名称为 rs0 config服务器,并将其部署为 sudo docker stack部署--with-registry-auth --compose-file docker-compose.yml测试

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:

mongod.conf 文件中的内容

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,成员:[{_i d:1,主机: cfg1-r1:27017},{_id:2,主机: cfg2-r1:27017}],设置:{getLastErrorDefaults:{w: majority,wtimeout:30000}}} )'

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

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

出现错误

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显示为容器。

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.

推荐答案

您的网络必须处于重叠模式,才能在多个主机之间共享私有网络:

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

docker network create --driver overlay --attachable mongoshard

然后在您的撰写中:

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

这篇关于启动docker swarm中设置的MongoDB副本集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 08:24