Shard Cluster Deployment & Configuration | MongoDB

MongodDB clustering is one of major requirement when it comes to the production critical environments. It can be clustered as several ways including replica cluster, shard cluster, and here we are going to cluster it with shard clustering mechanism. This cluster type has several advance capabilities.

  • Ability to split data sets across multiple data nodes.
  • Ability to easy scale up/dow horizontally when it is required.

In this scenario, we need to provision several servers together with mongoDB installed for configure the shard cluster.

I do use the following servers here for demonstrate the shard cluster installation. You can use as the same here for avoiding some of configuration mistakes. If you are not installed MongoDB use this Install MongoDB link here.

  • Mongo Query Router : mongodb-mongos
  • Configuration Server Primary : mongodb-config-01
  • Configuration Server Secondary : mongodb-config-02
  • Shard – 01 : Primary Node : mongodb-shard-server-01
  • Shard – 01 : Secondary Node : mongodb-shard-server-02
  • Shard – 02 : Primary Node : mongodb-shard-server-03
  • Shard – 02 : Secondary Node : mongodb-shard-server-04

Let’s follow the given steps to come up with a best approach of shard cluster creation.

  1. Log in to the AWS console and create an EC2 instance with MongoDB install, in order to install mongoDB use the following tutorial of Installing MongoDB on AWS Linux.
  2. Once the MongoDB installation done, clone it and relaunch EC2 machines using the cloned image five times in order to create necessary servers for the cluster environment. You can use Amazon AMI to clone the existing machines.
  3. Once you are done with all machine setups, make sure all the EC2 machines are accessible from each other in the cluster. As well as make sure to configure the hostnames in all machines as stated in above in the tutorial.
  4. Make sure those machines are accessible using SSH over the network that you are in. If not check again your security groups attached to the EC2 machines.
  5. Once you are able to log in to the each servers in the cluster, let’s make the change for accept request form any network in the MongoDB.
    1. Log in to each server and check the MongoDB service is running issuing the following command.
      # service mongod status

      Once you see the following output, then you are done, and MongoDB install successfully.
    2. Open the mongodb configuration file using the following command.
      # vim /etc/mongodb.conf
    3. Change the configuration with adding the bindIP as given below. Save and  exit from the configuration file.
      # network interfaces

      net:
       port: 27017
       bindIp: 0.0.0.0
    4. Now restart the mongod service using the following command.
      # service mongod restart

Configuration of MongoDB Config Server Cluster

  1. Log in to the  mongodb-config-01 server and check the MongoDB service is running issuing the following command. Once you see the following output, then you are done MongoDB install successfully.
    # service mongod status

    Mongod_Status
  2. Open the mongodb configuration file using the following command.
    # vim /etc/mongodb.conf
  3. Now we are going to add the replication settings for the config cluster. Add the clusterRole as configsvr. Under replication you need to add replica set name as you want, for the moment I’m using replconfig01.

    sharding:
    clusterRole: configsvr
    replication:
    replSetName:

  4. Repeat the same configuration change on mongodb-config-02 as well. Restart the mongod service on both mongoDB servers.
  5. Now, it’s time to issue the command to cluster the configuration server set. It means, MongoDB config servers are clustered and share the primary, secondary members. Here you can use the same replica set name as defined above.rs.initiate(
    {
    _id: "",
    configsvr: true,
    members: [
    { _id : 0, host : "mongodb-config-01:27017" },
    { _id : 1, host : "mongodb-config-02:27017" }
    ]
    }
    )
  6.  Once the the command executes successfully, you can check the cluster statuses by issuing following commands on both cluster members. Once you check this output, you can see all the information related to replica set. Sample output given below shows the health os the cluster, number of members, Sync source host, etc.
    rs.status()

    rs.status() Output
  7. Try run on both servers and get it verified by using the status command, as well as you can use the following command to check who’s the master in the replica set.
    rs.isMaster()

Configuration of MongoDB Shard Servers

