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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM