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
IP1        mongonode1

IP2      mongonode2

IP3       mongonode3
  • Now install python on each node

sudo yum -y update

sudo yum -y install yum-utils

sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm

sudo yum install -y python
  • Install Ansible on your server

    sudo yum install epel-release
    
    sudo yum install -y ansible

    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
## [dbservers]

##

[Master_node]

<Master_node_IP>

[slave_nodes]

<salve_node1_IP>

<slave_node2_IP>

[mongodb:children]

Master_node

slave_nodes
  • 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

ansible -m ping mongodb -u centos

 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

[MongoDB]

name=MongoDB Repository

baseurl=http://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/


gpgcheck=0

enabled=1
Ansible Block:

- name:

copy:

src: /mongodb.repo

dest: /etc/yum.repos.d/mongodb.repo
  • Now we will install mongodb using shell command, or yum block in ansible file

Ansible Block:

- name: Install mongodb

shell: sudo yum install -y mongodb-org
  • 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

Ansible block:

- name: remove mongod.conf file

file:

path: /etc/mongod.conf

state: absent
  • 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

# mongod.conf

# for documentation of all options, see:

# http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.

systemLog:

destination: file

logAppend: true

path: /var/log/mongodb/mongod.log

# Where and how to store data.

storage:

dbPath: /var/lib/mongo

journal:

enabled: true

# engine:

# mmapv1:

# wiredTiger:

# how the process runs

processManagement:

fork: true # fork and run in background

pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile

timeZoneInfo: /usr/share/zoneinfo

# network interfaces

net:

port: 27017

bindIp: 0.0.0.0    # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

#security:

#operationProfiling:

replication:

replSetName: <replicaset_name>

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:
  • Now we will copy this mongod.conf file to /etc/ of 3 nodes

Ansible block:

- name:

copy:

src: /mongod.conf

dest: /etc/mongod.conf
  • 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

rs.initiate( {

_id : <"replication_name">,

members: [

{ _id: 0, host: "mongonode1:27017" },

{ _id: 1, host: "mongonode2:27017" },

{ _id: 2, host: "mongonode3:27017" }

]

})

rs.slaveOk()

cfg = rs.conf()

cfg.members[0].priority = 1

cfg.members[1].priority = 1

cfg.members[2].priority = 1

rs.reconfig(cfg)
Anisble Block :

- hosts: Master_node

remote_user: centos

sudo : true

tasks:

- name:

copy:

src: /master/mongorc.js

dest: /etc/mongorc.js

mode: '0766'
  • Slave node mongorc.js file (this is one more mongorc.js file),

rs.slaveOk()

 

Ansible Block:

- hosts: slave_nodes

remote_user: centos

sudo : true

tasks:

- name:

copy:

src: /slave1/mongorc.js

dest: /etc/mongorc.js

mode: '0766'
  • 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

Ansible Block:

- hosts: all

remote_user: centos

sudo: true

tasks:

- name: executin

shell: sudo systemctl start mongod.service

- name: enabling mongod service

shell: sudo systemctl enable mongod.service
  • 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

---

- hosts: all

remote_user: centos

sudo : true

tasks:

- name:

copy:

src: /mongodb.repo

dest: /etc/yum.repos.d/mongodb.repo

- name: Install mongodb

shell: sudo yum install -y mongodb-org

- name: remove mongod.conf file

file:

path: /etc/mongod.conf

state: absent

- name:

copy:

src: /mongod.conf

dest: /etc/mongod.conf

- hosts: Master_node

remote_user: centos

sudo : true

tasks:

- name:

copy:

src: /master/mongorc.js

dest: /etc/mongorc.js

mode: '0766'

- hosts: slave_nodes

remote_user: centos

sudo : true

tasks:

- name:

copy:

src: /slave1/mongorc.js

dest: /etc/mongorc.js

mode: '0766'

- hosts: all

remote_user: centos

sudo: true

tasks:

- name: starting mongoDB service

shell: sudo systemctl start mongod.service

- name: enabling mongod service

shell: sudo systemctl enable mongod.service
  • check the ansible play book syntax

ansible-playbook <playbook_name_with path> --syntax-check

 

  • Execute the ansible play book

ansible-playbook <play book name.yml>

 

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

© 2019, Techrunnr. All rights reserved.

#1
#2
#3
Questions Answered
Articles Written
Overall Points

0 Comments

Leave a Reply

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.