Here, we are using the following shard structure to implement.

  • MONGODB-SHARD-01 & MONGODB-SHARD-02 will be using as a shard cluster with one replica.
  • MONGODB-SHARD-03 will be using as a cluster without replica.
  1. Log in to the  MONGODB-SHARD-01 server and check the MongoDB service is running issuing the following command. Once you see the following output, then you are done MongoDB install successfully.
    # service mongod status

    Mongod_Status
  2. Open the mongodb configuration file using the following command.
    # vim /etc/mongodb.conf
  3. use the following commands to add the shard server configurations. Use shardsvr as the cluster role, and use replSetName as you want.
    sharding:
    clusterRole: shardsvr
    replication:
    replSetName: shardreplica01
  4. Once the above configurations are done, you can restart the mongod service on both mongodb-shard-server-01 & mongodb-shard-server-02 servers.
  5. Do the same configurations changes to the mongodb-shard-server-03 & mongodb-shard-server-04, with a different replSetName. Use the following configuration.
    sharding:
    clusterRole: shardsvr
    replication:
    replSetName: shardreplica02
  6. Once you done with the configuration, restart those mongod services on the servers and let them refreshed.
  7. Log in to the mongoDB server mongodb-shard-server-01 and Initiate the replica with following commands. This execution collects all the mentioning hosts as a shard-01 collection.
    rs.initiate(
    {
    _id : shardreplica01,
    members: [
    { _id : 0, host : "mongodb-shard-server-01:27017" },
    { _id : 1, host : "mongodb-shard-server-02:27017" }
    ]
    }
    )
  8. Log in to the mongoDB server mongodb-shard-server-03 and Initiate the replica with following commands. This execution collects all the mentioning hosts as a shard-02 collection.
    rs.initiate(
    {
    _id : shardreplica02,
    members: [
    { _id : 0, host : "mongodb-shard-server-03:27017" },
    { _id : 1, host : "mongodb-shard-server-04:27017" }
    ]
    }
    )
  9. Once you done with those configurations, you can execute the following command to get the status of replicaset in each shard cluster.
    rs.status()

    Shard-Replica-Status

Now we are done with all the cluster replica set initialization, let’s move ahead with shard cluster creation with MongoDB query router.

Configuration of MongoDB Query Router

Query router is the main component in the cluster as it manages the cluster with pre-defined configurations. Here we use the following configurations given below.

Connecting Config server cluster with Query Router

As we created the config server cluster in this tutorial, it’s time to connect it with the query router. Please carefully follow the below steps to make a success connection.

  1. Stop the existing running mongod process in the mongodb-mongos server. (which is the servers, we use as the query router)
  2. And the start the mongod service using the following configurations.
    sudo mongos --configdb "replconfig01/mongodb-config-01:27017,mongodb-config-02:27017" --bind_ip "0.0.0.0"
  3. In addition to that you can set this command to run at the reboot using a cron job. Add this line to a shell script, and then use it on cron scheduler.
    @reboot /path/to/script
  4. Once you issue the following command you can see the output as all the nodes are connected successfully.
    ex: successfully connected mongodb-config-01:27017
  5. MongoDB config cluster creation is now complete, in the next step we need to add the shard servers with query router.
Connecting Shard servers with Query Router

In this steps we do conenct all the shard servers with mongodb query router, carefully follow the given steps until suceeded with the shard cluster.

  1. Log in to the mongod service in mongodb-mongos server, and issue the following commands sequentially for adding shard-01 & shard-02.
  2. Following command connects mongodb-shard-server-01 & mongodb-shard-server-02 servers together as the shard-01.
    sh.addShard( "shardreplica01/mongodb-shard-server-01:27017")
    sh.addShard( "shardreplica01/mongodb-shard-server-02:27017")
  3. Following command connects mongodb-shard-server-03 & mongodb-shard-server-04 servers together as the shard-02.
    sh.addShard( "shardreplica02/mongodb-shard-server-03:27017")
    sh.addShard( "shardreplica02/mongodb-shard-server-04:27017")
  4. Check the shard cluster status using the following command in order to make sure the cluster is up & running.
    sh.status()
  5. Sample output below clearly confirms shard clusters are configured successfully.

    Cluster-Status

Start Working with Shard Cluster

Let’s create a new DB by enabling shards.

  1. Log in to the Mongos using the following command and create a new DB.
    mongos --host localhost
    use mydb
  2. Enable sharding feature to the created database.
    sh.enableSharding("mydb")
  3. Once you enable the shard on DB, next step is to configure shard key for collections. Shard key is tricky point to get out maximum performance of the cluster. There are two types of shard keys including Ranged shard key and Hashed shard key. In this scenario, issue the following command to enable hashed shard key.
    sh.shardCollection("incentivio-order.merchant", { _id: "hashed"}, false, { numInitialChunks: 5 } )
  4.  Run the following copmmand to get the staus of the current cluster which output gives a comprehensive report of the cluster.
    sh.status()
  5. Following is a sample output that I’m using in my MongoDB cluster.
    Shrd-Status

    In this example, it shows the shard key as “hashed” as well as data chunks. That’s the confirmation of a proper working shard cluster.  

The shard key might be changed due to the use case in your production environment and which directly affects to the cluster performance. In my case I used data chinks using hashed shard key, and which gave me a very high throughput of data read/write in the DB.

This is about my MongoDB shard cluster tutorial, please feel free to add any comments if you have any queries regarding configuration. Thank you everyone, enjoy 🙂

Advertisements

One thought on “Shard Cluster Deployment & Configuration | MongoDB

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s