PostgreSQL High-Availability Cluster (based on "Patroni" and "DCS(etcd)"). Automating deployment with Ansible.
This Ansible playbook is designed for deploying a PostgreSQL high availability cluster on dedicated physical servers for a production environment. Сluster can be deployed in virtual machines for test environments and small projects.
This playbook support the deployment of cluster over already existing and running PostgreSQL. You must specify the variable postgresql_exists='true'
in the inventory file.
Attention! Your PostgreSQL will be stopped before running in cluster mode. You must planing downtime of existing databases.
❗ Please test it in your test enviroment before using in a production.
You have two options available for deployment ("Type A" and "Type B"):
To use this scheme, specify
with_haproxy_load_balancing: 'true'
in variable file vars/main.yml
This scheme provides the ability to distribute the load on reading. This also allows us to scale out the cluster (with read-only replicas).
- port 5000 (read / write) master
- port 5001 (read only) all replicas
- port 5002 (read only) synchronous replica only
- port 5003 (read only) asynchronous replicas only
❗ Your application must have support sending read requests to a custom port (ex 5001), and write requests (ex 5000).
Patroni is a template for you to create your own customized, high-availability solution using Python and - for maximum accessibility - a distributed configuration store like ZooKeeper, etcd, Consul or Kubernetes. Used for automate the management of PostgreSQL instances and auto failover.
etcd is a distributed reliable key-value store for the most critical data of a distributed system. etcd is written in Go and uses the Raft consensus algorithm to manage a highly-available replicated log. It is used by Patroni to store information about the status of the cluster and PostgreSQL configuration parameters.
What is Distributed Consensus?
HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications.
confd manage local application configuration files using templates and data from etcd or consul. Used to automate HAProxy configuration file management.
Keepalived provides a virtual high-available IP address (VIP) and single entry point for databases access. Implementing VRRP (Virtual Router Redundancy Protocol) for Linux. In our configuration keepalived checks the status of the HAProxy service and in case of a failure delegates the VIP to another server in the cluster.
PgBouncer is a connection pooler for PostgreSQL.
This is simple scheme without load balancing Used by default
To provide a single entry point (VIP) for databases access is used "vip-manager".
vip-manager manages a virtual IP (VIP) based on state kept in etcd or Consul. Is written in Go. ©️ Cybertec Schönig & Schönig GmbH https://www.cybertec-postgresql.com
RedHat and Debian based distros (x86_64)
- RedHat: 7
- CentOS: 7
- Ubuntu: 16.04
- Debian: 8
✅ tested, works fine: Debian 9, Ubuntu 18.04, CentOS 7.6
all supported PostgreSQL versions
✅ tested, works fine: PostgreSQL 9.6, 10, 11
This has been tested on Ansible 2.7.10 and higher.
This playbook requires root privileges or sudo.
Ansible (What is Ansible?)
- Install Ansible to the managed machine
My recommendation: latest releases via Pip
- Download or clone this repository
git clone https://github.com/vitabaks/postgresql_cluster.git
- Go to the playbook directory
cd postgresql_cluster/
- Edit the inventory file
Specify the ip addresses and connection settings (ansible_user
, ansible_ssh_pass
...) for your environment
vim inventory
- Edit the variable file vars/main.yml
vim vars/main.yml
proxy_env
(for offline installation)
example:
proxy_env:
http_proxy: http://proxy_server_ip:port
https_proxy: http://proxy_server_ip:port
cluster_vip
patroni_cluster_name
with_haproxy_load_balancing
'true'
(Type A) or'false'
/default (Type B)postgresql_version
- Run playbook:
ansible-playbook deploy_pgcluster.yml
See the vars/main.yml file for details.
Please note that the original design goal of this playbook was more concerned with the initial deploiment of a PostgreSQL HA Cluster and so it does not currently concern itself with performing ongoing maintenance of a cluster.
You should learn each component of the cluster for its further maintenance.
- Tutorial: Management of High-Availability PostgreSQL clusters with Patroni
- Patroni documentation
- etcd operations guide
Involves a set of policies, tools and procedures to enable the recovery or continuation of vital technology infrastructure and systems following a natural or human-induced disaster.
A high availability cluster provides an automatic failover mechanism, and does not cover all disaster recovery scenarios. You must take care of backing up your data yourself.
Patroni nodes are dumping the state of the DCS options to disk upon for every change of the configuration into the file patroni.dynamic.json located in the Postgres data directory. The master (patroni leader) is allowed to restore these options from the on-disk dump if these are completely absent from the DCS or if they are invalid.
However, I recommend that you read the disaster recovery guide for the etcd cluster:
I can recommend the following backup and restore tools:
Do not forget to validate your backups (for example pgbackrest auto).
Licensed under the MIT License. See the LICENSE file for details.
Vitaliy Kukharik (PostgreSQL DBA) [email protected]
Are welcome!