9

docker 搭一个mongodb shard cluster

 4 years ago
source link: https://segmentfault.com/a/1190000040405543
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

docker 搭一个mongodb shard cluster

发布于 23 分钟前

本文按照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目录下会越积越多。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK