blog
Deployment and Maintenance of MongoDB Using Ansible
Technology changes day-by-day and modern applications need to take serious adjustments in order to fulfill the fast delivery expectations of their organizations. Unfortunately, this makes them more complex, more sophisticated, and harder to maintain.
In terms of database management, the data structures inside of MongoDB change in accordance to application needs over time and it could be quite expensive (or rather risky).
In the long run, we need to have an efficient database configured easily and ensure competent software delivery. Achieving all these in a manual way comes with a number of setbacks such as
- Complicated coordination among team members.
- High chances of task repetition
- Susceptible to a lot of human mistakes and errors
- Uneasy to overcome complexity
- Reduced collaboration and job dissatisfaction
- Time-consuming
- Poor accountability and compliance
The difficulties of database administration are mainly centered on
- Deploying
- Maintaining
- Upgrading which may affect the operational overhead up to 95% reduction.
Achieving this can take a lot of time and manual effort for MongoDB. To ensure success you will need to have a simple system with which you can ensure all the listed setbacks above can be addressed from a single platform in a timely manner, that is to say, somehow an automated system. There are quite a number of options but in this article, we are going to discuss how utilizing Ansible.
What is Ansible
Ansible is simply a universal language that unravels the mystery of how work is done. In other words, it is an IT orchestration engine that automates the deployment of applications, configuration management and orchestrate more advanced IT tasks such as ensuring zero downtime rolling updates and continuous deployments.
Machines can easily be managed in an agent-less manner with a greater focus on security and reliability through the use of a language designed around “auditability” by humans.
While deploying MongoDB may not be that difficult, maintenance, backup, and monitoring become increased factors of concern as time goes by. In addition, it is not that easy when you are new to database management. With Ansible developers can deploy and configure applications with ease, it also allows for swift delivery to any hosting platform.
As Ansible is not part of the database cluster system it can be installed in any remote computer and a configuration made to your database host. Please check the installation guide to know which version is suitable for your operating system.
Ansible, by default, connects to a database host through an SSH protocol.
Ansible Playbooks
Playbooks are templates where Ansible code is written hence direct Ansible itself what to execute in a such like to-do-list manner. They are written in YAML (Yet Another Markup Language) format. Each contains step-by-step operations that are followed by the user on a particular machine which run sequentially. Their structure is constituted of one or more Plays. A Play is basically a code block that maps a set of instructions defined against a particular host.
Commonly Used YAML Tags in Ansible
-
name
This is the tag that defines the name of the Ansible playbook. It is advisable to set a name that precisely defines what it will be doing.
-
hosts
This defines a host group or list of hosts against which the defined tasks are to be run. It is a mandatory tag which tells Ansible on which hosts to run the tasks that have been listed. Since tasks can be performed on multiple machines either same or remote machines one can define a group of hosts entry in this tag.
-
vars
Like any other programming language, you will need variables. With this tag, you can define variables that you will be using in your playbook.
-
tasks
This tag will enable you to list a set of tasks to be executed. Tasks are actually actions one need to perform. A task field defines the name of task which essentially helps text for the user during debugging of the playbook. A piece of code defined as a module is linked internally by each task and any arguments that are to be used within the module are passed through the tasks tag.
A simple playbook structure looks something like this…
---
name: install and configure DB
hosts: testServer
become: yes
vars:
mongoDB_Port : 27017
tasks:
-name: Install the mongodb
yum:
-name: Ensure the installed service is enabled and running
service:
name:
Writing a Simple Playbook to Install and Start MongoDB
-
Enabling Root SSH Access
Some setups of managed nodes may deter you from log in as a root user hence need to define a playbook to resolve this. We will create a playbook enable-root-access.yml that will look like this
--- - hosts: ansible-test remote_user: ubuntu tasks: - name: Enable root login shell: sudo cp ~/.ssh/authorized_keys /root/.ssh/
When you run the command
$ ansible-playbook -i inventory.txt -c ssh enable-root-access.yaml
You should see something like
PLAY [ansible-test] *********************************************************** GATHERING FACTS *************************************************************** TASK: [Enable root login] ***************************************************** PLAY RECAP ********************************************************************
-
Selecting hosts and users in mongodbInstall.yaml
--- - hosts: ansible-test remote_user: root become: yes
-
Adding tasks to be executed
Tasks are executed sequentially, so we need to outline them in a sequential manner i.e.
- apt_key to add repository keys. MongoDB public GPG Key need to be imported first
- name: Import the public key used by the package management system apt_key: keyserver=hkp://keyserver.ubuntu.com:80 id=7F0CEB10 state=present
- Adding MongoDB apt_repository
- name: Add MongoDB repository apt_repository: repo='deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' state=present
- Installing packages and starting mongod, then reload the local package database
- name: install mongodb apt: pkg=mongodb-org state=latest update_cache=yes notify: - start mongodb
- Managing services, using handler to start and restart services
handlers: - name: start mongodb service: name=mongod state=started
- apt_key to add repository keys. MongoDB public GPG Key need to be imported first
The general playbook code should look like this
---
- hosts: ansible-test
remote_user: root
become: yes
tasks:
- name: Import the public key used by the package management system
apt_key: keyserver=hkp://keyserver.ubuntu.com:80 id=7F0CEB10 state=present
- name: Add MongoDB repository
apt_repository: repo='deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' state=present
- name: install mongodb
apt: pkg=mongodb-org state=latest update_cache=yes
notify:
- start mongodb
handlers:
- name: start mongodb
service: name=mongod state=started
We can then run this file with ansible using the command
ansible-playbook -i inventory.txt -c ssh mongodbInstall.yaml
If the playbook has been successfully executed you should see this in your terminal
PLAY [ansible-test] ***********************************************************
GATHERING FACTS ***************************************************************
ok: [12.20.3.105]
ok: [12.20.3.106]
TASK: [Import the public key used by the package management system] ***********
changed: [12.20.3.105]
changed: [12.20.3.106]
TASK: [Add MongoDB repository] ************************************************
changed: [12.20.3.105]
changed: [12.20.3.106]
TASK: [install mongodb] *******************************************************
changed: [12.20.3.105]
changed: [12.20.3.106]
NOTIFIED: [start mongodb] *****************************************************
ok: [12.20.3.106]
ok: [12.20.3.105]
PLAY RECAP ********************************************************************
12.20.3.105 : ok=5 changed=3 unreachable=0 failed=0
12.20.3.106 : ok=5 changed=3 unreachable=0 failed=0
If now you run mongo, you will be directed to mongo shell
MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("07c88442-0352-4b23-8938-fdf6ac66f253") }
MongoDB server version: 4.0.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Conclusion
Ansible is a simple open source IT engine that automates application deployment, service orchestration, and cloud provision.
It works by connecting database nodes and pushing out defines instructions known as modules to them, executes them through SSH by default and then getting rid of them when finished. It doesn’t run any daemons or servers hence can be run from any remote machine. In the next tutorial, we are going to discuss how to maintain a MongoDB replica set in the cloud using Ansible.