使用Docker部署MongoDB?Cluster實(shí)踐
環(huán)境準(zhǔn)備
- 四臺(tái)服務(wù)器,分別命名為ServerA、ServerB、ServerC、ServerD
- 2 Shard(1 Primary 1 Secondary 1 Arbiter) Nodes
- 3 Config Nodes
- 4 Router Nodes
docker-compose.yml
ServerA配置文件
version: '2'
services:
configsrv:
image: mongo
command: mongod --keyFile /data/configdb/mongodb-keyfile --oplogSize 1024 --replSet configrs --port 27017 --configsvr --wiredTigerCacheSizeGB 5
volumes:
- /data/configsrv_db:/data/configdb
ports:
- "27018:27017"
restart:
always
container_name:
configsrv
ulimits:
nofile:
soft: 300000
hard: 300000
rs1_node:
image: mongo
command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 10240 --replSet rs1 --directoryperdb --port 27017 --shardsvr
volumes:
- /data/rs1_node_db:/data/db
ports:
- "27019:27017"
restart:
always
container_name:
rs1_node
ulimits:
nofile:
soft: 300000
hard: 300000
router:
image: mongo
command: mongos --keyFile /data/db/mongodb-keyfile --configdb configrs/ServerA:27018,ServerB:27018,ServerC:27018
ports:
- "27017:27017"
volumes:
- /data/router_db:/data/db
restart:
always
container_name:
router
ulimits:
nofile:
soft: 300000
hard: 300000ServerB配置文件
version: '2'
services:
configsrv:
image: mongo
command: mongod --keyFile /data/configdb/mongodb-keyfile --oplogSize 1024 --replSet configrs --port 27017 --configsvr --wiredTigerCacheSizeGB 5
volumes:
- /data/configsrv_db:/data/configdb
ports:
- "27018:27017"
restart:
always
container_name:
configsrv
ulimits:
nofile:
soft: 300000
hard: 300000
rs1_node:
image: mongo
command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 10240 --replSet rs1 --directoryperdb --port 27017 --shardsvr
volumes:
- /data/rs1_node_db:/data/db
ports:
- "27019:27017"
restart:
always
container_name:
rs1_node
ulimits:
nofile:
soft: 300000
hard: 300000
rs2_arbiter:
image: mongo
command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 1024 --replSet rs2 --directoryperdb --port 27017 --shardsvr --wiredTigerCacheSizeGB 1 --nojournal --smallfiles
volumes:
- /data/rs2_arbiter_db:/data/db
ports:
- "27020:27017"
restart:
always
container_name:
rs2_arbiter
ulimits:
nofile:
soft: 300000
hard: 300000
router:
image: mongo
command: mongos --keyFile /data/db/mongodb-keyfile --configdb configrs/ServerA:27018,ServerB:27018,ServerC:27018
ports:
- "27017:27017"
volumes:
- /data/router_db:/data/db
restart:
always
container_name:
router
ulimits:
nofile:
soft: 300000
hard: 300000ServerC配置文件
version: '2'
services:
configsrv:
image: mongo
command: mongod --keyFile /data/configdb/mongodb-keyfile --oplogSize 1024 --replSet configrs --port 27017 --configsvr --wiredTigerCacheSizeGB 5
volumes:
- /data/configsrv_db:/data/configdb
ports:
- "27018:27017"
restart:
always
container_name:
configsrv
ulimits:
nofile:
soft: 300000
hard: 300000
rs2_node:
image: mongo
command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 10240 --replSet rs2 --directoryperdb --port 27017 --shardsvr
volumes:
- /data/rs2_node_db:/data/db
ports:
- "27019:27017"
restart:
always
container_name:
rs2_node
ulimits:
nofile:
soft: 300000
hard: 300000
rs1_arbiter:
image: mongo
command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 1024 --replSet rs1 --directoryperdb --port 27017 --shardsvr --wiredTigerCacheSizeGB 1 --nojournal --smallfiles
volumes:
- /data/rs1_arbiter_db:/data/db
ports:
- "27020:27017"
restart:
always
container_name:
rs1_arbiter
ulimits:
nofile:
soft: 300000
hard: 300000
router:
image: mongo
command: mongos --keyFile /data/db/mongodb-keyfile --configdb configrs/ServerA:27018,ServerB:27018,ServerC:27018
ports:
- "27017:27017"
volumes:
- /data/router_db:/data/db
restart:
always
container_name:
router
ulimits:
nofile:
soft: 300000
hard: 300000
ServerD配置文件
version: '2'
services:
rs2_node:
image: mongo
command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 10240 --replSet rs2 --directoryperdb --port 27017 --shardsvr
volumes:
- /data/rs2_node_db:/data/db
ports:
- "27019:27017"
restart:
always
container_name:
rs2_node
ulimits:
nofile:
soft: 300000
hard: 300000
router:
image: mongo
command: mongos --keyFile /data/db/mongodb-keyfile --configdb configrs/ServerA:27018,ServerB:27018,ServerC:27018
ports:
- "27017:27017"
volumes:
- /data/router_db:/data/db
restart:
always
container_name:
router
ulimits:
nofile:
soft: 300000
hard: 300000啟動(dòng)前準(zhǔn)備工作
- 創(chuàng)建mongodb-keyfile文件
openssl rand -base64 741 > mongodb-keyfile chmod 600 mongodb-keyfile
- 創(chuàng)建宿主機(jī)的volume文件夾
初始化Config節(jié)點(diǎn)
重要:在初始化啟動(dòng)前需要去掉docker-compose.yml配置文件中的--keyFile參數(shù)
啟動(dòng)節(jié)點(diǎn)
在ServerA、ServerB和ServerC三臺(tái)服務(wù)器上運(yùn)行命令:
docker-compose up -d configsrv
初始化
利用mongo連接到ServerA節(jié)點(diǎn),輸入以下命令創(chuàng)建管理用戶:
use admin
db.createUser({user: "mongoUserAdmin", pwd: "123456", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })
db.createUser({user: "mongoRootAdmin", pwd: "123456", roles: [ { role: "root", db: "admin" } ] })初始化ReplicaSet信息
rs.initiate(
{
_id: "configrs",
configsvr: true,
members: [
{ _id : 0, host : "ServerA:27018" },
{ _id : 1, host : "ServerB:27018" },
{ _id : 2, host : "ServerC:27018" }
]
}
)初始化Shard1節(jié)點(diǎn)
重要:在初始化啟動(dòng)前需要去掉docker-compose.yml配置文件中的--keyFile參數(shù)
啟動(dòng)節(jié)點(diǎn)
- 在ServerA和ServerB兩臺(tái)服務(wù)器上運(yùn)行命令:
docker-compose up -d rs1_node - 在ServerC服務(wù)器上運(yùn)行命令:
docker-compose up -d rs1_arbiter
初始化
利用mongo連接到ServerA節(jié)點(diǎn),創(chuàng)建管理用戶
use admin
db.createUser({user: "mongoUserAdmin", pwd: "123456", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })
db.createUser({user: "mongoRootAdmin", pwd: "123456", roles: [ { role: "root", db: "admin" } ] })初始化ReplicaSet信息
rs.initiate(
{
_id : "rs1",
members: [
{ _id : 0, host : "ServerA:27019" },
{ _id : 1, host : "ServerB:27019" }
]
}
)增加Arbiter節(jié)點(diǎn)
rs.addArb("ServerC:27020")查看rs狀態(tài):rs.status()
初始化Shard2節(jié)點(diǎn)
與Shard1節(jié)點(diǎn)雷同,只需要修改對(duì)應(yīng)的服務(wù)器IP
重啟Config和Shard節(jié)點(diǎn)
- 取消
--keyFile參數(shù)的注釋,刪掉上述創(chuàng)建的所有container - 利用
docker-compose再次啟動(dòng)上面所有節(jié)點(diǎn)
啟動(dòng)Router節(jié)點(diǎn)
使用命令docker-compose up -d router在四臺(tái)服務(wù)器上啟動(dòng)路由節(jié)點(diǎn)
配置Cluster
增加Shard節(jié)點(diǎn)
使用mongo連接到任意一臺(tái)服務(wù)器的router節(jié)點(diǎn),然后執(zhí)行以下命令將Shard節(jié)點(diǎn)加入到當(dāng)前Cluster中
use admin
db.auth("<username>","<password>")
sh.addShard("rs1/ServerA:27019")
sh.addShard("rs2/ServerD:27019")啟動(dòng)Sharding
在對(duì)collection進(jìn)行sharding之前一定要先對(duì)數(shù)據(jù)庫(kù)啟動(dòng)sharding
sh.enableSharding("<database>")
sh.shardCollection( "<database>.<collection>", { _id : "hashed" } )參考資料:
官方資料:
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Docker數(shù)據(jù)卷掛載命令volume(-v)與mount的使用總結(jié)
本文主要介紹了Docker數(shù)據(jù)卷掛載命令volume(-v)與mount的使用總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Docker使用nodejs鏡像構(gòu)建express服務(wù)的方法
這篇文章主要介紹了Docker使用nodejs鏡像構(gòu)建express服務(wù),主要包括nodejs容器的啟動(dòng),安裝nodejs第三方依賴模塊及啟動(dòng)nodejs服務(wù)的相關(guān)操作,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
docker安裝RocketMQ的實(shí)現(xiàn)步驟
本文主要介紹了docker安裝RocketMQ的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Docker拉取鏡像失敗解決(connect: connection refused)
最近遇到Docker拉取centos鏡像時(shí)報(bào)錯(cuò),本文主要介紹了Docker拉取鏡像失敗解決(connect: connection refused),具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
docker轉(zhuǎn)移鏡像的實(shí)現(xiàn)步驟
本文主要介紹了docker轉(zhuǎn)移鏡像,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-06-06
詳解Docker掛載本地目錄及實(shí)現(xiàn)文件共享的方法
本篇文章主要介紹了詳解Docker掛載本地目錄及實(shí)現(xiàn)文件共享的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
docker容器在uos-12038.101上啟動(dòng)報(bào)錯(cuò)的解決辦法
這篇文章主要介紹了docker容器在uos-12038.101上啟動(dòng)報(bào)錯(cuò)的問(wèn)題,解決方案是需要在/etc/systemd/system/或者 /usr/lib/systemd/system/2024-03-03
找到docker.service文件,在ExecStart=/usr/bin/dockerd后面添加 --default-ulimit nofile=65536:65536參數(shù),需要的朋友可以參考下
Docker搭建并啟動(dòng)Logstash的實(shí)現(xiàn)方式
這篇文章主要介紹了Docker搭建并啟動(dòng)Logstash的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08

