/ config

Step by step Mongo DB sharded cluster deployment

Mongo db shard cluster deployment

This tutorial explains the step by step process to deploy a sharded MongoDB cluster. Mongo DB version used is 3.2. This tutorial also assumes OS to be of Ubuntu. Concept remains the same for all other operating system. Commands will vary accordingly.

Prerequisite

Basics of Mongo DB sharding

Architecture

mongo-db-arch

Above diagram explains the architecture we will be using while deploying the cluster. Following table explains the servers:

**Machine Type****Components Installed****Description****IP Address****Hostname**
App Server 1Application, MongosThis server will server dual role of app server as well as the mongos server10.10.10.10appserver01
App Server 2Application, MongosThis server will server dual role of app server as well as the mongos server10.10.10.11appserver02
Mongo Config 1Mongo Config ServerUsed as mongodb config server10.10.10.12mongoconfig01
Mongo Config 2Mongo Config ServerUsed as mongodb config server10.10.10.13mongoconfig02
Mongo Config 3Mongo Config ServerUsed as mongodb config server10.10.10.14mongoconfig03
Shard 1 PrimaryMongo DBUsed as primary DB server in shard 110.10.10.15mongosh01db01
Shard 1 SecondaryMongo DBUsed as secondary DB server in shard 110.10.10.16mongosh01db02
Shard 1 SecondaryMongo DBUsed as secondary DB server in shard 110.10.10.17mongosh01db03
Shard 2 PrimaryMongo DBUsed as primary DB server in shard 210.10.10.18mongosh02db01
Shard 2 SecondaryMongo DBUsed as secondary DB server in shard 210.10.10.19mongosh02db02
Shard 2 SecondaryMongo DBUsed as secondary DB server in shard 210.10.10.20mongosh02db03
 

Steps

Login as root user to each server or make sure you have sudo rights. All the commands are without sudo. Please use sudo if you are not a root user.

Step 1: Configure hostname of each server

Configure hostname of each server as per the above table. You can choose a different hostname but make sure that you use same hostnames in all steps. Update the hostnames at following places:

  1. /etc/hostname
  2. /etc/hosts

Step 2: Install mongo DB on all servers

Install mongo DB on all the 11 servers using the steps mentioned in the link below:

https://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

Step 3: Configure Shard 1 replica set

We will start with setting up a replica set of 3 nodes with 1 primary and 2 secondary. We will be naming the replicaSet as “rs0”

Step 3.1: Add host entries for members in replica set

Edit /etc/hosts and add following entries

10.10.10.15 mongosh01db01 10.10.10.16 mongosh01db02 10.10.10.17 mongosh01db03

Step 3.2: Edit configuration file for each node in replica set

Edit /etc/mongod.conf and update following entries:

  • change bindIp to 0.0.0.0
    bindIp: 0.0.0.0
  • *****Add the configuration for replica set **rs0
    **
    ******replication: replSetName: rs0


  • Your overall configuration file should look likestorage: dbPath: /var/lib/mongodb journal: enabled: true systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log net: port: 27017 bindIp: 0.0.0.0 replication: replSetName: rs0

  • ****Restart mongo db service after the changes
    ****sudo service mongod restart

Step 3.3: Configure the replica set

**On any one of the nodes, follow the steps below to configure the replica set. 
**

  • ****Connect to mongo db using command:
    ****mongo

rs.initiate() rs.add("mongosh01db02") rs.add("mongosh01db03")

Check the replica set status using rs.status(). It should list one of the servers as pri****mary while other two as secondary.

Step 4: Configure Shard 2 replica set

Follow the steps listed in Step 3 with following servers with replica set name as rs1

mongosh02db01 mongosh02db02 mongosh02db03

Step 5: Configure Mongo config servers

We need to setup the mongo config servers as replica set too.

Step 5.1: Add host entries for members in replica set

Config servers should know about all the servers in the cluster. Add the host entries for all the servers in /etc/hosts**** file.

10.10.10.10 appserver01 10.10.10.11 appserver02 10.10.10.12 mongoconfig01 10.10.10.13 mongoconfig02 10.10.10.14 mongoconfig03 10.10.10.15 mongosh01db01 10.10.10.16 mongosh01db02 10.10.10.17 mongosh01db03 10.10.10.18 mongosh02db01 10.10.10.19 mongosh02db02 10.10.10.20 mongosh02db03

