Month: November 2015

How to setup a MongoDB Sharded cluster using docker with skydns

Posted on Updated on

Problem: To setup MongoDB Sharded cluster on development environment, need to have replica set, config server and Mongo Router on single machine.

Solution: One of the solution to setup MongoDB Sharded cluster on single machine is Docker. Docker MongoDB container are hosted on single host with isolation. The MongoBD sharded cluster using docker container is as shown below:


  1. Ubuntu server 14.04
  2. Install Docker host

Install Skydock and skydns for docker service discovery

Step 1: Check ip address docker0, it is docker networking gateway.

 ifconfig docker0

If docker0 bridge ip address is then go ahead else use whichever is assigned.

Step 3: Edit /etc/default/docker

DOCKER_OPTS="--bip= --dns= --dns --dns"

Step 4: Restart docker service

    sudo service docker restart

Step 5: Start the skydns container to manage the docker service discovery with skydock

docker run  -d -p --name skydns crosbymichael/skydns -nameserver -domain docker

docker run  -d -v /var/run/docker.sock:/docker.sock --name skydock crosbymichael/skydock -ttl 30 -environment dev -s /docker.sock -domain docker -name skydns

MongoDB sharded cluster on docker

Setup the replica set:

Start the replica set 1 primary shard

docker run  --name rs1-srv1 -d mongo mongod --storageEngine wiredTiger --replSet rs1

Start the replica set 1 secondary shard

docker run  --name rs1-srv2 -d mongo mongod --storageEngine wiredTiger --replSet rs1

Start the replica set 1 arbiter

docker run  --name rs1-arb -d mongo mongod --storageEngine wiredTiger --replSet rs1

The docker container domain name register at skydns is


default value for envirnment : dev and domain name is docker

for example: domain name for primary shard is where container name is rs1-srv1 and image name is mongo

Connect to primary shard to initiate replica set using mongodb shell client:

docker run -i -t mongo mongo --host

Then initiate the replica set and add secondary node and arbiter to replica set.

config = { _id: "rs1", members:[{ _id : 0, host : "" }]};



Start the MongoDB config server:

docker run   --name cfg1 -d mongo mongod --configsvr --port 27017 --dbpath /data/db

Start the MongoDB Router :

docker run  -p 27017:27017 --name mongo-router -d mongo mongos --configdb

Connect to MongoDB router to enable the sharding using mongodb shell client:

docker run -i -t mongo mongo --host

Add the shard server to config db from mongodb router:


Now MongoDB sharded cluster is ready. Need to enable sharded database on this cluster.
To give an example, here sharding is enabled on mytestdb and want to balance data distribution across the cluster use Hashed Shard Key which require hashed index.

use my_test_db

db.my_collection.ensureIndex( { _id : "hashed" } )

sh.shardCollection("my_test_db.my_collection", { "_id": "hashed" } )

Start docker container on reboot using the upstart

sudo vi /etc/init/docker-mongo-cluster.conf

then paste the following content:

    description "Docker container"
    author "Vinayak Bhadage"
    start on filesystem and started docker
    stop on runlevel [!2345]

                /usr/bin/docker start skydns
                /bin/sleep 10s
                /usr/bin/docker start skydock
                /bin/sleep 10s
                /usr/bin/docker start rs1-srv1
                /bin/sleep 10s
                /usr/bin/docker start rs1-srv2
                /bin/sleep 10s
                /usr/bin/docker start rs1-arb
                /bin/sleep 10s
                /usr/bin/docker start cfg1
                /bin/sleep 10s
                /usr/bin/docker start -a mongo-router

end script

In this case need to change “rs1-srv1” name of docker container and start the service

sudo service docker-mongo-cluster


Dev environment is ready using MongoDB Sharded cluster. You can use MongoChefGUI client to connect the Mongo Router.