Bootstrapping the Cluster
With Patroni configured on all three nodes, we bring the cluster up. The order matters: the first node to start acquires the leader lock in etcd, runs initdb, creates the users, and becomes the primary. The others then clone from it.
Start the first node (the seed primary)
On pg-sv01:
sudo systemctl start patroni
Give it ~15 seconds, then check:
sudo patronictl -c /etc/patroni/patroni.yml list
+ Cluster: pg-cluster --------+---------+---------+----+-----------+
| Member | Host | Role | State | TL | Lag in MB |
+---------+----------------+--------+---------+----+-----------+
| pg-sv01 | 10.100.100.104 | Leader | running | 1 | |
+---------+----------------+--------+---------+----+-----------+
pg-sv01 is now the primary on timeline 1.
Start the replicas
On pg-sv02 and pg-sv03:
sudo systemctl start patroni
Each replica automatically runs pg_basebackup from the leader, then begins streaming. After a minute:
sudo patronictl -c /etc/patroni/patroni.yml list
+ Cluster: pg-cluster --------+---------+-----------+----+-----------+
| Member | Host | Role | State | TL | Lag in MB |
+---------+----------------+---------+-----------+----+-----------+
| pg-sv01 | 10.100.100.104 | Leader | running | 1 | |
| pg-sv02 | 10.100.100.105 | Replica | streaming | 1 | 0 |
| pg-sv03 | 10.100.100.106 | Replica | streaming | 1 | 0 |
+---------+----------------+---------+-----------+----+-----------+
One Leader and two Replicas, all on the same timeline with zero lag. The cluster is live.
Quick sanity checks
# Who is the primary, and is it writable?
sudo patronictl -c /etc/patroni/patroni.yml list
# Connect to the primary and confirm it is NOT in recovery:
PGPASSWORD=ChangeMe_Postgres psql -h pg-sv01 -U postgres -c "SELECT pg_is_in_recovery();" # -> f
# Connect to a replica and confirm it IS in recovery (read-only):
PGPASSWORD=ChangeMe_Postgres psql -h pg-sv02 -U postgres -c "SELECT pg_is_in_recovery();" # -> t
Useful patronictl commands
| Command | What it does |
|---|---|
patronictl list |
Show cluster topology and lag |
patronictl topology |
Same, as a tree |
patronictl switchover |
Planned role change (you choose the new leader) |
patronictl failover |
Force a failover when there is no healthy leader |
patronictl restart <cluster> <member> |
Restart a member's PostgreSQL |
patronictl edit-config |
Edit the cluster-wide DCS config |
patronictl reinit <cluster> <member> |
Re-clone a member from the leader |
Next we put HAProxy in front so clients get a single, stable endpoint.
No comments to display
No comments to display