乐趣区

关于docker:docker-搭一个mongodb-shard-cluster

本文依照 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/bash

CFGRS0_NAME=cfgrs0
CFGRS0_REPLICA_1=${CFGRS0_NAME}-1
CFGRS0_REPLICA_2=${CFGRS0_NAME}-2
CFGRS0_REPLICA_3=${CFGRS0_NAME}-3

CFGSVR_PORT=27019

DBRS0_NAME=dbrs0
DBRS0_REPLICA_1=${DBRS0_NAME}-1
DBRS0_REPLICA_2=${DBRS0_NAME}-2
DBRS0_REPLICA_3=${DBRS0_NAME}-3

DBRS1_NAME=dbrs1
DBRS1_REPLICA_1=${DBRS1_NAME}-1
DBRS1_REPLICA_2=${DBRS1_NAME}-2
DBRS1_REPLICA_3=${DBRS1_NAME}-3

DBSVR_PORT=27018

until mongosh --host ${CFGRS0_REPLICA_1} --port ${CFGSVR_PORT} <<EOF
exit
EOF
do
  sleep 5
done
mongosh --host ${CFGRS0_REPLICA_1} --port ${CFGSVR_PORT} <<EOF
rs.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 }
    ]
  }
)
EOF

for rs in 0 1
do
    until mongosh --host `eval echo '$'{DBRS"${rs}"_REPLICA_1}` --port ${DBSVR_PORT} <<EOF
exit
EOF
  do
    sleep 5
  done
mongosh --host `eval echo '$'{DBRS"${rs}"_REPLICA_1}` --port ${DBSVR_PORT} <<EOF
rs.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 }
    ]
  }
)
EOF
done

mongos --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 目录下会越积越多。

退出移动版