An overview of the

  • This paper mainly introduces how to build Mongo sharding cluster in docker-swarm environment.
  • This article creates clusters in authorization mode, but users cannot be created if a script starts authorization between them. The user needs to be created in unauthorized mode, and then restarted in authorized mode. (The two modes have different startup scripts, but mount the same file directory)

Architecture diagram

  • There are three nodes in total: BreakPad (master server), BPCluster and Bogon

The front steps

  • Install the docker
  • Example Initialize the swarm cluster
    • docker swarm init

Deployment steps

After performing the preceding three steps, the cluster can be used. You do not need to perform the following four steps without authorization

  1. Create a directory
  2. Deployment Service (Unauthorized mode)
  3. Configuring Fragment Information
  4. Generate keyFile and change permissions
  5. Copy keyfile to another node
  6. Adding User Information
  7. Restarting the Service (authorization mode)

1. Create a directory

Run before-deploy.sh on all servers

#! /bin/bashDIR=/data/fates DATA_PATH="${DIR}/mongo" PWD='1qaz2wsx! @#' DATA_DIR_LIST=('config' 'shard1' 'shard2' 'shard3' 'script') function check_directory() { if [ !  -d "${DATA_PATH}" ]; then echo "create directory: ${DATA_PATH}" echo ${PWD} | sudo -S mkdir -p ${DATA_PATH} else echo "directory ${DATA_PATH} already exists." fi cd "${DATA_PATH}" for SUB_DIR in ${DATA_DIR_LIST[@]} do if [ ! -d "${DATA_PATH}/${SUB_DIR}" ]; then echo "create directory: ${DATA_PATH}/${SUB_DIR}" echo ${PWD} | sudo -S mkdir -p "${DATA_PATH}/${SUB_DIR}" else echo "directory: ${DATA_PATH}/${SUB_DIR} already exists." fi done echo ${PWD} | sudo -S chown -R $USER:$USER "${DATA_PATH}" } check_directoryCopy the code

2. Start the Mongo cluster in unauthorized mode

  • This step is not authorized and can be performed without login. It is used to create users

Create fate-Mongo.yaml on the primary server and execute the following script (note that the constraints properties are modified according to your machine name)

docker stack deploy -c fates-mongo.yaml fates-mongo
Copy the code
version: '3.4'
services:
  shard1-server1:
    image: Mongo: 4.0.5
    # --shardsvr: This parameter only changes the default 27017 port to 27018. If --port is specified, this parameter is not needed
    # -- directoryperDB: Use separate folders for each database
    command: mongod --shardsvr --directoryperdb --replSet shard1
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard1:/data/db
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==bpcluster
  shard2-server1:
    image: Mongo: 4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard2
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard2:/data/db
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==bpcluster
  shard3-server1:
    image: Mongo: 4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard3
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard3:/data/db
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==bpcluster
  shard1-server2:
    image: Mongo: 4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard1
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard1:/data/db
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==bogon
  shard2-server2:
    image: Mongo: 4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard2
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard2:/data/db
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==bogon
  shard3-server2:
    image: Mongo: 4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard3
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard3:/data/db
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==bogon
  shard1-server3:
    image: Mongo: 4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard1
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard1:/data/db
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==breakpad
  shard2-server3:
    image: Mongo: 4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard2
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard2:/data/db
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==breakpad
  shard3-server3:
    image: Mongo: 4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard3
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard3:/data/db
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==breakpad
  config1:
    image: Mongo: 4.0.5
    # -- configSvr: This parameter simply changes the default port from 27017 to 27019. If --port is specified, this parameter is optional
    command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/config:/data/configdb
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==bpcluster
  config2:
    image: Mongo: 4.0.5
    command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/config:/data/configdb
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==bogon
  config3:
    image: Mongo: 4.0.5
    command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/config:/data/configdb
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==breakpad
  mongos:
    image: Mongo: 4.0.5
    The default binding IP for # mongo3.6 is 127.0.0.1, where 0.0.0.0 allows access to other containers or hosts
    command: mongos --configdb fates-mongo-config/config1:27019,config2:27019,config3:27019 --bind_ip 0.0. 0. 0 --port 27017
    networks:
      - mongo
    ports:
      - 27017: 27017
    volumes:
      - /etc/localtime:/etc/localtime
    depends_on:
      - config1
      - config2
      - config3
    deploy:
      restart_policy:
        condition: on-failure
      mode: global

