From Standalone to High Availability: Convert Your PostgreSQL Database to a Patroni Cluster
In Database Management, achieving High Availability(HA) is very important, particularly for systems handling critical data and applications. In a Production Environment, any downtime can translate into substantial losses - be it operational, financial or reputational. By ensuring high availability for databases, businesses can avoid the risks associated with system/network failures, hardware malfunctions, or planned/scheduled maintenance activities.
Patroni: Open-Source Solution for HA with PostgreSQL
Popular open source RDBMS provides HA solutions with automated failovers in their own ways. Example, MySQL with InnoDB Cluster, PostgreSQL with Patroni(An open source external framework solution). During the initial implementation, high availability may be overlooked, leading to the configuration of the database as a standalone system.
However, as the system grows and the need for reliability increases, transitioning from a standalone setup to a HA cluster becomes a critical and necessary process. It's important to note that this conversion process is not always straightforward and may require careful planning and execution.
In this blog post, we will explore the process of converting a standalone PostgreSQL instance into a highly available Patroni Cluster, with a key focus on achieving this transition with minimal downtime and keeping it simple as much as possible.
Prerequisites for Converting to a Patroni Cluster
To proceed with this conversion, ensure that you do have following prerequisites
Running PostgreSQL Instance: You must have a PostgreSQL instance already up and running on your system.
Temporary sudo and Internet Access: Ensure that you have temporary sudo access and internet connectivity for downloading and installing any required packages.
Superuser Access to PostgreSQL: You'll need superuser access to PostgreSQL in order to create any necessary users required by Patroni
Location of Data and Bin Directories: Know the locations of the data directory (where PostgreSQL stores its data files) and the bin directory (where PostgreSQL executable files are located) on your system. This information will be necessary for configuring Patroni.
Step-by-Step Guide: Converting Standalone PostgreSQL to Patroni (with Demo)
To demonstrate the conversion process, we have set up a test server with the IP Address 192.168.33.11 (Node1) running Ubuntu 20.04. PostgreSQL has already been installed, configured, and is currently up and running on this server.
Setting Up the Cluster (etcd Installation & Initialization)
Let's begin the installation of the necessary packages for Patroni, starting with etcd on Node1. Since these operations won't affect the currently running PostgreSQL instance, we can proceed at any convenient time without causing any disruptions.
Etcd Installation & Initialization:
Installation of Etcd is straightforward. It can be installed by using the following
Once the installation completes, configure the etcd on the file /etc/default/etcd.
The above configuration can be used to setup and initialize the etcd cluster just by replacing the IP mentioned with your IP address and the node name.
Once the configuration is done, restart the etcd using the following command and check the etcd cluster status as well.
isLeader=true shows the proper configuration of etcd cluster on node 1 and we are good to proceed with the patroni setup.
Patroni Configuration
Lets install patroni on node 1
For the configuration, use the below content in your /etc/patroni/patroni.yml file.
Synchronizing PostgreSQL with Patroni
Before starting the patroni service, create the users defined under authentication section
Also, disable the postgres systemd unit as Patroni will manage the starting and stopping of the postgres process further.
Start the patroni service
Patroni has started and self promoted itself as a leader as it is the only node available now.
As a final step, it's crucial to restart PostgreSQL using Patroni to ensure that all subsequent startup procedures of PostgreSQL are managed by Patroni. This step completes the integration of PostgreSQL into the Patroni cluster, allowing Patroni to oversee the management and operation of PostgreSQL for high availability. This operation needs to be performed during maintenance as it needs a quick restart.
Restart if the PostgreSQL version is less than provided (e.g. 9.5.2).
We have successfully made our transition from a standalone to a highly available patroni cluster. Further replicas can be added by performing installation and configuration of etcd & patroni on the new nodes. Data Stream will be carried over by patroni to build the replicas.
Also read: Revolutionizing PostgreSQL Schema Changes with pg_osc
Benefits of Using Patroni for High Availability
Automatic failover, simplified management, and flexible configuration make Patroni a game-changer.
- Patroni takes over when your primary server fails, promoting a standby to minimize downtime.
- Manage your cluster with ease - Patroni automates complex tasks, freeing up your time.
- Scale effortlessly - add standby replicas for increased fault tolerance and read performance.
Mydbops helps you leverage Patroni for a robust PostgreSQL setup.
Contact Mydbops today for a free consultation! Leverage Patroni for a highly available and well-managed PostgreSQL environment.
{{cta}}