Blog

How to create MongoDB replica set cluster

How to create MongoDB replica set cluster

MongoDB is widely used non-relation, key value paired database. To achieve High availability of your mongo database, you can create cluster of mongo, which is called replica set. In Replica set there are primary server and secondary servers
There will be one primary replica of mongoDB cluster, where read and write operation will be performed.
All the written data will then be replicated to secondary replicas of cluster in realtime

While using MongoDB Replica set of 3 servers, 1 will be primary and other 2 will be secondary. When Primary instance goes down, there will be election among secondary instances and one primary instance will be choose. Default time for this election is 10 seconds, and you can overwrite it if required.

  • For Development environment, you can use 1 server or multiple servers to run multiple replicas. in 1 same server, you can run more than 1 mongodb and use them as replicas
  • For production it is recommended that you use separate server for each replica, which means for replica set of 3 servers, take 3 instances

In this article, we will see step by step guide on how you can setup MongoDB replica set on AWS EC2

MongoDB Replica set Setup – Index

Launch Instances/servers

Installation of MongoDB

Prepare hostnames

Configure MongoDB Configuration files

– Add Password based authentication
– Add Key based authentication

Setup Replication

Initiate Replication

Test

1. Launch instances

  • Launch 3 EC2 instances with Ubuntu 18.04/20.04 AMI
  • Select required instance size (for Demo purpose, you can choose t2.micro)
  • Choose public/private subnet for your instances
  • Make sure you give enough disk space for database (30-40 GB)

2. Install MongoDB

Follow below-mentioned steps to install mongo version 5

To install any other version/latest version, visit this link

sudo apt-get update
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
sudo apt-get install gnupg
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-database hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections

If all of the above command suceed, then go ahead and start mongo server

sudo systemctl start mongod
sudo systemctl status mongod

to enable mongodb to start always on start-up or after reboot, run this command

sudo systemctl enable mongod

3. Prepare hostnames

To create and launch replica sets, you need IP address/hostnames of cluster instances

  • If you set it up using IP Addresses, then if your IP changes, you need to make changes in replica set configuration. So it is recommended to always use hostname to setup replica sets.
  • While using hostnames, if any server changes, you can just change hostname pointing in either provider settings or /etc/hosts file and it will be reflected without changing any configuration in replica sets

You can either setup local hostnames for your IPs or you can globally host it in your Domain provider’s entries.

In this article, we will see both

To setup locally using /etc/hosts, run below-mentioned commands in all the cluster servers

sudo nano /etc/hostname
  • replace ip-xxx-xx-xx-xx with mongo-replicaset-01-poc.identicalcloud.com
sudo nano /etc/hosts
  • add line 127.0.0.1 mongo-replicaset-01-poc.identicalcloud.com
  • also add 3 more entries for all cluster servers
172.12.1.2 mongo-replicaset-01-poc.identicalcloud.com
172.12.2.3 mongo-replicaset-02-poc.identicalcloud.com
172.12.2.4 mongo-replicaset-03-poc.identicalcloud.com

after completing this, reboot server and check if hostname is set properly using this command

hostnamectl

Perform these above operations in all cluster servers

To setup globally, Go to your domain provider and add A Records for hostname and IP Addresses of cluster servers

4. MongoDB Configuration

In this step

  • We need to change bind IP settings so other cluster servers can talk to each other
  • We will also secure our mongoDB with authentication

Bind IP

  • Go to /etc/mongod.config file and edit bind ip settings as following
# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0
  • You need to add IP addresses and hostname of your cluster servers
  • If other instances are going to connect to this mongo directly, add those IPs as well.
  • If you are not sure who all will connect, then make it 0.0.0.0

Save the config

There are 2 types of authentication we need while setting up mongo replica

Authentication

  1. Password based authentication, that’s how clients will connect to mongo cluster
  2. Key based authentication, that’s how replica sets will connect with each other

Password based Authentication

We will create same user in all replica servers which will be admin

sudo mongod 
use admin;

db.createUser(
    {
        user: "USERNAME",
        pwd: "PASSWORD",
        roles: [
            { 
                role: "userAdminAnyDatabase", 
                db: "admin" 
            },
            { 
                role: "readWriteAnyDatabase", 
                db: "admin" 
            },
            { 
                role: "dbAdminAnyDatabase", 
                db: "admin" 
            },
            { 
                role: "clusterAdmin", 
                db: "admin" 
            }
        ]
    }
);

Key based Authentication

  1. Let’s create key file.
    • Key file will be created only in 1 server and same file will be copied to all other servers

Run below-mentioned command in 1st server

cd /etc
sudo mkdir mongodb
sudo cd mongodb
sudo openssl rand -base64 741 > mongodb.key
chmod 600 mongodb.key
cd ..
sudo chown -R mongodb:mongodb mongodb/

In all other servers, use below-mentioned commands

cd /etc
sudo mkdir mongodb
sudo cd mongodb
# create mongodb.key file and copy content from 1st server
chmod 600 mongodb.key
cd ..
sudo chown -R mongodb:mongodb mongodb/

Now, we are ready with both authentication, we will go ahead and change Mongo Configuration file to enable authentication

sudo vi /etc/mongod.conf
security:
    authorization: "enabled"
    keyFile: /etc/mongodb/mongodb.key

please make sure, path of key file is correct

Now, restart mongodb and verify if password based authentication is working

sudo systemctl restart mongod
sudo systemctl status mongod

5. Setup Replication

Now, we will setup configuration file for replication

  • Decide replica set name

edit sudo vi /etc/mongod.conf and add these lines, run these in all 3 servers

replication:
   replSetName: "poc-ic"

where poc-ic is replicaset name, you can choose name as per your use case

  • restart mongo
sudo systemctl restart mongod
sudo systemctl status mongod

6. Initiate Replication

  • Only run these commands in one server
  • Keep your hostnames/IP addresses list ready
rs.initiate( {
   _id : "poc-ic",
   members: [
      { _id: 0, host: "mongo-replicaset-01-poc.identicalcloud.com:27017" },
      { _id: 1, host: "mongo-replicaset-02-poc.identicalcloud.com:27017" },
      { _id: 2, host: "mongo-replicaset-03-poc.identicalcloud.com:27017" }
   ]
})
  • Wait for response { ok: 1 }
  • Now run rs.conf() to get configuration details of replication
  • Run rs.status() to get primary and secondary nodes details

6. Test

Test your replication using command line or MongoDB Compass

use string as following

mongodb://USERNAME:PASSWORD@IP1:27017,IP2:27017,IP3:27017/dbname?authSource=admin&replicaSet=poc0

e.g.

mongodb://USERNAME:PASSWORD@mongo-replicaset-01-poc.identicalcloud.com:27017,mongo-replicaset-02-poc.identicalcloud.com:27017,mongo-replicaset-03-poc.identicalcloud.com:27017/admin?authSource=admin&replicaSet=poc-ic

Done! You have completed MongoDB replication setup successfully.

We hope this article is useful to you. Do drop your views in the comment section below

Drafted On,
22nd January 2022
DevOps @identicalCloud.com

Leave a Comment