great place to work

How to Effectively Configure MongoDB Clusters using Replica Set

Share

MongoDB Clusters

MongoDB is a cross-platform document-oriented database program which has emerged as one of the most popular databases for modern applications. Built for optimal performance, MongoDB provides multi-cloud data distribution, fully automated infrastructure provisioning and sophisticated security controls.

In this article, we will explain how we configured MongoDB clusters using Replica Set, based on our project experience. Before we get started with the implementation steps, let’s quickly understand some of the terms used in the article. In the context of MongoDB, “cluster” is the word usually used for either a replica set or a shared cluster. A Replica Set is the replication of a group of MongoDB servers that hold copies of the same data.

Let’s get started with MongoDB Cluster configuration using Replica Set with authentication and create one primary and two secondary servers.  This will provide high availability, failover, security and improved performance. Here we are performing the configuration in a single node with multiple containers in AWS.

This implementation includes a simple three node replica MongoDB setup based on the official Mongo Docker image using the docker-compose file

Replica Set Architecture

In Replica Set configuration, we will create one primary server, which we used only for the write operation and two secondary servers, which will use the application only for read purpose.

Replica Set Architecture

MongoDB Server Architecture

We configured a single EC2 instance with multiple docker containers to configure the MongoDB cluster using Replica Set.

MongoDB Server Architecture

MongoDB Cluster Configuration Steps

Before we begin the configuration, we need to ensure the following items have been configured properly.

  • In AWS and Azure Cloud platform we need to add two additional network interfaces for the same VM instance – total three network interfaces required.
  • After adding the interfaces, we need to assign DNS names to the respective Public IPs.
  • The following steps are only required in AWS EC2 instances ( not required in Azure). In AWS we have to run the following commands to enable network connection for the other two public IPs:

o    sudo ip addr add <private IP> dev <Interface>

o   Verify the private IPs are listed using the command ip addr.

          Example:  sudo ip addr add 10.0.51.5/20 dev eth0

Steps for MongoDB Replica Set Configuration with Authentication

Step 1: Login to the MongoDB VM instance using SSH with your assigned credentials.

Step 2:  Create the following folder.

mkdir -p /home/ubuntu/docker/container

 Step 3: Go to the created folder path and run the following commands and create the data directories to store DB data.

cd /home/ubuntu/docker/container

mkdir -p mongo/data{1..3}/db          

Step 4: Create the docker compose file named docker-compose.yaml and add the service configurations in that file.

 Step 5: Go to the Mongo path and start the replica cluster container. This will successfully login in to the Mongo shell.

 cd /home/ubuntu/docker/container/mongo

 docker-compose up -d                   

Step 6: Verify the containers running properly and connect using password.

              docker ps

             docker-compose exec mongo1 mongo -u dbadmin -p BkvFxU6Mp%v#

             docker-compose exec mongo2 mongo –port 27018 -u dbadmin -p BkvFxU6Mp%v#

             docker-compose exec mongo3 mongo –port 27019 -u dbadmin -p BkvFxU6Mp%v#                         

Step 7: Verify the same thing externally, using any Mongo client like NoSQLBooster – it’s a graphical tool that helps us to connect with MongoDB.

Step 8: Login to Mongo1 and initialize replica set test-rs0 by running the following command and after that you will get an ok 1 message. This is an example dns name assigned

 rs.initiate({“_id” : “test-rs0″,”members” : [{“_id” : 0,”host” : “mytestdatabase1.example.com:27017”},{“_id” : 1,”host” : “mytestdatabase2.example.com:27018”},{“_id” : 2,”host” : “mytestdatabase3.example.com:27019”}]}); 

Step 9: Verify the replica set status.

             docker-compose exec mongo1 mongo -u dbadmin -p BkvFxU6Mp%v#

             rs.status()

Step 10: If primary server role is assigned with other nodes for ex: id 1 or id 2, then run the following command after login to the primary node – in my case primary node was id 2 (Mongo3).               

             docker-compose exec mongo3 mongo –port 27019 -u dbadmin -p BkvFxU6Mp%v#   

             cfg = rs.conf()

             cfg.members[0].priority = 3

             cfg.members[1].priority = 0.5

             cfg.members[2].priority = 0.5

             rs.reconfig(cfg)              

Step 11: Check the replica set using NoSQLBooster using the following URI and this will give you an Ok connection success message.

mongodb://dbadmin:BkvFxU6Mp%v#@mytestdatabase1.example.com:27017,

mytestdatabase2.example.com:27018,mytestdatabase3.example.com:27019/?replicaSet=test-rs0&authSource=admin

Conclusion

We have just seen how MongoDB achieves replication by the use of Replica Set. With Replication, you can ensure that the same data is available on more than one MongoDB Server. This is sometimes required for the purpose of increasing data availability. Many members (MongoDB instances) can be added to the Replica set depending on the requirements. Replication has several advantages that include high availability, increased read performance, greater scalability and improved data durability guarantee. 

Hope this article proves to be useful and is able to help you configure MongoDB Clusters using Replica Set in an effective way.

By,
Sreejith G
Lead Cloud Engineer, RapidValue

Related article: MongoDB and DocumentDB – The Functional Differences, Advantages and Comparison

Please Share Your Thoughts & Comments Below.

How can we help you?