rom8726.github.io

ansible-patroni

Ansible playbook for deploying PostgreSQL Patroni cluster

Github: github.com/rom8726/ansible-patroni

⚠️ WARNING: This configuration is for testing/development purposes only. DO NOT use in production without proper security hardening!

Overview

This Ansible playbook automates the deployment of a PostgreSQL high-availability cluster using Patroni and etcd. The setup includes the following components for connection management and high availability:

The connection path:

Client -> Keepalived VIP -> HAProxy -> PgBouncer -> PostgreSQL

Prerequisites

Components

Quick Start

  1. Clone this repository.
  2. Create config files with the command make init.
  3. Update inventory.ini with your servers:
[promoters]
patroni1 ansible_host=192.168.64.2 node_id=1
patroni2 ansible_host=192.168.64.3 node_id=2
patroni3 ansible_host=192.168.64.4 node_id=3
  1. Update ansible.cfg with your user (root by default).
  2. Set variable cluster_vip_1 in group_vars (some VIP address for keepalived).
  3. Set variable keepalived_vip_interface in keepalived default vars.
  4. Run the playbook:
# Deploy Patroni cluster
make up

Default Configuration

Network Ports

PostgreSQL 5432
Patroni API 8008
etcd client 2379
etcd peer 2380
HAProxy PostgreSQL (master) 5000
HAProxy PostgreSQL (replica) 5001
HAProxy Statistics 7000
PgBouncer 6432
Node Exporter 9100
Postgres Exporter 9187

Keepalived Configuration

PgBouncer Configuration

PostgreSQL Settings

Authentication (Default)

⚠️ Change these values:

Monitoring Components

Node Exporter

Postgres Exporter

Service Management

# Patroni service
systemctl status patroni
systemctl start patroni
systemctl stop patroni

# etcd service
systemctl status etcd
systemctl start etcd
systemctl stop etcd

# HAProxy service
systemctl status haproxy
systemctl start haproxy
systemctl stop haproxy

# PgBouncer service
systemctl status pgbouncer
systemctl start pgbouncer
systemctl stop pgbouncer

# Keepalived service
systemctl status keepalived
systemctl start keepalived
systemctl stop keepalived

# Monitoring services
systemctl status node_exporter
systemctl status postgres_exporter

Monitoring

Available Metrics

PostgreSQL Exporter Metrics

Postgres Exporter provides the following metric groups:

PostgreSQL Settings

Replication Status

Connection Statistics