简体   繁体   English

在 docker-compose 中运行 mongo 副本集

[英]Running mongo replica set in docker-compose

I have a node back-end application which requires MongoDB with replica set.我有一个节点后端应用程序,它需要带有副本集的 MongoDB。 I have created a docker image for my application which simply runs the application;我为我的应用程序创建了一个 docker 映像,它只是运行该应用程序; also created a docker-compose file which runs the MongoDB instance and config its replica set.还创建了一个运行 MongoDB 实例并配置其副本集的docker-compose文件。 here is my docker-compose file:这是我的docker-compose文件:

version: '2'

services:
  mongod1:
    image: mongo
    command: mongod --replSet ${RS} --oplogSize 16 --smallfiles --noprealloc --bind_ip 0.0.0.0
    ports:
      - '27017:27017'

  mongod2:
    image: mongo
    command: mongod --replSet ${RS} --oplogSize 16 --smallfiles --noprealloc --bind_ip 0.0.0.0
    ports:
      - '27018:27017'

  mongod3:
    image: mongo
    command: mongod --replSet ${RS} --oplogSize 16 --smallfiles --noprealloc --bind_ip 0.0.0.0
    ports:
      - '27019:27017'

  mongo-config:
    image: mongo
    depends_on:
      - mongod1
      - mongod2
      - mongod3
    volumes:
      - ./scripts:/scripts
    environment:
      - MONGO1=mongod1
      - MONGO2=mongod2
      - MONGO3=mongod3
      - USER=machine
      - PWD=abc123456
      - RS=rs0
    entrypoint: ['/scripts/setup.sh']

and in the entrypoint I have the bash script like below:在入口点我有如下的 bash 脚本:

#!/bin/bash

mongodb1=$(getent hosts ${MONGO1} | awk '{ print $1 }')
mongodb2=$(getent hosts ${MONGO2} | awk '{ print $1 }')
mongodb3=$(getent hosts ${MONGO3} | awk '{ print $1 }')

port=${PORT:-27017}

echo "Waiting for startup.."
until mongo --host ${mongodb1}:${port} --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)' &>/dev/null; do
  printf '.'
  sleep 1
done

echo "Started.."
echo "setup.sh; time now: $(date +"%T")"

mongo --host ${mongodb1}:${port} <<EOF
    use admin
    db.createUser(
        {
            user: ${USER},
            pwd: #{pwd},
            roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
        }
    )
EOF

mongo --host ${mongodb1}:${port} <<EOF
   var cfg = {
        "_id": "${RS}",
        "members": [
            {
                "_id": 0,
                "host": "${mongodb1}:${port}"
            },
            {
                "_id": 1,
                "host": "${mongodb2}:${port}"
            },
            {
                "_id": 2,
                "host": "${mongodb3}:${port}"
            }
        ]
    };
    rs.initiate(cfg, { force: true });
    rs.config();
EOF

mongo --host ${mongodb2}:${port} <<EOF
    db.setSlaveOk()
EOF

mongo --host ${mongodb3}:${port} <<EOF
    db.setSlaveOk()
EOF

now im my envirement i can connect to mongodb in localhost:27017 but application cannot connect to 0.0.0.0:27017 .现在我的环境可以连接到localhost:27017 mongodb 但应用程序无法连接到0.0.0.0:27017 how I can fix this?我该如何解决这个问题? should i use docker networks?我应该使用docker网络吗?

try to change 0.0.0.0 to 127.0.0.1 or just use localhost.尝试将 0.0.0.0 更改为 127.0.0.1 或仅使用本地主机。

BTW, I cannot create auth from your method by keeping getting Error: couldn't add user: not master , so I searched around, and found out that before rs.initiate() has been called, and if a mongod.conf with a setting of replication.replSetName: rs existed, the mongo instance you are connecting to will have no idea who is master or Primary.顺便说一句,我无法通过不断获取Error: couldn't add user: not master从您的方法创建身份验证,所以我四处搜索,发现在调用 rs.initiate() 之前,如果mongod.conf带有replication.replSetName: rs设置replication.replSetName: rs存在,您正在连接的 mongo 实例将不知道谁是 master 或 Primary。 so I switched the order of createUser and rs.initiate() , and put a delay, then it works fine.所以我切换了createUserrs.initiate()的顺序,并延迟了它,然后它就可以正常工作了。

