docker搭建mongo集群

docker搭建mongo集群

参考:

Mongo分片集群部署(http://www.cnblogs.com/kaka171129/p/8024704.html#commentform)

Sharded Cluster Components(https://docs.mongodb.com/manual/core/sharded-cluster-components/

集群结构

典型的三分片Mongo集群如下图所示,包含三类组件:查询路由、配置服务器、分片。

其中查询路由为mongos进程,配置服务器和分片都是mongod进程。配置服务器和分片都采取副本集(replica set)来确保可用性和健壮性,每个副本集最少包含三个节点。查询路由都是单实例运行,没有副本集机制,可根据需要增加实例数量,也可以在外部添加负载均衡。

 

生产结构

 

非生产结构

 

 

Docker操作

 

配置docker hub的源,这里配置网易的

vi /etc/docker/ daemon.json

{

  "registry-mirrors":["http://hub-mirror.c.163.com","https://registry.docker-cn.com"]

}

 

获取mongo3.2的镜像。(获取之前可以配置一下docker的相关路径,参考

http://www.cnblogs.com/jinanxiaolaohu/p/8301786.html

http://www.cnblogs.com/Bourbon-tian/p/7206642.html

操作前要ps –ef|grep docker,记录下原来的启动内容,以免异常。

docker pull mongo:3.2

 

计划

使用非生产结构

mongod:20001

config set:21001

mongos:23001

mongod:20002

config set:21002

mongos:23002

mongod:20003

config set:21003

mongos:23003

 

操作

1.数据复制集启动:

docker run –v /home/dockermong/db1:/data/db:z –p 20001:27017 –d mongo:3.2 –replSet rs0

docker run –v /home/dockermong/db1:/data/db:z –p 20002:27017 –d mongo:3.2 –replSet rs0

docker run –v /home/dockermong/db1:/data/db:z –p 20003:27017 –d mongo:3.2 –replSet rs0

 

2.检查

使用工具测试容器是否启动正常

 

3.创建复制集

随便进去一台mongo的控制台执行

rs.initiate(

  {

         _id: "rs0",

         members: [

           { _id : 0, host : "docker服务器的IP:20001" },

         ]

  }

)

//查看状态

rs.status()

//增加其他mongo到复制集

rs.add(“docker服务器的IP:20002”)

rs.add(“docker服务器的IP:20003”)

//检查复制集,会看到1个primary,2个secondary

rs.status()

//在primary输入,在primary或secondary查询

 

4.创建配置复制集

docker run –v /home/dockermong/configdb1:/data/configdb:z –p 21001:27017 –d mongo:3.2 –replSet confrs --configsvr

docker run –v /home/dockermong/configdb2:/data/configdb:z –p 22002:27017 –d mongo:3.2 –replSet confrs --configsvr

docker run –v /home/dockermong/configdb3:/data/configdb:z –p 23003:27017 –d mongo:3.2 –replSet confrs --configsvr

 

5.重复步骤3,创建配置复制集

 

6.创建集群路由mongos,这样就关联的mongos和配置集

docker run --name clu-mongos1 -p 23001:27017 -d --user mongodb  mongo:3.2  mongos --configdb configrs/docker服务器的IP:21001,docker服务器的IP:21002,docker服务器的IP:21003

 

7.关联数据复制集到路由(创建分片)

在mongos是上创建分片

sh.addShard( "rs0/docker服务器的IP:20001")

分片集群就会自动获取该分片下的其他复制集的信息

8.查看分片信息

sh.status()

会看到数据复制集的3个服务都加入到了shards中,并且会同步了对应的数据

 

9.使用

接入的时候可以把多个mongos对应的IP:PORT作为访问地址,可以起到mongos的高可用效果

注意:

1. docker run –v :z,解决selinux的问题,或者可以取消selinux模式

https://docs.docker.com/engine/reference/commandline/run/#mount-volumes-from-container-volumes-from

Labeling systems like SELinux require that proper labels are placed on volume content mounted into a container. Without a label, the security system might prevent the processes running inside the container from using the content. By default, Docker does not change the labels set by the OS.

To change the label in the container context, you can add either of two suffixes :z or :Z to the volume mount. These suffixes tell Docker to relabel file objects on the shared volumes. The z option tells Docker that two containers share the volume content. As a result, Docker labels the content with a shared content label. Shared volume labels allow all containers to read/write content. The Z option tells Docker to label the content with a private unshared label. Only the current container can use a private volume.

 

 

2.mongos启动要增加“--user mongodb”,解决

chown: cannot dereference '/proc/1/fd/1': Permission denied

chown: cannot dereference '/proc/1/fd/2': Permission denied

 

 

其他:

1.生产的多分片模式

2.权限验证

3.复制集(Replication Set)的tag的运用

4.docker compose

posted @ 2018-02-07 17:48  jinrain  阅读(5680)  评论(0编辑  收藏  举报