MongoDB——用docker部署mongo分片-两片-每片三节点副本集

 三台机器,centos7 64,  

192.168.1.20

192.168.1.21

192.168.1.22

docker pull mongo:4.0.24
docker run -d --name mongo27017 -p 27017:27017 mongo:4.0.24


1、分别在三台机器启动并配置config副本集

docker run -d --name mongoconfig \
              -p 26001:27019 \
              -v /root/data/soft/mongo/configdb:/data/configdb \
              -m 1G --memory-swap 1G \
              \mongo:4.0.24 \
              --configsvr \
              --replSet "replconfig" \
              --bind_ip_all \
              --storageEngine wiredTiger \
              --wiredTigerCacheSizeGB 0.6

看状态

docker exec -it mongoconfig mongo 127.0.0.1:27019 -eval "db.stats()"

初始化config副本集(初始化失败的话,防火墙中加入容器网段,因为宿主认为容器网段为非法ip)
进入容器mongo

docker exec -it mongoconfig mongo 192.168.1.20:26001

执行

rs.status()
config = {
_id : "replconfig",
members : [
{_id : 0, host : "192.168.1.20:26001" },
{_id : 1, host : "192.168.1.21:26001" },
{_id : 2, host : "192.168.1.22:26001" }
]
}
rs.initiate(config)

2、mongos入口服务

docker run -d --name mongos \
              -p 27017:27017 \
              --entrypoint "mongos" \
              -m 1G --memory-swap 1G \
              mongo:4.0.24 \
              --configdb  replconfig/192.168.1.20:26001,192.168.1.21:26001,192.168.1.22:26001 \
              --bind_ip_all

 

查看分片状态

docker exec -it mongos mongo 192.168.1.20:27017 -eval "sh.status()"


3、做第一个分片,副本集

docker run -d --name mongoshard1 \
              -p 26002:27018  \
              -v /root/data/soft/mongo/shard1db:/data/db \
              -m 1G --memory-swap 1G \
              mongo:4.0.24 \
              --shardsvr \
              --replSet "replshard1" \
              --bind_ip_all \
              --storageEngine wiredTiger \
              --wiredTigerCacheSizeGB 0.6

docker方式启动一个片,指定内存占用,容器和mongo都指定, 60% docker内容为宜

表示此容器最大占1G内存,禁用swap,数据和索引cache占0.6G内存

进入容器mongo

docker exec -it mongoconfig mongo 192.168.1.20:26002

执行

rs.status()
config = {
_id : "replshard1",
members : [
{_id : 0, host : "192.168.1.20:26002" },
{_id : 1, host : "192.168.1.21:26002" },
{_id : 2, host : "192.168.1.22:26002", arbiterOnly:true} 
]
}
rs.initiate(config)

4、做第二个分片,副本集

docker run -d --name mongoshard2 \
              -p 26003:27018  \
              -v /root/data/soft/mongo/shard2db:/data/db \
              -m 1G --memory-swap 1G \
              mongo:4.0.24 \
              --shardsvr \
              --replSet "replshard2" \
              --bind_ip_all \
              --storageEngine wiredTiger \
              --wiredTigerCacheSizeGB 0.6

进入容器mongo

docker exec -it mongoconfig mongo 192.168.1.20:26003

执行

rs.status()
config = {
_id : "replshard2",
members : [
{_id : 0, host : "192.168.1.20:26003" },
{_id : 1, host : "192.168.1.21:26003" },
{_id : 2, host : "192.168.1.22:26003", arbiterOnly:true}
]
}
rs.initiate(config)

5、做第三个分片,副本集

docker run -d --name mongoshard3 \
              -p 26004:27018  \
              -v /root/data/soft/mongo/shard3db:/data/db \
              -m 1G --memory-swap 1G \
              mongo:4.0.24 \
              --shardsvr \
              --replSet "replshard3" \
              --bind_ip_all \
              --storageEngine wiredTiger \
              --wiredTigerCacheSizeGB 0.6

进入容器mongo

docker exec -it mongoconfig mongo 192.168.1.20:26004

执行

rs.status()
config = {
_id : "replshard3",
members : [
{_id : 0, host : "192.168.1.20:26004" },
{_id : 1, host : "192.168.1.21:26004" },
{_id : 2, host : "192.168.1.22:26004", arbiterOnly:true}
]
}
rs.initiate(config)

6、添加分片

