MongoDB Sharded Cluster

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:

Prerequisite

  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 172.17.0.1 then go ahead else use whichever is assigned.

Step 3: Edit /etc/default/docker

DOCKER_OPTS="--bip=172.17.0.1/16 --dns=172.17.0.1 --dns 8.8.8.8 --dns 8.8.4.4"

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 172.17.0.1:53:53/udp --name skydns crosbymichael/skydns -nameserver 8.8.8.8:53 -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

container-name.image-name.environment.domain-name

default value for envirnment : dev and domain name is docker

for example: domain name for primary shard is rs1-srv1.mongo.dev.docker 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 rs1-srv1.mongo.dev.docker

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

config = { _id: "rs1", members:[{ _id : 0, host : "rs1-srv1.mongo.dev.docker:27017" }]};

rs.initiate(config);

rs.add("rs1-srv2.mongo.dev.docker:27017");
rs.addArb("rs1-arb.mongo.dev.docker:27017");
rs.status();
exit

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 cfg1.mongo.dev.docker:27017

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

docker run -i -t mongo mongo --host mongo-router.mongo.dev.docker

Add the shard server to config db from mongodb router:

sh.addShard("rs1/rs1-srv1.mongo.dev.docker:27017");
sh.status();

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
sh.enableSharding("my_test_db")

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

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

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]
    respawn
    script

                /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

Conclusion

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

References

  1. https://github.com/crosbymichael/skydock
  2. https://hub.docker.com/_/mongo/
  3. https://medium.com/@gargar454/deploy-a-mongodb-cluster-in-steps-9-using-docker-49205e231319
  4. https://medium.com/@gargar454/deploy-a-mongodb-cluster-in-steps-9-using-docker-49205e231319
Advertisements