docker中运行mongo副本集

1 创建镜像mongo集群配置镜像

新建文件夹mongo/setup,在该文件夹下创建Dockfile和mongo-rs-setup.sh两个文件。内容如下:

mongo-rs-setup.sh

#!/bin/bash
echo "Running mongo-rs-setup.sh"

#set default rs name
if test -z $RS_NAME
    then RS_NAME=rs
fi

#check mongo hosts
if test -z $MONGODB_PRIMARY
    then echo "mongo primary host don't set"
    kill 0
fi
if test -z $MONGODB_SECONDARY
    then echo "mongo secondary host don't set"
    kill 0
fi
if test -z $MONGODB_ARBITER
    then echo "mongo arbiter host don't set"
    kill 0
fi
#set default ports
if test -z $MONGODB_PRIMARY_PORT
    then MONGODB_PRIMARY_PORT=27017
fi
if test -z $MONGODB_SECONDARY_PORT
    then MONGODB_SECONDARY_PORT=27017
fi
if test -z $MONGODB_ARBITER_PORT
    then MONGODB_ARBITER_PORT=27017
fi

MONGODB_PRIMARY_IP=`ping -c 1 $MONGODB_PRIMARY | head -1  | cut -d "(" -f 2 | cut -d ")" -f 1`
MONGODB_SECONDARY_IP=`ping -c 1 $MONGODB_SECONDARY | head -1  | cut -d "(" -f 2 | cut -d ")" -f 1`
MONGODB_ARBITER_IP=`ping -c 1 $MONGODB_ARBITER | head -1  | cut -d "(" -f 2 | cut -d ")" -f 1`

echo "mongo primary ip:$MONGODB_PRIMARY_IP"
echo "mongo secondary ip:$MONGODB_SECONDARY_IP"
echo "mongo arbiter ip:$MONGODB_ARBITER_IP"

echo "Waiting for startup on mongo $MONGODB_PRIMARY.."
until curl http://$MONGODB_PRIMARY:$MONGODB_PRIMARY_PORT/serverStatus\?text\=1 2>&1 | grep uptime | head -1; do
  printf '.'
  sleep 1
done

echo "Waiting for startup on mongo $MONGODB_SECONDARY.."
until curl http://$MONGODB_SECONDARY:$MONGODB_SECONDARY_PORT/serverStatus\?text\=1 2>&1 | grep uptime | head -1; do
  printf '.'
  sleep 1
done

echo "Waiting for startup on mongo $MONGODB_ARBITER.."
until curl http://$MONGODB_ARBITER:$MONGODB_ARBITER_PORT/serverStatus\?text\=1 2>&1 | grep uptime | head -1; do
  printf '.'
  sleep 1
done

echo "Replicas started..."
mongo --host $MONGODB_PRIMARY <"_id": "$RS_NAME",
        "version": 1,
        "members": [
            {
                "_id": 0,
                "host": "$MONGODB_PRIMARY:$MONGODB_PRIMARY_PORT",
                "priority": 2
            },
            {
                "_id": 1,
                "host": "$MONGODB_SECONDARY:$MONGODB_SECONDARY_PORT",
                "priority": 1
            },
            {
                "_id": 2,
                "host": "$MONGODB_ARBITER:$MONGODB_ARBITER_PORT",
                "priority": 1,
                "arbiterOnly" : true
            }
        ]
    };
    try{
        var config = rs.config();
        rs.reconfig(cfg, { force: true });
    }catch(err){
        rs.initiate(cfg);
    }
EOF

Dockerfile

FROM mongo:3.2.10
COPY mongo-rs-setup.sh /
RUN chmod 777 mongo-rs-setup.sh
CMD /mongo-rs-setup.sh

创建mongo集群设置镜像

cd mongo
docker build -t szss/mongo-setup ./setup/

2 使用docker compose运行mongo副本集

docker-compose.yml

primary:
  hostname: primary
  image: mongo:3.2.10
  ports:
    - 27017:27017
  volumes:
    - /mongodb/replset/rs1:/data/db
  command: mongod --dbpath /data/db --replSet rs --oplogSize 128
secondary:
  image: mongo:3.2.10
  ports:
    - 27018:27017
  volumes:
    - /mongodb/replset/rs2:/data/db
  command: mongod --dbpath /data/db --replSet rs --oplogSize 128
  links:
    - primary
arbiter:
  image: mongo:3.2.10
  ports:
    - 27019:27017
  command: mongod --dbpath /data/db --replSet rs --smallfiles --oplogSize 128
  links:
    - primary
startup:
  image: szss/mongo-setup
  environment:
    - RS_NAME=rs
    - MONGODB_PRIMARY=192.168.0.100
    - MONGODB_SECONDARY=192.168.0.100
    - MONGODB_ARBITER=192.168.0.100
    - MONGODB_PRIMARY_PORT=27017
    - MONGODB_SECONDARY_PORT=27018
    - MONGODB_ARBITER_PORT=27019
  links:
    - primary
    - secondary
    - arbiter

在docker-compose.yml文件所在目录运行docker-compose up -d,启动mongo副本集

你可能感兴趣的:(mongodb,docker)