我在部署为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 显示为容器列表中的容器。