networks:
  mongo:
    driver: overlay
    # If an external network has been created, release the following sentence
    # external: true

Copy the code

3. Configure fragment information

#Adding a Configuration Server
docker exec -it $(docker ps | grep "config" | awk '{ print $1 }') bash -c "echo 'rs.initiate({_id: \"fates-mongo-config\",configsvr: true, members: [{ _id : 0, host : \"config1:27019\" },{ _id : 1, host : \"config2:27019\" }, { _id : 2, host : \"config3:27019\" }]})' | mongo --port 27019"

#Adding a Shard Server
docker exec -it $(docker ps | grep "shard1" | awk '{ print $1 }') bash -c "echo 'rs.initiate({_id : \"shard1\", members: [{ _id : 0, host : \"shard1-server1:27018\" },{ _id : 1, host : \"shard1-server2:27018\" },{ _id : 2, host : \"shard1-server3:27018\", arbiterOnly: true }]})' | mongo --port 27018"
docker exec -it $(docker ps | grep "shard2" | awk '{ print $1 }') bash -c "echo 'rs.initiate({_id : \"shard2\", members: [{ _id : 0, host : \"shard2-server1:27018\" },{ _id : 1, host : \"shard2-server2:27018\" },{ _id : 2, host : \"shard3-server3:27018\", arbiterOnly: true }]})' | mongo --port 27018"
docker exec -it $(docker ps | grep "shard3" | awk '{ print $1 }') bash -c "echo 'rs.initiate({_id : \"shard3\", members: [{ _id : 0, host : \"shard3-server1:27018\" },{ _id : 1, host : \"shard2-server2:27018\" },{ _id : 2, host : \"shard3-server3:27018\", arbiterOnly: true }]})' | mongo --port 27018"

#Add a sharding cluster to Mongos
docker exec -it $(docker ps | grep "mongos" | awk '{ print $1 }') bash -c "echo 'sh.addShard(\"shard1/shard1-server1:27018,shard1-server2:27018,shard1-server3:27018\")' | mongo "
docker exec -it $(docker ps | grep "mongos" | awk '{ print $1 }') bash -c "echo 'sh.addShard(\"shard1/shard2-server1:27018,shard2-server2:27018,shard2-server3:27018\")' | mongo "
docker exec -it $(docker ps | grep "mongos" | awk '{ print $1 }') bash -c "echo 'sh.addShard(\"shard1/shard3-server1:27018,shard3-server2:27018,shard3-server3:27018\")' | mongo "
Copy the code

4. Generate a key file

Perform the first three steps, already available to ensure that mongo shard cluster can be used successfully, if you do not need to add authorization, the following steps do not need to see.

Run generate-keyfile.sh on the primary server

#! /bin/bashDATA_PATH=/data/fates/mongo PWD='1qaz2wsx! @#' function check_directory() { if [ ! -d "${DATA_PATH}" ]; then echo "directory: ${DATA_PATH} not exists, please run before-depoly.sh." fi } function generate_keyfile() { cd "${DATA_PATH}/script" if [ !  -f "${DATA_PATH}/script/mongo-keyfile" ]; then echo 'create mongo-keyfile.' openssl rand -base64 756 -out mongo-keyfile echo "${PWD}" | sudo -S chmod 600 mongo-keyfile echo "${PWD}" | sudo -S chown 999 mongo-keyfile else echo 'mongo-keyfile already exists.' fi } check_directory generate_keyfileCopy the code

5. Copy the key file to the script directory of another server

Perform a copy of the keyFile file on the server where it was generated (note the -p parameter to keep the permissions changed earlier)

sudo scp -p /data/fates/mongo/script/mongo-keyfile username@server2:/data/fates/mongo/script
sduo scp -p /data/fates/mongo/script/mongo-keyfile username@server3:/data/fates/mongo/script
Copy the code

6. Add user information

Run the add-user.sh command on the primary server

