
PostgreSQL is a powerful, robust, and scalable open-source relational database management system. Critical applications require high availability through replication and load balancing in a cluster architecture. A Patroni cluster requires an odd number of nodes to maintain quorum safety. Quorum refers to the rule that the majority of nodes must agree to elect a leader or make changes. With three nodes, if one fails, the remaining two still form a majority, allowing the cluster to continue operating safely.
This article shows you how to build a three-node PostgreSQL cluster on Ubuntu 24.04 using Patroni for automated replication and failover, and HAProxy to load-balance client requests.
Before you begin, ensure you:
Have access to three Ubuntu 24.04 servers with at least 2 CPU cores and 4 GB RAM, as a non-root sudo users.
Install PostgreSQL on all three servers.
A registered domain with A records for three subdomains pointing to each server's IP address:
node1.example.comnode2.example.comnode3.example.comReplace all domain placeholders throughout this article with your actual values. For example, if your domain is mydb.com, replace node1.example.com with node1.mydb.com.
Install required packages, configure firewall ports, and set up SSL certificates for secure cluster communication.
Update the system package index of each node.
Install HAProxy, Certbot, and required Python packages on each node.
Install Patroni and its dependencies using pip3.
Download and install etcd binaries on each node.
Open required firewall ports on each node.
The ports serve the following purposes:
Reload the firewall.
Verify firewall configuration.
Set up Let's Encrypt SSL certificates for secure cluster communication.
Request SSL certificates for each node. Run this command on Node1:
Repeat for Node2 and Node3, updating the subdomain for each node.
Create a certificate preparation script on each node. Update the HOSTNAME variable with subdomain for each node.
Add the following content, updating HOSTNAME for each node:
Make the script executable.
Run the script.
Create an SSL renewal hook script on each node.
Add the following content, updating HOSTNAME for each node:
Make the renewal script executable.
Configure automatic renewal hooks. Run on Node1:
Repeat for Node2 and Node3, updating the subdomain for each node.
Patroni uses etcd as a distributed key-value store for cluster coordination. Configure etcd with HTTPS security on all nodes.
Create the systemd service file for etcd.
Add the following configuration for Node1, replace example.com with your domain name:
For Node2 and Node3, update:
--name parameter with node2 or node3 value--initial-advertise-peer-urls and --advertise-client-urlsCreate the data and config directories.
Set ownership and secure the data directory.
Reload systemd so it sees the new unit file.
Enable the etcd service so it starts on boot.
Start the etcd service.
Verify etcd cluster status.
Check cluster membership.
Output:
Verify cluster health.
Configure PostgreSQL to use SSL certificates and optimize performance settings.
Edit PostgreSQL configuration on each node.
Update the following settings for Node1:
Update certificate paths for Node2 and Node3 accordingly.
Enforce SSL connections by editing pg_hba.conf.
Add this line at the end:
Restart PostgreSQL.
Verify SSL is enabled.
Output:
Patroni manages PostgreSQL replication and automatic failover. Configure it on each node with HTTPS support.
Create the Patroni configuration file.
Add the following configuration for Node1:
For Node2 and Node3, update:
name fieldconnect_address URLsUse the same passwords across all nodes.
Create the runtime directory for PostgreSQL.
Set ownership and permissions so the patroni user can use it.
Ensure the directory is recreated on reboot by adding a tmpfiles.d entry.
Prepare PostgreSQL data directory for Patroni.
Back up any existing data directory.
Create the new PostgreSQL data directory for Patroni.
Give ownership to the patroni user.
Lock down permissions on the data dir.
Make PostgreSQL binaries accessible (needed for some init scripts).
Stop PostgreSQL to let Patroni manage it.
Create the Patroni systemd service.
Add the following configuration:
Reload systemd so it sees the unit file.
Enable Patroni so it starts automatically on boot.
Start Patroni now and check it’s running.
Verify cluster status.
Output:
Set up HAProxy on all nodes to load balance PostgreSQL connections.
Remove the default configuration.
Create a new HAProxy configuration.
Add the following configuration (same for all nodes):
Restart HAProxy.
Verify HAProxy status.
Verify high availability and automatic failover capabilities.
Connect to the cluster from a PostgreSQL client. Replace the password and domains:
Check which node is currently serving as primary.
Exit the psql console.
Test failover by stopping Patroni on the current leader node (for example, Node1).
Check the new cluster status from another node.
The output shows a new leader has been elected automatically.
Verify the cluster is still accessible through the connection string.
Restart the stopped node to rejoin the cluster.
You have successfully deployed a highly available PostgreSQL cluster on Ubuntu 24.04 using Patroni for replication management and HAProxy for load balancing. The cluster automatically handles node failures and ensures continuous database availability. All traffic is secured with SSL/TLS encryption. Consider adding monitoring tools like Prometheus and Grafana to track cluster health and performance metrics in production environments.
0 Comments
Be the first to comment and share your perspective with the community.