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} <

使用docker-compose up即可启动集群,使用docker-compose down --volumes即可停止集群。

PS:仅使用docker-compose down可能会致使磁盘空间不断减小,每次挂载的volume在/var/lib/docker目录下会越积越多。

你可能感兴趣的:(dockermongodb)