Thanks for the skeleton.谢谢你的骨架。

version: "3.3"

services:
  mongo-admin:
    image: mongo-express
    container_name: "mongo-admin"
    environment:
      - ME_CONFIG_MONGODB_SERVER=db_manager,db_worker_1,db_worker_2
      - ME_CONFIG_OPTIONS_EDITORTHEME=ambiance
      - ME_CONFIG_BASICAUTH_USERNAME=$MONGOADMIN
      - ME_CONFIG_BASICAUTH_PASSWORD=$MONGOPASS
    ports:
      - 8081:8081
    restart: on-failure
    depends_on:
      - db_manager
      - db_worker_1
      - db_worker_2
      - mongo-set-setup

  db_manager:
    image: mongo:4.0
    container_name: db_manager
    volumes:
      - mongodb1:/data/db
      - ./mongod.conf:/etc/mongod.conf
    command: mongod --config /etc/mongod.conf
    ports:
      - "27017:27017"
    restart: always

  db_worker_1:
    image: mongo:4.0
    container_name: db_worker_1
    volumes:
      - mongodb2:/data/db
      - ./mongod.conf:/etc/mongod.conf
    command: mongod --config /etc/mongod.conf
    ports:
      - "27018:27017"
    restart: always

  db_worker_2:
    image: mongo:4.0
    container_name: db_worker_2
    volumes:
      - mongodb3:/data/db
      - ./mongod.conf:/etc/mongod.conf
    command: mongod --config /etc/mongod.conf
    ports:
      - "27019:27017"
    restart: always

  mongo-set-setup:
    container_name: "mongo-set-setup"
    image: mongo:4.0
    depends_on:
      - db_manager
      - db_worker_1
      - db_worker_2
    volumes:
      - ./setupmongo.sh:/scripts/setupmongo.sh
    environment:
      - MONGO1=db_manager
      - MONGO2=db_worker_1
      - MONGO3=db_worker_2
      - USER=$MONGOUSER
      - PASSWORD=$MONGOPASSWORD
      - RS=rs
    entrypoint: ["/scripts/setupmongo.sh"]

#
volumes:
  mongodb1:
  mongodb2:
  mongodb3:

and shell和壳

#!/bin/bash
mongodb1=`getent hosts ${MONGO1} | awk '{ print $1 }'`
mongodb2=`getent hosts ${MONGO2} | awk '{ print $1 }'`
mongodb3=`getent hosts ${MONGO3} | awk '{ print $1 }'`

port=${PORT:-27017}
echo "Waiting for startup.."
until mongo --host ${mongodb1}:${port} --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)' &>/dev/null; do
    printf '.'
    sleep 1
done
echo "Started.."

echo ~~~~~~~~setup replic time now: `date +"%T" `
echo ${mongodb1},  ${mongodb2},  ${mongodb3}
mongo --host ${mongodb1}:${port} <<EOF
    var cfg = {
        "_id": "${RS}",
        "protocolVersion": 1,
        "members": [
            {
                "_id": 0,
                "host": "${mongodb1}:${port}",
                "priority": 2
            },
            {
                "_id": 1,
                "host": "${mongodb2}:${port}",
                "priority": 0
            },
            {
                "_id": 2,
                "host": "${mongodb3}:${port}",
                "priority": 0
            }
        ]
    };
    rs.initiate(cfg, { force: true });
    rs.reconfig(cfg, { force: true });
    rs.slaveOk();
    db.getMongo().setReadPref('nearest');
    db.getMongo().setSlaveOk();
EOF
sleep 30

echo ${USER}, ${PASSWORD}
echo ~~~~~~~~~~setup user auth time now: `date +"%T" `
mongo --host ${mongodb1}:${port} <<EOF
    use admin
    db.createUser(
        {
            "user": "${USER}",
            "pwd": "${PASSWORD}",
            "roles": [ "readWrite"]
        }
    )
EOF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM