CentOS7+Docker+MangoDB下部署簡單的MongoDB分片叢集
簡單的在Docker上快速部署MongoDB分片叢集
前言
文中使用的環境如下
OS:CentOS Linux release 7.5.1804 (Core)
Docker:Docker version 18.06.1-ce, build e68fc7a
docker image中的MongoDB :v4.0.4
第一步:拉取映象
docker pull mongo
執行 docker images
得到如下結果,證明映象已經拉取完畢
[[email protected]_0_7_centos ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mongo latest 525bd2016729 2 weeks ago 383MB
第二步:建立配置服務複製集和分片複製集
建立配置服務複製集
docker run --name configsvr0 -d mongo --configsvr --replSet "rs_configsvr" --bind_ip_all docker run --name configsvr1 -d mongo --configsvr --replSet "rs_configsvr" --bind_ip_all docker run --name configsvr2 -d mongo --configsvr --replSet "rs_configsvr" --bind_ip_all
建立分片0複製集
docker run --name shardsvr00 -d mongo --shardsvr --replSet "rs_shardsvr0" --bind_ip_all
docker run --name shardsvr01 -d mongo --shardsvr --replSet "rs_shardsvr0" --bind_ip_all
docker run --name shardsvr02 -d mongo --shardsvr --replSet "rs_shardsvr0" --bind_ip_all
建立分片1複製集
docker run --name shardsvr10 -d mongo --shardsvr --replSet "rs_shardsvr1" --bind_ip_all docker run --name shardsvr11 -d mongo --shardsvr --replSet "rs_shardsvr1" --bind_ip_all docker run --name shardsvr12 -d mongo --shardsvr --replSet "rs_shardsvr1" --bind_ip_all
執行 docker ps
得到如下結果,證明配置服務和分片集都成功啟動了
[[email protected]M_0_7_centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e1f9851e714 mongo "docker-entrypoint.s…" 21 seconds ago Up 20 seconds 27017/tcp shardsvr12
a015c3b1c001 mongo "docker-entrypoint.s…" 29 seconds ago Up 29 seconds 27017/tcp shardsvr11
4a9b33a8b641 mongo "docker-entrypoint.s…" 36 seconds ago Up 36 seconds 27017/tcp shardsvr10
8ceef0e613a0 mongo "docker-entrypoint.s…" 57 seconds ago Up 57 seconds 27017/tcp shardsvr02
e25eb6431963 mongo "docker-entrypoint.s…" About a minute ago Up About a minute 27017/tcp shardsvr01
d2746bfd295b mongo "docker-entrypoint.s…" About a minute ago Up About a minute 27017/tcp shardsvr00
d4709d527fb8 mongo "docker-entrypoint.s…" 10 minutes ago Up 10 minutes 27017/tcp configsvr2
50b2a3a3487a mongo "docker-entrypoint.s…" 10 minutes ago Up 10 minutes 27017/tcp configsvr1
95e1549af67a mongo "docker-entrypoint.s…" 10 minutes ago Up 10 minutes 27017/tcp configsvr0
執行 docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(docker ps -aq)
得到我們剛剛啟動的容器的名稱和ip列表
[[email protected]_0_7_centos ~]# docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(docker ps -aq)
/shardsvr12 - 172.17.0.10
/shardsvr11 - 172.17.0.9
/shardsvr10 - 172.17.0.8
/shardsvr02 - 172.17.0.7
/shardsvr01 - 172.17.0.6
/shardsvr00 - 172.17.0.5
/configsvr2 - 172.17.0.4
/configsvr1 - 172.17.0.3
/configsvr0 - 172.17.0.2
第三步:初始化配置複製集和分片複製集
執行以下指令(分步執行)進入配置集的第一個容器並初始化配置集
ps:使用--configsvr
構建的配置容器預設的埠是27019
docker exec -it configsvr0 bash
mongo --host 172.17.0.2 --port 27019
rs.initiate(
{
_id: "rs_configsvr",
configsvr: true,
members: [
{ _id : 0, host : "172.17.0.2:27019" },
{ _id : 1, host : "172.17.0.3:27019" },
{ _id : 2, host : "172.17.0.4:27019" }
]
}
)
執行效果如下
[[email protected]_0_7_centos ~]# docker exec -it configsvr0 bash
[email protected]:/# mongo --host 172.17.0.2 --port 27019
MongoDB shell version v4.0.4
connecting to: mongodb://172.17.0.2:27019/
Implicit session: session { "id" : UUID("a8eacad5-85bb-450f-a4c9-855009f8bceb") }
MongoDB server version: 4.0.4
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2018-12-05T03:25:52.144+0000 I STORAGE [initandlisten]
2018-12-05T03:25:52.144+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-12-05T03:25:52.144+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-12-05T03:25:53.213+0000 I CONTROL [initandlisten]
2018-12-05T03:25:53.213+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-12-05T03:25:53.213+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-12-05T03:25:53.213+0000 I CONTROL [initandlisten]
2018-12-05T03:25:53.213+0000 I CONTROL [initandlisten]
2018-12-05T03:25:53.213+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-12-05T03:25:53.213+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-12-05T03:25:53.213+0000 I CONTROL [initandlisten]
2018-12-05T03:25:53.213+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-12-05T03:25:53.213+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-12-05T03:25:53.213+0000 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> rs.initiate(
... {
... _id: "rs_configsvr",
... configsvr: true,
... members: [
... { _id : 0, host : "172.17.0.2:27019" },
... { _id : 1, host : "172.17.0.3:27019" },
... { _id : 2, host : "172.17.0.4:27019" }
... ]
... }
... )
{
"ok" : 1,
"operationTime" : Timestamp(1543989131, 1),
"$gleStats" : {
"lastOpTime" : Timestamp(1543989131, 1),
"electionId" : ObjectId("000000000000000000000000")
},
"lastCommittedOpTime" : Timestamp(0, 0),
"$clusterTime" : {
"clusterTime" : Timestamp(1543989131, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
可以看到執行中有警告,大意是我們沒有配置訪問限制和和一些建議的配置,此處略過這些警告
如何退出MangoDB/容器的命令列?當然是exit
啊!
同理,我們初始化兩個分片集:
ps:使用--shardsvr
構建的分片容器預設的埠是27018
docker exec -it shardsvr00 bash
mongo --host 172.17.0.5 --port 27018
rs.initiate(
{
_id : "rs_shardsvr0",
members: [
{ _id : 0, host : "172.17.0.5:27018" },
{ _id : 1, host : "172.17.0.6:27018" },
{ _id : 2, host : "172.17.0.7:27018" }
]
}
)
docker exec -it shardsvr10 bash
mongo --host 172.17.0.8 --port 27018
rs.initiate(
{
_id: "rs_shardsvr1",
members: [
{ _id : 0, host : "172.17.0.8:27018" },
{ _id : 1, host : "172.17.0.9:27018" },
{ _id : 2, host : "172.17.0.10:27018" }
]
}
)
第四步:建立叢集入口並關聯配置集
ps:預設是mongod(分片處理模式),我們需要將起修改為mongos(路由模式),負責路由和協調操作,使得叢集像一個整體的系統
docker run --name mongos0 -d --entrypoint "mongos" mongo --configdb rs_configsvr/172.17.0.2:27019,172.17.0.3:27019,172.17.0.4:27019 --bind_ip_all
同樣執行 docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(docker ps -aq)
得到容器的名稱和ip的對應列表,並獲得mongo0容器的ip為172.17.0.11
第五步:在叢集入口(路由)上掛載分片集
ps:使用--configdb
構建的路由容器預設的埠是27017
docker exec -it mongos0 bash
mongo --host 172.17.0.11 --port 27017
sh.addShard("rs_shardsvr0/172.17.0.5:27018,172.17.0.6:27018,172.17.0.7:27018")
sh.addShard("rs_shardsvr1/172.17.0.8:27018,172.17.0.9:27018,172.17.0.10:27018")
測試
進入路由容器建立test資料庫並啟用分片
docker exec -it mongos0 bash
mongo --host 172.17.0.11 --port 27017
sh.enableSharding("test")
分片 Collection
對 test.order 的 _id 欄位進行雜湊分片:
sh.shardCollection("test.order", {"_id": "hashed" })
插入10000條資料測試下
mongos> use test
switched to db test
mongos> for (i = 1; i <= 10000; i=i+1){
db.order.insert({'price': 1})
}
WriteResult({ "nInserted" : 1 })
mongos> db.order.find().count()
10000
到分片資料庫看下情況
[email protected]:/# mongo --host 172.17.0.8 --port 27018
rs_shardsvr1:PRIMARY> db.order.count()
4966
rs_shardsvr1:PRIMARY>exit
[email protected]:/# mongo --host 172.17.0.5 --port 27018
rs_shardsvr0:PRIMARY> db.order.count()
5034
可以看到已經得到預期效果
那麼備份呢?(備份預設情況下不允許讀取,需要執行db.getMongo().setSlaveOk()
來允許讀取)
[email protected]:/# mongo --host 172.17.0.6 --port 27018
rs_shardsvr0:SECONDARY> db.getMongo().setSlaveOk()
rs_shardsvr0:SECONDARY> db.order.count()
5034
可以看到備份已經重新寫入
練習過程參考了Jay54520的部落格: 檢視原文