副本设置mongo docker-compose

时间:2017-02-12 16:38:49

标签: mongodb docker docker-compose

我尝试使用docker-compose配置mongodb replicaSet,但是当我停止主容器时,似乎它没有传递给辅助容器。

redis:
 image: redis
 ports:
  - "6379:6379"

mongo3:
 hostname: mongo3
 image: mongo
 entrypoint: [ "/usr/bin/mongod", "--replSet", "rs", "--journal","--dbpath","/data/db","--smallfiles", "--rest" ]
 volumes:
  - ./data/mongo3:/data/db
 ports:
  - "27018:27017"
  - "28018:28017"
 restart: always

mongo2:
 hostname: mongo2
 image: mongo
 entrypoint: [ "/usr/bin/mongod", "--replSet", "rs", "--journal","--dbpath","/data/db","--smallfiles", "--rest" ]
 volumes:
  - ./data/mongo2:/data/db
 ports:
  - "27019:27017"
  - "28019:28017"
 restart: always

mongo1:
 hostname: mongo1
 image: mongo
 entrypoint: [ "/usr/bin/mongod", "--replSet", "rs", "--journal","--dbpath","/data/db","--smallfiles", "--rest" ]
 volumes:
  - ./data/mongo1:/data/db
ports:
  - "27017:27017"
  - "28017:28017"
links:
 - mongo2:mongo2
 - mongo3:mongo3
restart: always

web:
 build: .
 ports:
  - "2000:2000"
 volumes:
  - .:/vip
 links:
  - redis
  - mongo1
  - mongo2
  - mongo3

nginx:
 restart: always
 build: ./nginx/
 ports:
  - "80:80"
 links:
  - web:web

mongosetup:
 image: mongo
 links:
  - mongo1:mongo1
  - mongo2:mongo2
  - mongo3:mongo3
 volumes:
  - ./scripts:/scripts
 entrypoint: [ "/scripts/setup.sh" ]

setup.sh:

#!/bin/bash

MONGODB1=`ping -c 1 mongo1 | head -1  | cut -d "(" -f 2 | cut -d ")" -f 1`
MONGODB2=`ping -c 1 mongo2 | head -1  | cut -d "(" -f 2 | cut -d ")" -f 1`
MONGODB3=`ping -c 1 mongo3 | head -1  | cut -d "(" -f 2 | cut -d ")" -f 1`

echo "**********************************************" ${MONGODB1}
echo "Waiting for startup.."
until curl http://${MONGODB1}:28017/serverStatus\?text\=1 2>&1 | grep uptime | head -1; do
  printf '.'
  sleep 1
done

echo curl http://${MONGODB1}:28017/serverStatus\?text\=1 2>&1 | grep uptime | head -1
echo "Started.."


echo SETUP.sh time now: `date +"%T" `
mongo --host ${MONGODB1}:27017 <<EOF
var cfg = {
    "_id": "rs",
    "version": 1,
    "members": [
        {
            "_id": 0,
            "host": "${MONGODB1}:27017",
            "priority": 2
        },
        {
            "_id": 1,
            "host": "${MONGODB2}:27017",
            "priority": 0
        },
        {
            "_id": 2,
            "host": "${MONGODB3}:27017",
            "priority": 0
        }
    ],settings: {chainingAllowed: true}
};
rs.initiate(cfg, { force: true });
rs.reconfig(cfg, { force: true });
rs.slaveOk();
db.getMongo().setReadPref('nearest');
db.getMongo().setSlaveOk(); 
EOF

6 个答案:

答案 0 :(得分:11)

我遇到了类似的问题,并使用以下撰写文件解决了该问题:

version: "3.8"

services:
  mongo1:
    image: mongo:4.2
    container_name: mongo1
    command: ["--replSet", "my-replica-set", "--bind_ip_all", "--port", "30001"]
    volumes:
      - ./data/mongo-1:/data/db
    ports:
      - 30001:30001
    healthcheck:
      test: test $$(echo "rs.initiate({_id:'my-replica-set',members:[{_id:0,host:\"mongo1:30001\"},{_id:1,host:\"mongo2:30002\"},{_id:2,host:\"mongo3:30003\"}]}).ok || rs.status().ok" | mongo --port 30001 --quiet) -eq 1
      interval: 10s
      start_period: 30s

  mongo2:
    image: mongo:4.2
    container_name: mongo2
    command: ["--replSet", "my-replica-set", "--bind_ip_all", "--port", "30002"]
    volumes:
      - ./data/mongo-2:/data/db
    ports:
      - 30002:30002

  mongo3:
    image: mongo:4.2
    container_name: mongo3
    command: ["--replSet", "my-replica-set", "--bind_ip_all", "--port", "30003"]
    volumes:
      - ./data/mongo-3:/data/db
    ports:
      - 30003:30003

