How To Automate PostgreSQL and repmgr on Vagrant
How To Automate PostgreSQL and repmgr on Vagrant
In this tutorial, I build a fault-tolerant PostgreSQL cluster using Vagrant and Ansible. This configuration may help minimize deployment issues.
Join the DZone community and get the full member experience.Join For Free
I often get asked if it's possible to build a resilient system with PostgreSQL.
Considering that resilience should feature cluster high-availability, fault tolerance, and self-healing, it's not an easy answer. But there is a lot to be said about this.
As of today, we can't achieve that level of resilience with the same ease as MongoDB built-in features. But let's see what we can in fact do with the help of repmgr and some other tooling.
At the end of this exercise, we will have achieved some things that come in handy, such as:
- a few Ansible roles that can be reused for production
- a Vagrantfile for single-command cluster deployment
- a development environment that’s more realistic; being close to production state is good to foresee "production-exclusive issues"
- build a local development environment PostgreSQL cluster with fault tolerance capabilities;
- develop configuration management code to reuse in production.
Note: An alternative to installing Ansible on your host machine would be using the
ansible-local Vagrant provider, which needs Ansible installed on the generated virtual machine instead.
1. Write a Vagrantfile
You can use
vagrant init to generate the file or simply create it and insert our first blocks.
Let's go block by block:
- the 1st block is where we set up the Vagrant version;
- on the 2nd block, we iterate the following code so we reuse it to generate 3 equal VMs;
- OS, hostname and network settings are set in the 3rd block;
- the 4th block contains VirtualBox specific settings.
You can create the servers with:
2. Add a provisioner
Just by doing the first step alone, we can already launch 3 working virtual machines. A little exciting, but the best is yet to come.
Launching virtual machines is a nice feature of Vagrant, but we want these servers to have PostgreSQL and repmgr configured, so we will use configuration management software to help us. This is the moment Ansible walks in to amaze us.
Vagrant supports several providers, two of them being Ansible and Ansible Local. The difference between them is where Ansible runs, or in other words, where it must be installed. By Vagrant terms, the Ansible provider runs on a host machine (your computer) and the Ansible Local provider runs on guest machines (virtual machines). As we already installed Ansible in the prerequisites section, we'll go with the first option.
Let's add a block for this provisioner in our
Ansible allows us to configure several servers simultaneously. To take advantage of this feature on Vagrant, we add
ansible.limit = "all" and must wait until all 3 VMs are up. Vagrant knows they are all created because of the condition
if n == 3, which makes Ansible only run after Vagrant iterated 3 times.
ansible.playbook is the configuration entry point and
ansible.host_vars contains the Ansible host variables to be used on the tasks and templates we are about to create.
3. Create an organized Ansible folder structure
If you're already familiar with Ansible, there's little to learn in this section. For those who aren't, it doesn't get too complicated.
First, we have a folder for all Ansible files, named
provisioning. Inside this folder, we have our aforementioned entry point
group_vars folder for Ansible group variables, and a
We could have all Ansible tasks within
playbook.yaml, but role folder structure helps with organization. You can read the Ansible documentation to learn the best practices. Below, you will find the folder structure for this tutorial.
4. Ansible roles
4.1 PostgreSQL role
repmgr on PostgreSQL, we need to edit two well-known PostgreSQL configuration files:
pg_hba.conf. We will then write our tasks to apply the configurations on
tasks/main.yaml. I named the PostgreSQL role folder as
postgres_12 but you can easily use another version if you want to.
You can reuse the default file which comes with PostgreSQL installation and add the following lines to whitelist
repmgr database sessions from your trusted VMs. Create an Ansible template file (Jinja2 format) like so:
In the same fashion as
pg_hba.conf, you can reuse the
postgresql.conf default file and add a few more replication related settings to the bottom of the file:
The tasks below will install PostgreSQL and apply our configurations. Their names are self-explanatory.
4.2 SSH server configuration
Generate a key pair to use throughout our virtual machines to allow access to them. If you don't know how to do it, this link can help. Just make sure the keys file paths match the paths in the next step.
The tasks below will install the OpenSSH server and apply our configurations. Their names are self-explanatory.
4.3 repmgr installation
We configure settings like promote command, follow command, timeouts and retry count on failure scenarios inside
repmgr.conf. We will copy this file to its default directory
/etc to avoid passing the
-f argument on the
repmgr command all the time.
The tasks below will install
repmgr and apply our configurations. Their names are self-explanatory.
4.4 repmgr node registration
Finally, we reach the moment where fault tolerance is established.
This role was built according to the
repmgr documentation and it might be the most complex role, as it needs to:
- run some commands as root and others as Postgres;
- stop services between reconfigurations;
- have different tasks for primary, standby, and support witness role configuration (in case you want node3 to also be a standby node, just assign
role: standbyin Vagrantfile
5. Set Group Variables
Create a file
group_vars/all.yaml to set your VMs IP addresses and the PostgreSQL version you would like to use. Like
host_vars set on
Vagrantfile, these variables will be placed in the templates placeholders.
6. Put All Pieces Together With a Playbook
The only thing missing is the playbook itself. Create a file named
playbook.yaml and invoke the roles we have been developing.
gather_facts is an Ansible property to fetch operative system data like distribution (
ansible_distribution_release) among other useful variables. You can also read these variables with the Ansible setup module.
7. Start Cluster
It's finished. You can now start your cluster with
vagrant up and then perform your connections and failover tests.
Testing Cluster Failover
Now that our cluster is up and configured, you can start by shutting down your standby node:
You will see that the cluster is operating normally. Bring the standby node back and it will stay that way.
How about taking down the primary node?
At this point, as
repmgrd is enabled, the standby node will retry connecting to the primary node the configured number of times (
reconnect_attempts = 5) and, if it obtains no response, will promote itself to primary and take over write operations on the PostgreSQL cluster. Success!
To join the cluster again, the old primary node will have to lose its current data, clone the new primary data, and register as a new standby.
This last command shows us that the cluster is working properly, but with inverted roles.
Nothing wrong with this, but let's make these nodes switch their roles.
And we're back to the initial state.
We managed to build a fault-tolerant PostgreSQL cluster using Vagrant and Ansible.
High availability is a big challenge. Much like life’s own matters, we are only prepared for the biggest challenges when we fit that challenges’ conditions.
Production environment unique problems are natural and tough to guess. Bridging the gap between development and production is a way to prevent deployment/production issues. We can make some efforts toward that objective, and that is precisely what we just achieved with this high availability database setup.
You can find the source code of this tutorial here.
Published at DZone with permission of Rui Trigo . See the original article here.
Opinions expressed by DZone contributors are their own.