MongoDB Cluster Creation with High-availability

Highly available MongoDB clusters are deployed by replicating the Mongo databases. Database replication is a mechanism that is increasing the availability of the data by having multiple copies of data in different instances. So the data can be recovered at any point of failure. I will also increase the performance of the application, reliability and provide fault tolerance.

Mongo DB Replication (Primary & Secondary & Arbiter) 

  1. Replication Method (Master,Slave Replication)
  2. Configure Hosts Files 
  3. Setup MongoDB authentication between nodes 
  4. Creating Administrative User in MongoDB 
  5. Configure MongoDB 
  6. Replication and add members in MongoDB 
  7. Test Replication 

1.  Replication Method 

MongoDB replication is configured using private IP, to avoid network latency in the MongoDB replica set cluster. In this replica set three nodes are there (Primary,Secondary,Arbiter). In this replica set, if primary node down in this set another Primary node is elected by the voting process. In this set data replication will happen only in Primary and Secondary nodes. Arbiter is only used for voting process for elect the primary node. In this three-node set the data transfer will be like below mentioned 

2.  Configure Hosts Files 

Each of node in the replica set should has the own host name to identify other nodes in a set. So, add the hostname with the host’s private IP like below mentioned, 

Note: use actual hostname of the node 

3. Setup MongoDB authentication between nodes 

  • Generate key file as follows, 

openssl rand -base64 756 > mongo-keyfile 

  • Perform the following step in all nodes, 

mkdir /opt/mongo-replication 

  • Place the mongo-keyfile in the /opt/mongo-replication directory in all nodes and execute below mentioned commands to provide valid permission for mongo-keyfile 

sudo chmod 400 /opt/mongo-replication/mongo-keyfile 

sudo chown mongod:mongod /opt/mongo-replication/mongo-keyfile 

4. Creating Administrative User in MongoDB 

  • LogIn into mongoDB as follows 

mongo –host <mongo_host > —authenticationDatabase <authenticationDatabase> –port <port> –username <username> –password <password> 

  • Use the following commands to create an admnistrative user in mongoDB 

use admin; 

db.createUser({user: “mongodb-admin”, pwd: “[email protected]”, roles:[{role: “root”, db: “admin”}]}) 

5. Configure MongoDB 

  1. db-node1 
  • Add the following in /etc/mongod.conf 

net: 

  port: 27017 

  bindIp: 0.0.0.0  

security: 

  keyFile: /opt/mongo-replication/mongo-keyfile  

replication: 

  replSetName: mongoreplica 

  1. db-node2 
  • Add the following in /etc/mongod.conf 

net: 

  port: 27017 

  bindIp: 0.0.0.0

security: 

  keyFile: /opt/mongo-replication/mongo-keyfile  

replication: 

  replSetName: mongoreplica 

  1. db-arbiter 
  • Add the following in /etc/mongod.conf 

net: 

  port: 27017 

  bindIp: 0.0.0.0

security: 

  keyFile: /opt/mongo-replication/mongo-keyfile  

replication: 

  replSetName: mongoreplica 

Once Completed this configuration changes, restart mongod in all nodes by as follows, 

systemctl restart mongod 

6. Replication and add members in MongoDB 

  • LogIn to the db-node using admin credentials in which you intend to use as your primary by as follows 

mongo —authenticationDatabase admin –port 27017 –username mongodb-admin –password [email protected] 

  • Initiate replication and add members by as follows 

rs.initiate() 

rs.add(“db-node2”) 

rs.add(“db-arbiter”) 

  • check the configuration of your replica set 

This will display a replica set configuration object with information about each member as well as some metadata about the replica set. 

  • check the status of your replica set by as follows, 

rs.status() 

This shows the state, uptime, and other data about the set. 

7. Test Replication 

  • Insert test data by as follows in primary node, 

use replicatest; 

db.products.insert( { item: “card”, qty: 15 } )

  • Login into the secondary database and execute the following commands 

db.getMongo().setSlaveOk() 

use replicatest; 

db. products .find() 

  • If replication is working, you’ll see a inserted document which was inserted in Master node.
  • db.getMongo().setSlaveOk() command is required to enable read operations in slave host. It should executed everytime when the slave DB node is connected by user for read operations.

Note:

i) For Database Security reasons Default MongoDB Port 27017 should be changed.

ii) In MongoDB configuration file(Ex./etc/mongod.conf) bindIp is set as 0.0.0.0. Because MongoDB configuration in bindIp parameter does not support multiple Bind IP addresses. So the Database nodes are should be protected by Strong Passwords, Iptables, System and network firewalls, Security groups, etc…

Reference: https://docs.mongodb.com/