本文依照mongodb网站上的领导教程应用docker搭建了一个shard集群,仅供学习参考。
有如下目录构造:
.├── docker-compose.yaml└── shard-setup └── setup.sh
其中,docker-compose.yaml
内容如下:
version: "3.8"services: mongos-1: hostname: mongos-1 container_name: mongos-1 image: mongo:5.0.0 entrypoint: /usr/bin/bash /shard-setup/setup.sh volumes: - ./shard-setup:/shard-setup networks: - mongo-shard-net restart: on-failure depends_on: - cfgrs0-1 - cfgrs0-2 - cfgrs0-3 - dbrs0-1 - dbrs0-2 - dbrs0-3 - dbrs1-1 - dbrs1-2 - dbrs1-3 cfgrs0-1: hostname: cfgrs0-1 container_name: cfgrs0-1 image: mongo:5.0.0 entrypoint: /usr/bin/mongod --configsvr --replSet "cfgrs0" --bind_ip_all networks: - mongo-shard-net restart: on-failure expose: - "27019" cfgrs0-2: hostname: cfgrs0-2 container_name: cfgrs0-2 image: mongo:5.0.0 entrypoint: /usr/bin/mongod --configsvr --replSet "cfgrs0" --bind_ip_all networks: - mongo-shard-net restart: on-failure expose: - "27019" cfgrs0-3: hostname: cfgrs0-3 container_name: cfgrs0-3 image: mongo:5.0.0 entrypoint: /usr/bin/mongod --configsvr --replSet "cfgrs0" --bind_ip_all networks: - mongo-shard-net restart: on-failure expose: - "27019" # 配置dbrs0和dbrs1 dbrs0-1: hostname: dbrs0-1 container_name: dbrs0-1 image: mongo:5.0.0 entrypoint: /usr/bin/mongod --shardsvr --replSet "dbrs0" --bind_ip_all networks: - mongo-shard-net restart: on-failure expose: - "27018" dbrs0-2: hostname: dbrs0-2 container_name: dbrs0-2 image: mongo:5.0.0 entrypoint: /usr/bin/mongod --shardsvr --replSet "dbrs0" --bind_ip_all networks: - mongo-shard-net restart: on-failure expose: - "27018" dbrs0-3: hostname: dbrs0-3 container_name: dbrs0-3 image: mongo:5.0.0 entrypoint: /usr/bin/mongod --shardsvr --replSet "dbrs0" --bind_ip_all networks: - mongo-shard-net restart: on-failure expose: - "27018" dbrs1-1: hostname: dbrs1-1 container_name: dbrs1-1 image: mongo:5.0.0 entrypoint: /usr/bin/mongod --shardsvr --replSet "dbrs1" --bind_ip_all networks: - mongo-shard-net restart: on-failure expose: - "27018" dbrs1-2: hostname: dbrs1-2 container_name: dbrs1-2 image: mongo:5.0.0 entrypoint: /usr/bin/mongod --shardsvr --replSet "dbrs1" --bind_ip_all networks: - mongo-shard-net restart: on-failure expose: - "27018" dbrs1-3: hostname: dbrs1-3 container_name: dbrs1-3 image: mongo:5.0.0 entrypoint: /usr/bin/mongod --shardsvr --replSet "dbrs1" --bind_ip_all networks: - mongo-shard-net restart: on-failure expose: - "27018"networks: mongo-shard-net: name: mongo-shard-net
setup.sh
用于设置config以及shard主从,最初作为mongos运行,内容如下:
#/bin/bashCFGRS0_NAME=cfgrs0CFGRS0_REPLICA_1=${CFGRS0_NAME}-1CFGRS0_REPLICA_2=${CFGRS0_NAME}-2CFGRS0_REPLICA_3=${CFGRS0_NAME}-3CFGSVR_PORT=27019DBRS0_NAME=dbrs0DBRS0_REPLICA_1=${DBRS0_NAME}-1DBRS0_REPLICA_2=${DBRS0_NAME}-2DBRS0_REPLICA_3=${DBRS0_NAME}-3DBRS1_NAME=dbrs1DBRS1_REPLICA_1=${DBRS1_NAME}-1DBRS1_REPLICA_2=${DBRS1_NAME}-2DBRS1_REPLICA_3=${DBRS1_NAME}-3DBSVR_PORT=27018until mongosh --host ${CFGRS0_REPLICA_1} --port ${CFGSVR_PORT} <<EOFexitEOFdo sleep 5donemongosh --host ${CFGRS0_REPLICA_1} --port ${CFGSVR_PORT} <<EOFrs.initiate( { _id: "${CFGRS0_NAME}", configsvr: true, members: [ { _id : 0, host : "${CFGRS0_REPLICA_1}:${CFGSVR_PORT}", priority: 2 }, { _id : 1, host : "${CFGRS0_REPLICA_2}:${CFGSVR_PORT}", priority: 1 }, { _id : 2, host : "${CFGRS0_REPLICA_3}:${CFGSVR_PORT}", priority: 1 } ] })EOFfor rs in 0 1do until mongosh --host `eval echo '$'{DBRS"${rs}"_REPLICA_1}` --port ${DBSVR_PORT} <<EOFexitEOF do sleep 5 donemongosh --host `eval echo '$'{DBRS"${rs}"_REPLICA_1}` --port ${DBSVR_PORT} <<EOFrs.initiate( { _id: "eval echo '$'{DBRS"${rs}"_NAME}", members: [ { _id : 0, host : "`eval echo '$'{DBRS"${rs}"_REPLICA_1}`:${DBSVR_PORT}", priority: 2 }, { _id : 1, host : "`eval echo '$'{DBRS"${rs}"_REPLICA_2}`:${DBSVR_PORT}", priority: 1 }, { _id : 2, host : "`eval echo '$'{DBRS"${rs}"_REPLICA_3}`:${DBSVR_PORT}", priority: 1 } ] })EOFdonemongos --configdb ${CFGRS0_NAME}/${CFGRS0_REPLICA_1}:${CFGSVR_PORT},\${CFGRS0_REPLICA_2}:${CFGSVR_PORT},${CFGRS0_REPLICA_3}:${CFGSVR_PORT} \--bind_ip_all
应用docker-compose up
即可启动集群,应用docker-compose down --volumes
即可进行集群。
PS:仅应用docker-compose down
可能会以致磁盘空间一直减小,每次挂载的volume在/var/lib/docker
目录下会越积越多。