MongoDB replica set with Docker swarm
MongoDB replica set with Docker swarm

MongoDB replica set with Docker swarm

Created
Nov 17, 2021 05:39 AM
Tags
mongodb
sharding
docker
author
notion image
일단 local.yml 파일
version: "3.7"
x-mongo-common: &mongo-common
  image: mongo:latest
  networks:
    - mongoshard
  entrypoint:
    [
      "/usr/bin/mongod",
      "--bind_ip_all",
      "--replSet",
      "rs0",
      "--journal",
      "--dbpath",
      "/data/db",
    ]
  env_file:
    - ${PWD}/.env
  environment:
    MONGO_REPLICA_SET_NAME: rs0
  deploy:
    mode: replicated
    replicas: 1
    update_config:
      parallelism: 3
services:
  mongo1:
    <<: *mongo-common
    hostname: mongo1
    volumes:
      - mongodb1:/data/db
    ports:
      - 27020:27017
    configs:
      - source: mongo_init
        target: /docker-entrypoint-initdb.d/init-mongo.js
    healthcheck:
      test: echo 'db.runCommand("ping").ok' | mongosh mongo1:27017/test --quiet
      interval: 10s
      start_period: 30s
  mongo2:
    <<: *mongo-common
    hostname: mongo2
    volumes:
      - mongodb2:/data/db
    ports:
      - 27021:27017
    healthcheck:
      test: echo 'db.runCommand("ping").ok' | mongosh mongo2:27017/test --quiet
      interval: 10s
      start_period: 30s
  mongo3:
    <<: *mongo-common
    hostname: mongo3
    volumes:
      - mongodb3:/data/db
    ports:
      - 27022:27017
    healthcheck:
      test: echo 'db.runCommand("ping").ok' | mongosh mongo3:27017/test --quiet
      interval: 10s
      start_period: 30s
configs:
  mongo_init:
    file: mongo/init.js
volumes:
  mongodb1:
  mongodb2:
  mongodb3:
networks:
  mongoshard:
    name: mongoshard
    driver: overlay
 
실행 스크립트 local 을 만들고 chmod +x local
#!/usr/bin/env bash
action=$1
cluster="local"
docker_status="while true; do TEXT=$(docker stats --no-stream $(docker ps --format={{.Names}})); sleep 0.1; clear; echo "$TEXT"; done"

if [ -f .env ]
then
    export $(cat .env | sed "s/#.*//g" | xargs)
fi

printUsage() {
    echo "Usage: $0 [action]"
    echo "[action]:"
    echo "  start   docker stack deploy"
    echo "  stop    docker rm"
    echo "  restart docker stack update"
    echo "  build   radius build using buildx"
    echo ""
}

errorCheck() {
    if [[ $? -ne 0 ]]; then
        exit 1
    fi
}

if [[ -z ${action} ]]; then
    printUsage
    exit 1
fi

architecture=""
arch=$(uname -m)
if [[ $arch == x86_64* ]]; then
    architecture="amd64"
    elif [[ $arch == i*86 ]]; then
    architecture="amd64"
    elif  [[ $arch == arm* ]]; then
    architecture="arm64"
fi

if [ $action == "test" ]; then
    echo $architecture &&
    eval $docker_status
fi

if [ $action == "start" ]; then
    docker stack deploy -c ./.docker/${cluster}-stack.yml --prune --with-registry-auth gn-${cluster} &&
    # bash ${PWD}/scripts/mongo-rs-init.sh gn-${cluster}_mongo1 1
    sleep 5 &&
    docker exec $(docker ps -q -f name="mongo1") mongo mongo1:27017 /docker-entrypoint-initdb.d/init-mongo.js &&
    echo "Done! 🎉" &&
    docker stack services gn-${cluster}
fi

if [ $action == "stop" ]; then
    docker stack rm gn-${cluster} &&
    echo "Stopped docker stack: gn-${cluster}" &&
    echo "Done! 🎉"
    
fi

if [ $action == "build" ]; then
    docker buildx rm multi-builder | true &&
    docker buildx create --use --name multi-builder --platform linux/amd64,linux/arm64 &&
    docker buildx build -o type=docker --platform linux/${architecture} -t danbi-radius:${cluster} .docker/radius &&
    echo "Done! 🎉" &&
    echo "Build complete"
fi

if [ $action == "mongo:check" ]; then
    echo $(netstat -anp tcp | grep 27017) &&
    echo $(netstat -anp udp | grep 27017)
fi

if [ $action == "restart" ]; then
    docker service update gn-${cluster}
fi

if [ $action == "update" ]; then
    docker service update --force gn-${cluster} --with-registry-auth -q
fi
 

mongo-init.js

const endpoints = {
  MONGODB1: "mongo1",
  MONGODB2: "mongo2",
  MONGODB3: "mongo3",
};
const cfg = {
  _id: "rs0",
  protocolVersion: NumberLong(1),
  version: 1,
  members: [
    {
      _id: 0,
      host: `${endpoints.MONGODB1}:27017`,
      priority: 10,
    },
    {
      _id: 1,
      host: `${endpoints.MONGODB2}:27017`,
      priority: 0,
    },
    {
      _id: 2,
      host: `${endpoints.MONGODB3}:27017`,
      arbiterOnly: true,
    },
  ],
};
function init() {
  const error = rs.initiate(cfg);
  printjson(error);
  printjson("Done! 🎉");
}
init();
const config = rs.conf();
printjson(config);
// try {
//   const config = rs.conf();
//   if (config._id === "rs0") {
//     printjson(config);
//   } else {
//     init();
//   }
// } catch (e) {
//   init();
// }
.docker/mongo/init.js