mongo分片及副本集搭建例子

https://github.com/senssei/mongo-cluster-docker

基于上面仓库,改造了一个副本集。
https://github.com/kxtry/mongo-cluster-docker

1.副本集docker-compose脚本

version: '3'
services:
    mgoprimary:
        container_name: "mgoprimary"
        image: mongo:4.2
        ports:
            - "27017:27017"
        command: mongod --replSet vmlocal --port 27017  --oplogSize 16
        links:
            - mgoreplica1:mgoreplica1
            - mgoreplica2:mgoreplica2
        restart: always
        extra_hosts:
            - "mgo.db.com:{{extern_ip}}"
        volumes:
            - /etc/localtime:/etc/localtime:ro
            - /etc/timezone:/etc/timezone:ro
            - ./data/mgoprimary/data:/data/db

    mgoreplica1:
        container_name: "mgoreplica1"
        image: mongo:4.2
        ports:
            - "27018:27017"
        command: mongod --replSet vmlocal --port 27017  --oplogSize 16
        restart: always
        extra_hosts:
            - "mgo.db.com:{{extern_ip}}" 
        volumes:
            - /etc/localtime:/etc/localtime:ro
            - /etc/timezone:/etc/timezone:ro
            - ./data/mgoreplica1/data:/data/db

    mgoreplica2:
        container_name: "mgoreplica2"
        image: mongo:4.2
        ports:
            - "27019:27017"
        command: mongod --replSet vmlocal --port 27017  --oplogSize 16        
        restart: always
        extra_hosts:
            - "mgo.db.com:{{extern_ip}}"
        volumes:
            - /etc/localtime:/etc/localtime:ro
            - /etc/timezone:/etc/timezone:ro
            - ./data/mgoreplica2/data:/data/db

    setup-vmlocal:
        container_name: "setup-vmlocal"
        image: mongo:4.2
        depends_on:
            - "mgoprimary"
            - "mgoreplica1"
            - "mgoreplica2"
        extra_hosts:
            - "mgo.db.com:{{extern_ip}}"
        links:
            - mgoprimary:mgoprimary
            - mgoreplica1:mgoreplica1
            - mgoreplica2:mgoreplica2
        volumes:
            - ./scripts:/scripts
        environment: 
            - MONGO1=mgo.db.com:27017
            - MONGO2=mgo.db.com:27018
            - MONGO3=mgo.db.com:27019
            - RS=vmlocal
        entrypoint: [ "/scripts/setup.sh" ]

3.与docker-compose.yml相同子目录下的./scripts/setup.sh脚本

#!/bin/bash 

mongodb1="${MONGO1}"
mongodb2="${MONGO2}"
mongodb3="${MONGO3}"

echo "Waiting for startup.."
until mongo --host ${mongodb1} --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} <

3.构建脚本build_cluster.sh

#!/bin/sh                                                                                                                                                                                                   
                                                                                                                                                                                                            
path_current=`pwd`                                                                                                                                                                                          
path_script=$(cd "$(dirname "$0")"; pwd)                                                                                                                                                                    
                                                                                                                                                                                                            
mode=$1                                                                                                                                                                                                     
extern_ip=$2                                                                                                                                                                                                
                                                                                                                                                                                                            
case "$mode" in                                                                                                                                                                                             
     'build')                                                                                                                                                                                               
        if [ -f $path_script/docker-compose.yml ]; then                                                                                                                                                     
           echo 'the docker-compose.yml had been exist. if you want to continue, remove it.'                                                                                                                
           exit 1                                                                                                                                                                                           
        fi                                                                                                                                                                                                  
        /bin/cp $path_script/docker-compose.yml.template $path_script/docker-compose.yml && echo "$path_script/docker-compose.yml" | xargs /bin/sed -i "s#{{extern_ip}}#$extern_ip#g"                       
        echo 'success to build the file.'                                                                                                                                                                   
     ;;                                                                                                                                                                                                     
     *)                                                                                                                                                                                                     
       basename=`basename "$0"`                                                                                                                                                                             
       echo "Usage: $basename  {build}{extern_ip}"                                                                                                                                                          
       echo "---$basename build 192.168.10.104"                                                                                                                                                             
     ;;                                                                                                                                                                                                     
esac