sh.status()
sh.addShard("replshard1/192.168.1.20:26002,192.168.1.21:26002,192.168.1.22:26002")
sh.addShard("replshard2/192.168.1.20:26003,192.168.1.21:26003,192.168.1.22:26003")
sh.addShard("replshard3/192.168.1.20:26004,192.168.1.21:26004,192.168.1.22:26004")


7、把其中一个节点更改为仲裁节点,必须在主节点执行

rs.remove("192.168.1.22:26003")
rs.add({_id : 2, host : "192.168.1.22:26003", arbiterOnly:true})
 

rs.remove("192.168.1.22:26002")
rs.add({_id : 2, host : "192.168.1.22:26002", arbiterOnly:true})

 或者在初始化时,就决定好仲裁节点,加上arbiterOnly:true 

docker的shell操作
docker exec -it mongoshard1 mongo 192.168.1.21:26002 -eval "rs.remove('192.168.1.22:26002')"
docker exec -it mongoshard1 mongo 192.168.1.21:26002 \
-eval "rs.add({_id : 2, host : '192.168.1.22:26002', arbiterOnly:true})" docker exec -it mongoshard1 mongo 192.168.1.21:26002 -eval "rs.status().members"

 

8、docker 修改正在运行的容器 -m 内存大小

docker update -m 1500M --memory-swap 1500M 897a6a09807a
docker update -m 1G --memory-swap 1G mongos
docker update -m 1G --memory-swap 1G mongoconfig
docker update -m 1G --memory-swap 1G mongoshard1
docker update -m 1G --memory-swap 1G mongoshard2
docker update -m 1G --memory-swap 1G mongoshard3
docker stats

-m 表示最大使用内存     --memory-swap表示M + swap

主机层没有开启 swap ,容器运行时无论怎么设置 --memory-swap,都不会使用到 swap,容器最大能使用的内存等于设置的内存限制;

主机层开启了 swap 如下表:

memorymemory-swap效果
M 正数 S 容器最大可用内存为S,其中 ram 为 M,swap 为(S-M),若 S=M 则无可用 swap 资源
M 0 相当于未设置 memory-swap
M -1 容器最大可用内存为 M+主机可用swap
M 未设置 容器最大可用内存为 2M,其中 ram 为 M,swap 为 M

9、重新设置每个节点的 cache_size 

由于每个分片都作了高可用,且数据文件映射到了宿主,

依次停止、删除、用加wiredTigerCacheSizeGB参数的命令重启,即可

以下命令查看状态

docker exec -it mongoshard1 mongo 192.168.1.21:26002 -eval "rs.status().members"
docker exec -it mongoshard1 mongo 192.168.1.22:26002/test \
-eval "db.serverStatus().wiredTiger.cache" | grep max

 关注 bytes read into cache 这个指标

嗯?? 调节 cache 规模不一定非得重启服务,我们可以动态调整:

docker exec -it mongoconfig mongo 192.168.1.22:26001/test \
-eval "db.adminCommand({setParameter:1, wiredTigerEngineRuntimeConfig:'cache_size=600M'})"

cache_size

指定WT存储引擎内部cache的内存用量上限。

需要注意的是,仅作用于WiredTiger cache,而非mongod进程的内存用量上限。MongoDB同时使用WT cache和文件系统cache,往往mongod进程的内存用量高于该值。cache_size相对于物理内存总量不要设置的太满,需要留有一定内存为操作系统所用,否则有OOM潜在风险。

cache_size支持在不停服的情况下动态调整,比如将cache_size设置为80GB,执行如下命令:

db.adminCommand({setParameter: 1, wiredTigerEngineRuntimeConfig: "cache_size=80G"})

 

docker exec -it mongoshard1 mongo 192.168.1.20:26003/admin \
-eval "db.runCommand({setParameter:1, wiredTigerEngineRuntimeConfig:'cache_size=800M'})"

docker exec -it mongoshard1 mongo 192.168.1.20:26002 -eval "db.serverStatus().wiredTiger.cache" | grep max

docker exec -it mongoshard1 mongo 192.168.1.20:26003 -eval "db.serverStatus().wiredTiger.cache" | grep max

docker exec -it mongoshard1 mongo 192.168.1.21:26002 -eval "db.serverStatus().wiredTiger.cache" | grep max

docker exec -it mongoshard1 mongo 192.168.1.21:26003 -eval "db.serverStatus().wiredTiger.cache" | grep max

 

 

 

 

posted @ 2021-05-17 19:08  会飞的斧头  阅读(569)  评论(0编辑  收藏  举报