MongoDB Database Deployment Automation

Agus Syafaat

Organizations are making use of infrastructure in the cloud because it offers speed, flexibility, and scalability. You can imagine if we can spin up a new database instance with just a click, and it takes a couple of minutes until it is ready, we also can deploy the application faster than when compared to on-prem environment.

Unless you are using MongoDB’s own cloud service, the major cloud providers do not offer a managed MongoDB service so it is not really a one-click operation to deploy a single instance or cluster. The common way is to spin up VMs and then deploy them on these. The deployment needs to be taken care of from A to Z - we need to prepare the instance, install the database software, tune some configurations and secure the instance. These tasks are essential, although they are not always properly followed - with potentially disastrous consequences.

Automation plays an important role in making sure all tasks starting from installation, configuration, hardening and until the database service is ready. In this blog, we will discuss deployment automation for MongoDB.

Software Orchestrator

There is a lot of new software tooling to help engineers to deploy and manage their infrastructure. Configuration management helps engineers deploy faster and effectively, reducing deployment time for new services. Popular options include Ansible, Saltstack, Chef, and Puppet. Every product has advantages and disadvantages, but they all work very well and are hugely popular. Deploying a stateful service like a MongoDB ReplicaSet or Sharded Cluster can be a bit more challenging since these are multi-server setups and the tools have poor support for incremental and cross node coordination. Deployment procedures usually call for orchestration across nodes, with tasks carried out in a specific order.

MongoDB Deployment Tasks to Automate

Deployment of a MongoDB server involves a number of things; add MongoDB repository into local, install MongoDB package, configure port, username, and start the service. 

Task: install MongoDB

- name: install mongoDB
  apt: 
    name: mongodb
    state: present
    update_cache: yes

 

Task: copy the mongod.conf from configuration file.

- name: copy config file
  copy:
    src: mongodb.conf
    dest: /etc/mongodb.conf
    owner: root
    group: root
    mode: 0644
  notify:
    - restart mongodb

Task: create MongoDB limit configuration:

- name: create /etc/security/limits.d/mongodb.conf
  copy:
    src: security-mongodb.conf
    dest: /etc/security/limits.d/mongodb.conf
    owner: root
    group: root
    mode: 0644
  notify:
    - restart mongodb

Task: configuring swappiness

- name: config vm.swappiness
  sysctl:
    name: vm.swappiness
    value: '10'
    state: present

Task: configure TCP Keepalive time

- name: config net.ipv4.tcp_keepalive_time
  sysctl:
    name: net.ipv4.tcp_keepalive_time
    value: '120'
    state: present

Task: ensure MongoDB will automatically start

- name: Ensure mongodb is running and and start automatically on reboots
  systemd:
    name: mongodb
    enabled: yes
    state: started

We can combine all of these tasks into a single playbook and run the playbook to automate the deployment. If we run an Ansible playbook from the console:

$ ansible-playbook -b mongoInstall.yml

We will see the progress of deployment from our Ansible script, the output should be something like below:

PLAY [ansible-mongo] **********************************************************

GATHERING FACTS ***************************************************************
ok: [10.10.10.11]

TASK: [install mongoDB] *******************************************************
ok: [10.10.10.11]

TASK: [copy config file] ******************************************************
ok: [10.10.10.11]

TASK: [create /etc/security/limits.d/mongodb.conf]*****************************
ok: [10.10.10.11]


TASK: [config vm.swappiness] **************************************************
ok: [10.10.10.11]

TASK: [config net.ipv4.tcp_keepalive_time]*************************************
ok: [10.10.10.11]

TASK: [config vm.swappiness] **********************************************
ok: [10.10.10.11]

PLAY RECAP ********************************************************************
[10.10.10.11]          : ok=6    changed=1    unreachable=0    failed=0

After the deployment, we can check the MongoDB service on target server.

Deployment Automation of MongoDB using ClusterControl GUI

There are two ways to deploy MongoDB using ClusterControl. We can use it from the dashboard of ClusterControl, it is GUI-based and just needs 2 dialoges until it triggers a new job for new deployment of MongoDB.

First we need to fill the SSH User and password, fill the Cluster Name as shown below:

And then, choose the vendor and version of MongoDB, define the user and password, and the last is fill the target IP Address

Deployment Automation of MongoDB using s9s CLI

From the command line interface, one can use the s9s tools. The deployment of MongoDB using s9s is just a one line command as below:

$ s9s cluster --create --cluster-type=mongodb --nodes="10.10.10.15"  --vendor=percona --provider-version=4.2 --db-admin-passwd="12qwaszx" --os-user=vagrant --cluster-name="MongoDB" --wait
Create Mongo Cluster
/ Job 183 FINISHED   [██████████] 100% Job finished.


So deploying MongoDB, whether it is a ReplicaSet or a Sharded Cluster, is very easy, and is completely automated by ClusterControl.

ClusterControl
The only management system you’ll ever need to take control of your open source database infrastructure.