在我的 /etc/hosts 文件中包含以下内容:

127.0.0.1       mongo1
127.0.0.1       mongo2
127.0.0.1       mongo3

我在 GitHub 存储库中记录了它,并在此处发布了一篇小博文:

https://github.com/UpSync-Dev/docker-compose-mongo-replica-set

https://www.upsync.dev/2021/02/02/run-mongo-replica-set.html

答案 1 :(得分:5)

对于MongoDB 4.0,您不需要第四个容器即可运行安装脚本。调出3个容器的copysetSet真的很简单:

version: "3"
services:
  mongo1:
    hostname: mongo1
    container_name: localmongo1
    image: mongo:4.0-xenial
    expose:
      - 27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
  mongo2:
    hostname: mongo2
    container_name: localmongo2
    image: mongo:4.0-xenial
    expose:
      - 27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
  mongo3:
    hostname: mongo3
    container_name: localmongo3
    image: mongo:4.0-xenial
    expose:
      - 27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]

此处有更多信息:https://github.com/msound/localmongo/tree/4.0

答案 2 :(得分:3)

我想请你看一下khezen/mongo

您可以使用以下内容在3个节点的docker swarm中部署mongo副本集:

version: '3'

services:

  replica1:
    image: khezen/mongo:slim
    deploy:
      mode: replicated
      replicas: 1
      update_config:
        parallelism: 1
        delay: 10s
      restart_policy:
        condition: on-failure
      palcement:
        node.hostname: node-1
    environment:
      RS_NAME: shard1
      SHARD_SVR: 'y'
      AUTH: 'y'
    volumes:
      - /data/mongo/replica1:/data/db
    networks:
      - mongo_cluster

  replica2:
    image: khezen/mongo:slim
    deploy:
      mode: replicated
      replicas: 1
      update_config:
        parallelism: 1
        delay: 10s
      restart_policy:
        condition: on-failure
      palcement:
        node.hostname: node-2
    environment:
      RS_NAME: shard1
      SHARD_SVR: 'y'
      AUTH: 'y'
    volumes:
      - /data/mongo/replica2:/data/db
    networks:
      - mongo_cluster

  replica3:
    image: khezen/mongo:slim
    deploy:
      mode: replicated
      replicas: 1
      update_config:
        parallelism: 1
        delay: 10s
      restart_policy:
        condition: on-failure
      palcement:
        node.hostname: node-3
    environment:
      RS_NAME: shard1
      SHARD_SVR: 'y'
      MASTER: replica3
      SLAVES: replica1 replica2
      AUTH: 'y'
    volumes:
      - /data/mongo/replica3:/data/db
    networks:
      - mongo_cluster

networks:
  mongo_cluster:
    driver: overlay

免责声明:我是此图片的维护者。

答案 3 :(得分:0)

我设置了要点,并提供了有关如何使用docker-compose文件和猫鼬进行设置的指南。 https://gist.github.com/harveyconnor/518e088bad23a273cae6ba7fc4643549

答案 4 :(得分:0)

我在通过身份验证的独立replica set服务中设置mongodb时遇到了类似的问题,这就是我最终得到的结果。

docker-compose.yml:

version: '3.7'