The user name and password given by the script are both root and have the root permission. It can be customized

docker exec -it $(docker ps | grep "mongos" | awk '{ print $1 }') bash -c "echo -e 'use admin\n db.createUser({user:\"root\",pwd:\"root\",roles:[{role:\"root\",db:\"admin\"}]})' | mongo"
Copy the code

7. Create docker-started YAML script file (license)

  • In this step, you need to enter the user name and password created in the previous step

Create fate-Mongo-key. yaml on master server and restart in authorized mode (script is different, mount path is used before)

docker stack deploy -c fates-mongo-key.yaml fates-mongo
Copy the code
version: '3.4'
services:
  shard1-server1:
    image: Mongo: 4.0.5
    # --shardsvr: This parameter only changes the default 27017 port to 27018. If --port is specified, this parameter is not needed
    # -- directoryperDB: Use separate folders for each database
    command: mongod --shardsvr --directoryperdb --replSet shard1 --keyFile /data/mongo-keyfile
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard1:/data/db
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==bpcluster
  shard2-server1:
    image: Mongo: 4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard2 --keyFile /data/mongo-keyfile
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard2:/data/db
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==bpcluster
  shard3-server1:
    image: Mongo: 4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard3 --keyFile /data/mongo-keyfile
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard3:/data/db
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==bpcluster
  shard1-server2:
    image: Mongo: 4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard1 --keyFile /data/mongo-keyfile
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard1:/data/db
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==bogon
  shard2-server2:
    image: Mongo: 4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard2 --keyFile /data/mongo-keyfile
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard2:/data/db
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==bogon
  shard3-server2:
    image: Mongo: 4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard3 --keyFile /data/mongo-keyfile
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard3:/data/db
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==bogon
  shard1-server3:
    image: Mongo: 4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard1 --keyFile /data/mongo-keyfile
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard1:/data/db
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==breakpad
  shard2-server3:
    image: Mongo: 4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard2 --keyFile /data/mongo-keyfile
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard2:/data/db
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==breakpad
  shard3-server3:
    image: Mongo: 4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard3 --keyFile /data/mongo-keyfile
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard3:/data/db
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==breakpad
  config1:
    image: Mongo: 4.0.5
    # -- configSvr: This parameter simply changes the default port from 27017 to 27019. If --port is specified, this parameter is optional
    command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles --keyFile /data/mongo-keyfile
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/config:/data/configdb
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==bpcluster
  config2:
    image: Mongo: 4.0.5
    command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles --keyFile /data/mongo-keyfile
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/config:/data/configdb
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==bogon
  config3:
    image: Mongo: 4.0.5
    command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles --keyFile /data/mongo-keyfile
    networks:
      - mongo
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/config:/data/configdb
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==breakpad
  mongos:
    image: Mongo: 4.0.5
    The default binding IP for # mongo3.6 is 127.0.0.1, where 0.0.0.0 allows access to other containers or hosts
    command: mongos --configdb fates-mongo-config/config1:27019,config2:27019,config3:27019 --bind_ip 0.0. 0. 0 --port 27017  --keyFile /data/mongo-keyfile
    networks:
      - mongo
    ports:
      - 27017: 27017
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
    depends_on:
      - config1
      - config2
      - config3
    deploy:
      restart_policy:
        condition: on-failure
      mode: global

networks:
  mongo:
    driver: overlay
    # If an external network has been created, release the following sentence
    # external: true
Copy the code

Problems encountered

Boot failure

The configuration file cannot be found because it is not mounted inside the container

Config3 failed to start. Procedure

The mount path in the configuration file was written incorrectly

The container started successfully, but the connection failed and was rejected

Only the script to start the container is executed, and the subsequent configuration is not set (step 3)

Error opening file: /data/mongo-keyfile: Permission denied

  • The mongo-keyfile file must be changed to owner 999 and permission 600

AddShard failure

  • We must wait for Mongos to start before we can execute it
  • Automatically modify the constraints properties in the script based on the server name

Data is stored in only one shard after all shards are completed:

The default value of a shard’s chrunk is 200MB, which is too small to use only one chunk. You can modify this parameter to verify the effect