Public Repository

Last pushed: 2 years ago
Short Description
The Config Server for MongoDB Cluster
Full Description

##Goal

Setup a MongoDB Cluster quickly by using docker containers. ^_^

##How-To

Please pull the below images

sudo docker pull openroc/dk-mongos
sudo docker pull openroc/dk-mongo-configsvr
sudo docker pull openroc/dk-mongo-shard

Create volumes, because we use volume mapping for keeping db data

Note:

  1. suppose your volume data in /data01/volumes, and the server environment is Ubuntu Server
  2. suppose to deploy,
    • 3 mongos services
    • 3 configsvr services
    • 3 shard services, like shard1, shard2, shard3

In Console,

# create mongs dir and  their log dir
sudo mkdir -p /data01/volumes/v_dk_mongos_01/log
sudo mkdir -p /data01/volumes/v_dk_mongos_02/log
sudo mkdir -p /data01/volumes/v_dk_mongos_02/log

# create configsvr db dir and their log dir
sudo mkdir -p /data01/volumes/v_dk_mongo_configsvr_01/db
sudo mkdir -p /data01/volumes/v_dk_mongo_configsvr_02/db
sudo mkdir -p /data01/volumes/v_dk_mongo_configsvr_03/db

sudo mkdir -p /data01/volumes/v_dk_mongo_configsvr_01/log
sudo mkdir -p /data01/volumes/v_dk_mongo_configsvr_02/log
sudo mkdir -p /data01/volumes/v_dk_mongo_configsvr_03/log

# create shard1,2,3 db dir and their log dir
sudo mkdir -p /data01/volumes/v_dk_mongo_shard_11/db
sudo mkdir -p /data01/volumes/v_dk_mongo_shard_12/db
sudo mkdir -p /data01/volumes/v_dk_mongo_shard_13/db
sudo mkdir -p /data01/volumes/v_dk_mongo_shard_21/db
sudo mkdir -p /data01/volumes/v_dk_mongo_shard_22/db
sudo mkdir -p /data01/volumes/v_dk_mongo_shard_23/db
sudo mkdir -p /data01/volumes/v_dk_mongo_shard_31/db
sudo mkdir -p /data01/volumes/v_dk_mongo_shard_32/db
sudo mkdir -p /data01/volumes/v_dk_mongo_shard_33/db

sudo mkdir -p /data01/volumes/v_dk_mongo_shard_11/log
sudo mkdir -p /data01/volumes/v_dk_mongo_shard_12/log
sudo mkdir -p /data01/volumes/v_dk_mongo_shard_13/log
sudo mkdir -p /data01/volumes/v_dk_mongo_shard_21/log
sudo mkdir -p /data01/volumes/v_dk_mongo_shard_22/log
sudo mkdir -p /data01/volumes/v_dk_mongo_shard_23/log
sudo mkdir -p /data01/volumes/v_dk_mongo_shard_31/log
sudo mkdir -p /data01/volumes/v_dk_mongo_shard_32/log
sudo mkdir -p /data01/volumes/v_dk_mongo_shard_33/log

Copy the MongoDB configuration file to associated services dir with name "default.conf"

The default.conf of mongos,

net:
    bindIp: 0.0.0.0
    port: 27017

processManagement:
    fork: false
    pidFilePath: /mongo/pid

systemLog:
    destination: file
    path: /mongo/log/mongos.log

The default.conf of configsvr,

net:
    bindIp: 0.0.0.0
    port: 27017

processManagement:
    fork: false
    pidFilePath: /mongo/pid

storage:
    dbPath: /mongo/db
    journal:
        enabled: true

replication:
    oplogSizeMB: 10

sharding:
    clusterRole: configsvr

systemLog:
    destination: file
    path: /mongo/log/shard.log

The default.conf of shard

net:
   bindIp: 0.0.0.0
   port: 27017

processManagement:
   fork: false
   pidFilePath: /mongo/pid

storage:
   dbPath: /mongo/db
   journal:
       enabled: true

replication:
   oplogSizeMB: 10

sharding:
   clusterRole: shardsvr

systemLog:
   destination: file
   path: /mongo/log/shard.log

The script for starting these docker containers as follow,

#!/bin/bash

sudo echo "sudo done"

docker_ip="172.16.42.1"
vpath="/data01/volumes"
configsvrs="${docker_ip}:21001,${docker_ip}:21002,${docker_ip}:21003"

function clean() {
    dst=${1}
    ( cd ${vpath} && find ${dst}* -name "*.lock" | xargs sudo rm )
}

function run_mongos() {
    echo "run mongodb mongos"
    clean v_dk_mongos
    sudo docker run -e CONFIGSVRS=${configsvrs} -v ${vpath}/v_dk_mongos_01:/mongo -p ${docker_ip}:20001:27017 -h mongos_01 -d openroc/dk-mongos
    sudo docker run -e CONFIGSVRS=${configsvrs} -v ${vpath}/v_dk_mongos_02:/mongo -p ${docker_ip}:20002:27017 -h mongos_02 -d openroc/dk-mongos
    sudo docker run -e CONFIGSVRS=${configsvrs} -v ${vpath}/v_dk_mongos_03:/mongo -p ${docker_ip}:20003:27017 -h mongos_03 -d openroc/dk-mongos
}

