Ansible playbook for MongoDB Replication setup on CentOS78 min read
Hi Techrunnr, this post is regarding mongoDB installation and replica setup with HA using ansible on CentOS7
- Prerequisites:
- 3 servers with CentOS7 operating system
- Deploy 3 servers in 3 different AZ
- Python on 3 nodes
- Ansible on execution server
- Enable Security groups on each server to communicate with each other
- Create 3 servers on different AZ
- Lets say server A <IP1>, server B <IP2>, server C <IP3>, make sure that security groups are enable to allow traffic from each other
- Edit the /etc/hosts file in 3 nodes, add the IP
Now install python on each node
Install Ansible on your server
Note: In AWS centOS servers will have user name “centos”
- Edit the ansible hosts file, We will have one master and 2 slave, so we will make 3 groups of servers called Master_node, slave_node, one parent group
make sure that your ansible server can have permission to login to these nodes (ssh login)
Test the 3 nodes are available to ansible server
So we should get pong output from 3 nodes, it means that ansible is able to communicate with the 3 nodes
- Installation of MongoDB on centOS using Ansible:
Create mongodb.repo file in our server, we will copy this file to /etc/yum.repos.d directory of each server
Now we will install mongodb using shell command, or yum block in ansible file
- MongoDb has installed on 3 nodes, Now we need to configure the mongod.conf file and mongorc.js file, before doing this we will remove the mongod.conf file from 3 node, because we need to enable replication setup in the configuration file
Removing mongod.conf files from 3 nodes
Edit the mongod.conf file according to the replication setup and bind to 0.0.0.0, below shows the mongod.conf file for replication setup
Now we will copy this mongod.conf file to /etc/ of 3 nodes
Configure replication setup, we have 3 nodes, one node will be master and the other 2 will be slaves, mongorc.js file will setup the replication with HA, mongorc.js file provides mongoDB to replication setup and priorities to the servers, here we are giving each node priority as 1, we provide different mongorc.js files to master node, and slave node, so make directory with master and slave,
Master node mongorc.js file, In this file we are initiating the replication setup and priorities of each node, below shows the mongorc.js file for master node, where we initiate the replication setup execute this on this server
Slave node mongorc.js file (this is one more mongorc.js file),
We have Installed mongodb, copied mongod.conf file and mongorc.js files, Now we need to start the mongodb service in each node and we need to enable the mongodb service,
command line: sudo systemctl start mongod
Check list before we start :
Installation block of mongodb
removing mongod.conf file
copied mongod.conf file with replication setup
copied mongorc.js file
starting the mongodb service
Full ansible-playbook look as below
check the ansible play book syntax
Execute the ansible play book
It will execute all the tasks
Loginto each node and connect to mongo shell, it will show you replication setup, try to execute show dbs; command on each mongo shell, insert some queries in master mongo, and test it on slave nodes