The Production Cluster
Three nodes, automatic failover, pooling, TLS and backups: what compose_prod.yaml runs and why
compose.yaml is one Postgres, enough to learn SQL on. Everything from the
replication chapter onwards runs on compose_prod.yaml: the shape a real
deployment has, on one laptop. This page is the map; the later chapters
measure each part.
| Piece | Version | Job |
|---|---|---|
| PostgreSQL | 18.6 | the database, on three nodes |
| Patroni | 4.1.5 | runs Postgres on each node, decides who is primary, fails over |
| etcd | 3.7.2 | three-member consensus store holding Patroni's leader key |
| HAProxy | 3.2.26 | port 5000 always reaches the primary, 5001 the replicas |
| PgBouncer | 1.26.1 | connection pooling; the only endpoint applications use |
| pgBackRest | 2.59.3 | backups and the continuous WAL archive, to SeaweedFS |
PostgreSQL 19 was at release candidate 1 when this was built, so the module runs 18.6, the newest stable release. pgBackRest 2.60.0 had been out for five days but wasn't in the PostgreSQL apt repository yet, so the node image pins the packaged 2.59.3.
Bring it up
(cd ../seaweedfs && docker compose -f compose_prod.yaml up -d) # backup target
docker compose -f compose_prod.yaml up -d --build
docker exec pg-1 patronictl -c /etc/patroni/patroni.yml list
| Member | Host | Role | State | TL | Receive LSN | Lag | Replay LSN | Lag |
| pg-1 | pg-1 | Replica | streaming | 1 | 0/4000060 | 0 | 0/4000060 | 0 |
| pg-2 | pg-2 | Replica | streaming | 1 | 0/4000060 | 0 | 0/4000060 | 0 |
| pg-3 | pg-3 | Leader | running | 1 | | | | |From up to three nodes streaming took 34 seconds. Nobody chose
pg-3 as the primary: all three Patroni processes raced to create the
leader key in etcd, and pg-3 won. It ran initdb, the other two cloned it
with pg_basebackup and started streaming.
One config for three nodes
Each node runs the same image (node/Dockerfile: the Postgres image plus
Patroni and pgBackRest from the PostgreSQL apt repository) and the same
config/patroni.yml. Only the name and addresses differ, passed as
environment variables.
The bootstrap.dcs block is read once. After the cluster exists, the copy
in etcd is the truth and you change it with patronictl edit-config.
Editing the file and restarting changes nothing, which surprises everyone
once.
How clients connect
Applications connect to PgBouncer (appdb for reads and writes,
appdb_ro for read-only). PgBouncer connects to HAProxy, and HAProxy
asks each node's Patroni REST API which one is primary:
listen primary
bind :5000
option httpchk
http-check send meth GET uri /primary # 200 only on the leader
default-server inter 1s fall 2 rise 1 on-marked-down shutdown-sessions
server pg-1 pg-1:5432 check port 8008
server pg-2 pg-2:5432 check port 8008
server pg-3 pg-3:5432 check port 8008Nothing in the application knows which node is primary. After a
failover, HAProxy's next health check finds the new one. The failover
chapter shows that two of these settings (rise 1, and PgBouncer's
server_login_retry) were not the first version: the drills found them.
Every connection must use TLS (pg_hba.conf rejects anything else), and
the superuser can only log in over the local socket, through docker exec. The application gets a role that can read and write data but not
create or drop tables. The security lab attacks each of these.
What went wrong building it
These are part of the lesson, because each one is a real production trap.
- Replicas took up to five minutes to appear.
pg_basebackupcopies nothing until the primary's next checkpoint, and withcheckpoint_timeoutat 5 minutes that's the worst case. Patroni'sbasebackup: [{checkpoint: fast}]forces one. With it, 34 seconds. - pgBackRest refused to talk to SeaweedFS. pgBackRest only speaks
HTTPS to S3, and SeaweedFS's S3 port is plain HTTP. A small HAProxy
(
s3-gateway) now terminates TLS in front of it, which is better anyway: backups shouldn't cross a network in plaintext. - Rebuilding the cluster broke WAL archiving. A new cluster gets a new
system identifier, and pgBackRest refused to archive into the stanza the
previous cluster created:
system-id 7699567011657928738 do not match repo1 stanza ... 7699566270498205730. It's protecting you from mixing two databases' WAL.scripts/reset.shdeletes the repository together with the volumes. - Neither PgBouncer nor pgBackRest accepts comments at the end of a
line.
default_pool_size = 20 ; per pairis a fatal config error in PgBouncer, andstart-fast=y # commentmakes pgBackRest complain thatstart-fastmust beyorn.
This runs everything on one machine, so it can't survive that machine. Real HA puts the three Postgres nodes and the three etcd members in separate failure domains (zones), and runs two PgBouncers and two HAProxies behind a virtual IP or a load balancer. The module README lists every gap between this and production.