function run_configsvr() {
    echo "run mongodb configsvr"
    clean v_dk_mongo_configsvr
    sudo docker run -v ${vpath}/v_dk_mongo_configsvr_01:/mongo -p ${docker_ip}:21001:27017 -h configsvr_01 -d openroc/dk-mongo-configsvr
    sudo docker run -v ${vpath}/v_dk_mongo_configsvr_02:/mongo -p ${docker_ip}:21002:27017 -h configsvr_02 -d openroc/dk-mongo-configsvr
    sudo docker run -v ${vpath}/v_dk_mongo_configsvr_03:/mongo -p ${docker_ip}:21003:27017 -h configsvr_03 -d openroc/dk-mongo-configsvr
}

function run_shard1() { 
    echo "run mongodb shard1"
    clean v_dk_mongo_shard_1
    sudo docker run -e SHARDNAME=shard1 -v ${vpath}/v_dk_mongo_shard_11:/mongo -p ${docker_ip}:22011:27017 -h shard_11 -d openroc/dk-mongo-shard
    sudo docker run -e SHARDNAME=shard1 -v ${vpath}/v_dk_mongo_shard_12:/mongo -p ${docker_ip}:22012:27017 -h shard_12 -d openroc/dk-mongo-shard
    sudo docker run -e SHARDNAME=shard1 -v ${vpath}/v_dk_mongo_shard_13:/mongo -p ${docker_ip}:22013:27017 -h shard_13 -d openroc/dk-mongo-shard
} 

function run_shard2() { 
    echo "run mongodb shard2"
    clean v_dk_mongo_shard_2
    sudo docker run -e SHARDNAME=shard2 -v ${vpath}/v_dk_mongo_shard_21:/mongo -p ${docker_ip}:22021:27017 -h shard_21 -d openroc/dk-mongo-shard
    sudo docker run -e SHARDNAME=shard2 -v ${vpath}/v_dk_mongo_shard_22:/mongo -p ${docker_ip}:22022:27017 -h shard_22 -d openroc/dk-mongo-shard
    sudo docker run -e SHARDNAME=shard2 -v ${vpath}/v_dk_mongo_shard_23:/mongo -p ${docker_ip}:22023:27017 -h shard_23 -d openroc/dk-mongo-shard
} 

function run_shard3() { 
    echo "run mongodb shard3"
    clean v_dk_mongo_shard_3
    sudo docker run -e SHARDNAME=shard3 -v ${vpath}/v_dk_mongo_shard_31:/mongo -p ${docker_ip}:22031:27017 -h shard_31 -d openroc/dk-mongo-shard
    sudo docker run -e SHARDNAME=shard3 -v ${vpath}/v_dk_mongo_shard_32:/mongo -p ${docker_ip}:22032:27017 -h shard_32 -d openroc/dk-mongo-shard
    sudo docker run -e SHARDNAME=shard3 -v ${vpath}/v_dk_mongo_shard_33:/mongo -p ${docker_ip}:22033:27017 -h shard_33 -d openroc/dk-mongo-shard
}

function run_shard() {
    run_shard1
    run_shard2
    run_shard3
}

# check args
case $1 in 
    configsvr)
        run_configsvr
        ;;
    shard)
        run_shard
        ;;
    shard1)
        run_shard1
        ;;
    shard2)
        run_shard2
        ;;
    shard3)
        run_shard3
        ;;
    mongos)
        run_mongos
        ;;
    all)
        run_shard
        run_configsvr
        run_mongos
        ;;
    *)
        echo "start-dk-mongo-cluster.sh [configsvr|shard|mongos|all]"
        ;;
esac

For starting all,

#!/bin/bash

./start-dk-mongo-cluster.sh all

####MongoDB Sharding Config

For shard1

#!/bin/bash

mongo 172.16.42.1:22011

# after connected,
> use admin
> config = { _id:"shard1", members:[{_id:0,host:"172.16.42.1:22011"},{_id:1,host:"172.16.42.1:22012"},{_id:2,host:"172.16.42.1:22013",arbiterOnly:true}]}
> rs.initiate(config)

For shard2

#!/bin/bash

mongo 172.16.42.1:22022

# after connected,
> use admin
> config = { _id:"shard2", members:[{_id:0,host:"172.16.42.1:22022"},{_id:1,host:"172.16.42.1:22023"},{_id:3,host:"172.16.42.1:22021",arbiterOnly:true}]}
> rs.initiate(config)

For shard3

#!/bin/bash

mongo 172.16.42.1:22033

# after connected,
> use admin
> config = { _id:"shard3", members:[{_id:0,host:"172.16.42.1:22033"},{_id:1,host:"172.16.42.1:22031"},{_id:3,host:"172.16.42.1:22032",arbiterOnly:true}]}    > rs.initiate(config)

Then, enable these shards in mongos,

#!/bin/bash

mongo 172.16.42.1:20001 # 20002 or 20003 should be ok two.

# after connected,
> use admin
> db.runCommand( { addshard : "shard1/172.16.42.1:22011,172.16.42.1:22012,172.16.42.1:22013"});
> db.runCommand( { addshard : "shard2/172.16.42.1:22021,172.16.42.1:22022,172.16.42.1:22023"});
> db.runCommand( { addshard : "shard3/172.16.42.1:22031,172.16.42.1:22032,172.16.42.1:22033"});

# check the results
> use config
> db.database.find()

If any question, please contact me, openroc(AT)gmail.com.

Docker Pull Command
Owner
openroc