services:
  
  ...

  db:
    image: mongo
    restart: always
    expose:
      - 27017
    environment:
      MONGO_INITDB_DATABASE: ${DATABASE_NAME}
      MONGO_INITDB_ROOT_USERNAME: ${DATABASE_USER}
      MONGO_INITDB_ROOT_PASSWORD: ${DATABASE_PASSWORD}
      MONGO_REPLICA_SET_NAME: ${MONGO_REPLICA_SET_NAME}
    command: ["--replSet", "${MONGO_REPLICA_SET_NAME}", "--bind_ip_all"]
    healthcheck:
      test: test $$(echo "rs.status().ok" | mongo -u $${MONGO_INITDB_ROOT_USERNAME} -p $${MONGO_INITDB_ROOT_PASSWORD} --quiet) -eq 1
      interval: 10s
      start_period: 30s
    volumes:
      - ./db:/data/db
      - ./scripts/set-credentials.sh:/docker-entrypoint-initdb.d/set-credentials.sh

  replica-setup:
    image: mongo
    restart: on-failure
    networks:
      default:
    volumes:
      - ./scripts/setup-replica.sh:/scripts/setup-replica.sh
    entrypoint: [ "bash", "/scripts/setup-replica.sh" ]
    depends_on:
      - db
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${DATABASE_USER}
      MONGO_INITDB_ROOT_PASSWORD: ${DATABASE_PASSWORD}

./ scripts / setup-replica.sh:

#!/bin/bash

MONGODB1=db

echo "Waiting for MongoDB startup..."
until curl http://${MONGODB1}:27017/serverStatus\?text\=1 2>&1 | grep uptime | head -1; do
  printf '.'
  sleep 1
done

# check if replica set is already initiated
RS_STATUS=$( mongo --quiet --host ${MONGODB1}:27017 -u $MONGO_INITDB_ROOT_USERNAME -p $MONGO_INITDB_ROOT_PASSWORD --eval "rs.status().ok" )
if [[ $RS_STATUS != 1 ]]
then
  echo "[INFO] Replication set config invalid. Reconfiguring now."
  RS_CONFIG_STATUS=$( mongo --quiet --host ${MONGODB1}:27017 -u $MONGO_INITDB_ROOT_USERNAME -p $MONGO_INITDB_ROOT_PASSWORD --eval "rs.status().codeName" )
  if [[ $RS_CONFIG_STATUS == 'InvalidReplicaSetConfig' ]]
  then
    mongo --quiet --host ${MONGODB1}:27017 -u $MONGO_INITDB_ROOT_USERNAME -p $MONGO_INITDB_ROOT_PASSWORD <<EOF
config = rs.config()
config.members[0].host = db # Here is important to set the host name of the db instance
rs.reconfig(config, {force: true})
EOF
  else
    echo "[INFO] MongoDB setup finished. Initiating replicata set."
    mongo --quiet --host ${MONGODB1}:27017 -u $MONGO_INITDB_ROOT_USERNAME -p $MONGO_INITDB_ROOT_PASSWORD --eval "rs.initiate()" > /dev/null
  fi
else
  echo "[INFO] Replication set already initiated."
fi

./ scripts / set-credentials.sh:

#!/bin/bash
set -e

mongo -- "$MONGO_INITDB_DATABASE" <<EOF
    var rootUser = '$MONGO_INITDB_ROOT_USERNAME';
    var rootPassword = '$MONGO_INITDB_ROOT_PASSWORD';
    var admin = db.getSiblingDB('admin');
    admin.auth(rootUser, rootPassword);

    var user = '$MONGO_INITDB_ROOT_USERNAME';
    var password = '$MONGO_INITDB_ROOT_PASSWORD';
    db.createUser({user: user, pwd: password, roles: ["readWrite"]});
EOF

我所取得的成就是:

  • 使用用户名/密码身份验证为默认集合设置mongodb服务
  • 首次运行服务时初始化replica set
  • 存在先前的数据库数据时重新配置replica set成员
  • 通过检查mongodb的状态对replica set服务进行健康检查

答案 5 :(得分:0)

我一直在寻找如何使用一个数据库实例启动 MongoDB 副本集以进行本地开发,最终到了这里。我发现这里的答案太复杂了,所以我想出了以下解决方案:

docker-compose.yml

version: "3"
services: 
  mongo:
    hostname: mongodb
    container_name: mongodb
    image: mongo:latest
    restart: always
    ports:
      - "27017:27017"
    volumes:
      - ./scripts:/docker-entrypoint-initdb.d/
    command: ["--replSet", "rs0", "--bind_ip_all"]

当前目录中有一个名为“scripts”的文件夹,其中有一个名为“init.js”的文件(名称并不重要)。此文件夹作为卷安装到“/docker-entrypoint-initdb.d/”,这是一个特殊文件夹。当MongoDB启动时,该目录下的所有文件都会被执行。文件内容为:

init.js

rs.initiate();