In this blog post, we will discuss deploying a Percona Server for MongoDB (PSMDB) replica set with Ansible*. Ansible is a popular automation tool that can configure systems, deploy software, and orchestrate more advanced tasks like continuous deployments or zero downtime rolling updates. Its main goals are simplicity and ease of use. By default, it uses the SSH protocol to connect to the server. To know more about it, please go through the documentation.
With Ansible, one can easily configure and deploy the PSMDB replica set (RS). Kindly follow the installation instructions of the Ansible for the respective operating system.
Below is the inventory, in which we have specified the host’s info.
Inventory:
|
1 |
inventory<br><br># Ansible inventory file<br>[rs1]<br>ip-172-31-93-193.ec2.internal mongodb_primary=True<br>ip-172-31-85-203.ec2.internal<br>ip-172-31-80-251.ec2.internal |
Global variable file all defined in group_vars
|
1 |
group_vars/all<br>---<br>##################<br>## General configuration<br>##################<br>packages:<br> - percona-server-mongodb<br><br>#repo version to install<br>repo_version: psmdb-42<br>mongo_root_password: your_secrets<br>rs_port: 27017<br>mongod_log_path: /var/log/mongo<br>mongod_path: /var/lib/mongo<br>#mongo_extra_args: Define argument like --tls --tlsCertificateKeyFile /path/client.pem --tlsCAFile /path/caToValidateServerCertificates.pem<br><br># use_tls: true/false<br>use_tls: false<br><br># only relevant if use_tls: false<br>keyfile_path: /var/lib/mongo<br>keyFile_location: /var/lib/mongo/keyfile<br><br># openssl rand -base64 741 if you want to generate a new one<br>keyfile_content: |<br> 8pYcxvCqoe89kcp33KuTtKVf5MoHGEFjTnudrq5BosvWRoIxLowmdjrmUpVfAivh<br> CHjqM6w0zVBytAxH1lW+7teMYe6eDn2S/O/1YlRRiW57bWU3zjliW3VdguJar5i9<br><br>keyfile_encryption: false<br>encryption_key_content: |<br> vvMTZ3dnSbG7wc6DkPpt+rp3Cc+jF8lJsYlq6QE1yEM=<br># path to the keyfile to encrypt<br>encryption_keyfile: /opt/mongodb.key |
The playbook main.yml for the Deployment of PSMDB RS is below. Here, we are sharing a high-level TASK to deploy the replica set. To get the complete main.yml playbook, please check the GitHub link.
|
1 |
main.yml<br><br>---<br>- name: install percona rpms, Deploy PSMDB RS<br> hosts: all<br> become: yes<br> tasks:<br> - name: install percona key<br> rpm_key:<br> key: https://downloads.percona.com/downloads/RPM-GPG-KEY-percona<br> state: present<br> when: ansible_os_family == "RedHat" <br><br> - name: install percona repo<br> package:<br> name: "https://repo.percona.com/yum/percona-release-latest.noarch.rpm"<br> state: present<br> when: ansible_os_family == "RedHat" <br><br> - name: install deb files from repo<br> become: yes<br> block:<br> - name: download percona repo<br> get_url:<br> url: "https://repo.percona.com/apt/percona-release_latest.focal_all.deb"<br> dest: /home/percona<br> when: ansible_os_family == "Debian"<br><br> - name: install repo<br> apt:<br> deb: /home/percona/percona-release_latest.focal_all.deb<br> when: ansible_os_family == "Debian"<br><br> - name: Update and upgrade apt packages<br> apt:<br> update_cache: yes<br> when: ansible_os_family == "Debian" <br><br> - name: Enable specific version<br> shell: "/usr/bin/percona-release enable {{ repo_version }} && /usr/bin/percona-release enable tools"<br><br> - name: install packages<br> package:<br> name: "{{ item }}"<br> state: present<br> with_items: "{{ packages }}"<br><br>#Deploy the PSMDB Replica set<br><br> - name: copy mongod.conf to rs member<br> become: yes<br> template:<br> src: templates/mongod-replicaset.conf.j2<br> dest: /etc/mongod.conf<br> owner: root<br> group: root<br> mode: 0644<br><br> - name: copy init-rs.js file to initialize the replica set<br> template:<br> src: templates/init-rs.js.j2<br> dest: /tmp/init-rs.js<br> mode: 0644<br> when: mongodb_primary is defined and mongodb_primary<br><br> - name: bootstrap replica sets<br> block:<br> - name: set up keyfile if not using ssl<br> become: yes<br> copy:<br> dest: "{{ keyFile_location }}"<br> content: "{{ keyfile_content }}"<br> owner: mongod<br> group: root<br> mode: 0600<br> when: not use_tls | bool<br><br> - name: start mongod on rs member<br> become: yes<br> service:<br> name: mongod<br> state: restarted<br><br> - name: wait for few secs so servers finish starting<br> pause:<br> seconds: 15<br><br> - name: run the init command for rs <br> shell: mongo {{ mongo_extra_args | default("") }} --port {{ rs_port}} < /tmp/init-rs.js<br> when: mongodb_primary is defined and mongodb_primary<br><br> - name: wait a few secs so replica sets finish initializing<br> pause:<br> seconds: 15<br><br>#Add a root user to the MongoDB<br><br> - name: create a users for RS<br> block:<br> - name: prepare the command to create root user<br> template:<br> src: templates/createRoot.js.j2<br> dest: /tmp/createRoot.js<br> mode: 0644<br> when: mongodb_primary is defined and mongodb_primary<br><br> - name: run the command to create a root user<br> shell: mongo admin {{ mongo_extra_args | default("") }} --port {{ rs_port }} < /tmp/createRoot.js<br> when: mongodb_primary is defined and mongodb_primary<br>... |
Execute the playbook as below:
|
1 |
ansible-playbook -i inventory main.yml |
If the playbook has been executed successfully, you’ll see output like the below (here showing the trimmed output of the playbook i.e the recap of the playbook):
|
1 |
[centos@ip-172-31-17-26 testAnsible]$ ansible-playbook -i inventory main.yml<br>PLAY [install percona rpms, Deploy PSMDB RS] *****************************************************************************<br>.<br>.<br>.<br>PLAY RECAP *****************************************************************************<br><br>ip-172-31-80-251.ec2.internal : ok=11 changed=6 unreachable=0 failed=0 skipped=7 rescued=0 ignored=0<br><br>ip-172-31-85-203.ec2.internal : ok=11 changed=6 unreachable=0 failed=0 skipped=7 rescued=0 ignored=0<br><br>ip-172-31-93-193.ec2.internal : ok=17 changed=10 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0 |
Let’s connect to mongo and verify if PSMDB RS deployment has been set up.
|
1 |
[centos@ip-172-31-93-193 ~]$ mongo -uroot -p<br>Percona Server for MongoDB shell version v4.2.21-21<br>Enter password:<br>connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb<br>Implicit session: session { "id" : UUID("41f13673-edf8-43e6-8b0e-040f08640f26") }<br>Percona Server for MongoDB server version: v4.2.21-21<br>rs1:PRIMARY> rs.conf().members<br>[<br> {<br> "_id" : 1,<br> "host" : "ip-172-31-93-193.ec2.internal:27017",<br> "arbiterOnly" : false,<br> "buildIndexes" : true,<br> "hidden" : false,<br> "priority" : 10,<br> "tags" : {<br><br> },<br> "slaveDelay" : NumberLong(0),<br> "votes" : 1<br> },<br> {<br> "_id" : 2,<br> "host" : "ip-172-31-85-203.ec2.internal:27017",<br> "arbiterOnly" : false,<br> "buildIndexes" : true,<br> "hidden" : false,<br> "priority" : 1,<br> "tags" : {<br><br> },<br> "slaveDelay" : NumberLong(0),<br> "votes" : 1<br> },<br> {<br> "_id" : 3,<br> "host" : "ip-172-31-80-251.ec2.internal:27017",<br> "arbiterOnly" : false,<br> "buildIndexes" : true,<br> "hidden" : false,<br> "priority" : 1,<br> "tags" : {<br><br> },<br> "slaveDelay" : NumberLong(0),<br> "votes" : 1<br> }<br>]<br>rs1:PRIMARY><br> |
From the above, we can see, PSMDB RS has been deployed successfully. Our automation with Ansible to deploy a replica set worked properly.
Ansible is the most popular simple, ease-of-use automation tool and plays a major role in configuring systems, deploying software, and continuous deployment. With Ansible, we have easily deployed a PSMDB RS with ease by defining tasks in a playbook.
We also encourage you to try our products for MongoDB like Percona Server for MongoDB, Percona Backup for MongoDB, and Percona Operator for MongoDB.
*Disclaimer: This blog explains how Ansible can be used to deploy a Percona MongoDB replica set. Ansible is not a Percona product and is not supported by Percona. The information in this blog post is intended for educational purposes only.
Percona Distribution for MongoDB is a freely available MongoDB database alternative, giving you a single solution that combines the best and most important enterprise components from the open source community, designed and tested to work together.
Resources
RELATED POSTS