Step 5.2: Edit configuration file for each node in replica set

Edit /etc/mongod.conf**** and update following entries.

  • change bindIp to 0.0.0.0
    bindIp: 0.0.0.0

  • ****Change port number to 27019
    ****port: 27019

  • ****Add the configuration for replica set with name **configReplSet
    **
    ****replication: replSetName: configReplSet

  • **Configure the server as a config server**sharding: clusterRole: "configsvr"

  • ****Your overall configuration file should look like
    ****storage: dbPath: /var/lib/mongodb journal: enabled: true systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log net: port: 27019 bindIp: 0.0.0.0 replication: replSetName: rs0 sharding: clusterRole: "configsvr"

  • ****Restart mongo db service after the changes
    ****sudo service mongod restart

Step 5.3: Configure the config server replica set

On any one of the config server nodes, follow the steps below to configure the replica set.

  • ****Connect to mongo db using command (note the change in port number):
    ****mongo mongoconfig01:27019
  • ****Add the other config server nodes in the replica set using:
    ****rs.initiate( { _id: "configReplSet", configsvr: true, members: [ { _id: 0, host: "mongoconfig01:27019" }, { _id: 1, host: "mongoconfig02:27019" }, { _id: 2, host: "mongoconfig03:27019" } ] } )

**Check the config replica set status using rs.status(). It should list one of the servers as **primary while other two as secondary.

Step 6: Configure Mongos servers

Follow below steps for both the mongos servers.

Step 6.1: Add host entries for members in replica set

Mongos servers should know about all the servers in the cluster. Add the host entries for all the servers in /etc/hosts**** file.

10.10.10.10 appserver01 10.10.10.11 appserver02 10.10.10.12 mongoconfig01 10.10.10.13 mongoconfig02 10.10.10.14 mongoconfig03 10.10.10.15 mongosh01db01 10.10.10.16 mongosh01db02 10.10.10.17 mongosh01db03 10.10.10.18 mongosh02db01 10.10.10.19 mongosh02db02 10.10.10.20 mongosh02db03

Step 6.2: Edit configuration file for each mongos

Edit /etc/mongod.conf**** and update following entries:

  • change bindIp to 0.0.0.0
    bindIp: 0.0.0.0
  • Remove storage section from the config file
  • ****Add the configuration for **config servers **under **sharding
    ******sharding: configDB:configReplSet/mongoconfig01:27019,mongoconfig02:27019,mongoconfig03:27019
  • ******Your overall configuration file should look like
    ******systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log net: port: 27017 bindIp: 0.0.0.0 sharding: configDB: configReplSet/mongoconfig01:27019,mongoconfig02:27019,mongoconfig03:27019
  • ******Setup mongos as a service
    ******cp /etc/init/mongod.conf /etc/init/mongos.conf edit /etc/init/mongos.conf Update DAEMON=/usr/bin/mongod to DAEMON=/usr/bin/mongos Update if [ -f /etc/default/mongod ]; then . /etc/default/mongod; fi to if [ -f /etc/default/mongos ]; then . /etc/default/mongos; fi
  • ******Restart mongo db service after the changes (note mongos instead of mongod)
    ******sudo service mongos restart

** Step 6.3: Configure the shards**


  • ****Connect to mongos using command:
    ****mongo appserver01:27017
  • ****Add the replica sets as shards. Adding any one of the server in a replica set will add all the servers.
    ****sh.addShard( "rs0/mongosh01db01:27017") sh.addShard( "rs1/mongosh02db01:27017" )

**Check the status using sh.status()****. This should list the shard servers in the cluster. You can also use the shardMap command to get the complete shard map as follows:

use admin; db.runCommand("getShardMap");

 

Cluster setup of mongo DB is now complete. You can connect the app to the mongos query router using localhost:27017. Note that you need to enable sharding for each database and shard the collection you intent to shard. Checkout:

https://docs.mongodb.org/manual/tutorial/deploy-shard-cluster/#deploy-the-config-server-replica-set

 

References

https://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

https://docs.mongodb.org/manual/core/sharding-introduction/

https://docs.mongodb.org/manual/tutorial/deploy-shard-cluster