简体   繁体   English

Mongo副本节点与docker

[英]Mongo replica node with docker

I have setup a Mongo Primary node with docker using docker image https://hub.docker.com/_/mongo/ . 我使用docker image https://hub.docker.com/_/mongo/设置了一个带有docker的Mongo主节点。 Now I wish to add replica node in my current setup using docker only. 现在我希望仅使用docker在我当前的设置中添加副本节点。 Can anybody help me with this? 任何人都可以帮我吗?

You could check a guide like " Deploy a MongoDB Cluster in 9 steps Using Docker ", but that involve multiple working docker servers. 您可以查看“ 使用Docker以9个步骤部署MongoDB群集 ”这样的指南,但这涉及多个工作的docker服务器。

It uses a key file key file to be used on all nodes ( openssl rand -base64 741 > mongodb-keyfile ) for internal authentication . 它使用密钥文件密钥文件在所有节点( openssl rand -base64 741 > mongodb-keyfile )上使用,以进行内部身份验证
That allows to create an admin user, which then adds the replica servers: 这允许创建管理员用户,然后添加副本服务器:

docker run \
--name mongo \
-v /home/core/mongo-files/data:/data/db \
-v /home/core/mongo-files:/opt/keyfile \
--hostname="node1.example.com" \
--add-host node1.example.com:${node1} \
--add-host node2.example.com:${node2} \
--add-host node3.example.com:${node3} \
-p 27017:27017 -d mongo:2.6.5 \
--smallfiles \
--keyFile /opt/keyfile/mongodb-keyfile \
--replSet "rs0"

On each replica, you initiate and then check the config: 在每个副本上,您启动然后检查配置:

 rs.initiate()
 rs.conf()

Back to node 1, you declare the replica: 返回节点1,声明副本:

rs0:PRIMARY> rs.add("node2.example.com")
rs0:PRIMARY> rs.add("node3.example.com")

There are number of steps to solve this problem with docker: 解决此问题的步骤有很多步骤:

  1. switch to db admin 切换到数据库管理员

use admin 使用管理员

  1. Create a new site admin user 创建新的站点管理员用户
 db.createUser( { user: "siteUserAdmin", pwd: "password", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] }); 
  1. Create a new root user 创建新的root用户

db.createUser( { user: "siteRootAdmin", pwd: "password", roles: [ { role: "root", db: "admin" } ] }); db.createUser({user:“siteRootAdmin”,pwd:“password”,roles:[{role:“root”,db:“admin”}]});

  1. We can now initiate the replica set: 我们现在可以启动副本集:
  rs.initiate() { "info2" : "no configuration explicitly specified -- making one", "me" : "node1.example.com:27017", "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1 } 
  1. Verify the initial replica & check the configuration : 验证初始副本并检查配置:
 rs0:PRIMARY> rs.conf() { "_id" : "rs0", "version" : 1,r "members" : [ { "_id" : 0, "host" : "node1.example.com:27017" } ] } 

I built an image which provide env variables for replication and sharded cluster: khezen/mongo 我构建了一个图像,为复制和分片集群提供env变量: khezen / mongo

replica set is initialized automatically. 副本集自动初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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