Cloud Native 10 min read

Managing StackGres 1.6 HA Clusters Fully with Patroni

This guide walks through identifying master and replica pods, performing graceful switchover, triggering automated failover, and reinitializing replicas in a StackGres 1.6 cluster using kubectl and patronictl commands, with practical notes and safety warnings.

Hacker Afternoon Tea
Hacker Afternoon Tea
Hacker Afternoon Tea
Managing StackGres 1.6 HA Clusters Fully with Patroni

Identify Master and Replica Pods

List StackGres pods with label filters:

kubectl get pods -n default -l app=StackGresCluster,stackgres.io/cluster=true

Master only:

kubectl get pods -n default -l app=StackGresCluster -l role=master

Replica only:

kubectl get pods -n default -l app=StackGresCluster,stackgres.io/cluster=true -l role=replica

Alternatively, exec into a Patroni container and run patronictl list to view cluster members, roles, IPs, ports, state, timeline and replication lag:

kubectl exec -it stackgres-0 -c patroni -- bash
patronictl list
+-----------+-------------+------------------+--------+---------+----+-----------+
|  Cluster  |    Member   |       Host       |  Role  |  State  | TL | Lag in MB |
+-----------+-------------+------------------+--------+---------+----+-----------+
| stackgres | stackgres-0 | 10.244.0.11:5433 | Leader | running |  2 |           |
| stackgres | stackgres-1 | 10.244.0.12:5433 |        | running |  2 |       0.0 |
| stackgres | stackgres-2 | 10.244.0.13:5433 |        | running |  2 |       0.0 |
+-----------+-------------+------------------+--------+---------+----+-----------+
Note: Change the -n flag to the appropriate namespace.

Graceful Switchover

Execute Switchover to a Replica

Run patronictl switchover. The tool prompts for the current master, the candidate replica, the execution time (default now) and a confirmation.

patronictl switchover
Master [stackgres-0]:
Candidate ['stackgres-1', 'stackgres-2'] []:
When should the switchover take place (e.g. 2020-01-16T17:23) [now]:
Current cluster topology
+-----------+-------------+------------------+--------+---------+----+-----------+
|  Cluster  |    Member   |       Host       |  Role  |  State  | TL | Lag in MB |
+-----------+-------------+------------------+--------+---------+----+-----------+
| stackgres | stackgres-0 | 10.244.0.11:5433 | Leader | running |  2 |           |
| stackgres | stackgres-1 | 10.244.0.12:5433 |        | running |  2 |       0.0 |
| stackgres | stackgres-2 | 10.244.0.13:5433 |        | running |  2 |       0.0 |
+-----------+-------------+------------------+--------+---------+----+-----------+
Are you sure you want to switchover cluster stackgres, demoting current master stackgres-0? [y/N]: y
2020-01-16 16:26:13.03648 Successfully switched over to "stackgres-1"
+-----------+-------------+------------------+--------+---------+----+-----------+
|  Cluster  |    Member   |       Host       |  Role  |  State  | TL | Lag in MB |
+-----------+-------------+------------------+--------+---------+----+-----------+
| stackgres | stackgres-0 | 10.244.0.11:5433 |        | stopped |    | unknown |
| stackgres | stackgres-1 | 10.244.0.12:5433 | Leader | running |  3 |           |
| stackgres | stackgres-2 | 10.244.0.13:5433 |        | running |    | unknown |
+-----------+-------------+------------------+--------+---------+----+-----------+
Important: Avoid other patronictl operations unless you fully understand their impact, as they may cause data loss or corruption.

Automated Failover

Patroni‑Driven Failover

Delete the leader pod to trigger failover: kubectl delete pod cluster-0 -n stackgres Kubernetes recreates the pod. Depending on the lock expiration timing, either the new pod assumes leadership or the remaining replica ( cluster-1) is promoted.

+ Cluster: cluster (6979461716096839850) ---+---------+----+-----------+
| Member    | Host                | Role    | State   | TL | Lag in MB |
+-----------+---------------------+---------+---------+----+-----------+
| cluster-0 | 192.168.40.142:7433 | Replica | running |  2 |           |
| cluster-1 | 192.168.12.150:7433 | Leader  | running |  2 |         0 |
+-----------+---------------------+---------+---------+----+-----------+

The same steps can be repeated by terminating either the primary or a replica pod.

Replica Reinitialization

Reinitialize a Stale Replica

When a replica cannot catch up and Patroni cannot recover automatically, reinitialize it from the primary using patronictl reinit.

patronictl reinit
Which member do you want to reinitialize [stackgres-1]? []:
Are you sure you want to reinitialize members stackgres-1? [y/N]: y
Success: reinitialize for member stackgres-1

# Verify the new topology
patronictl list
+-----------+-------------+------------------+--------+---------+----+-----------+
|  Cluster  |    Member   |       Host       |  Role  |  State  | TL | Lag in MB |
+-----------+-------------+------------------+--------+---------+----+-----------+
| stackgres | stackgres-0 | 10.244.0.11:5433 | Leader | running |  2 |           |
| stackgres | stackgres-1 | 10.244.0.12:5433 | Replica| running |  2 |       0   |
+-----------+-------------+------------------+--------+---------+----+-----------+
Important: Use reinitialization only when you understand the consequences; improper use can corrupt the cluster.

Reference:

https://github.com/zalando/patroni
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

kubernetesPostgreSQLFailoverHAPatroniSwitchoverStackGres
Hacker Afternoon Tea
Written by

Hacker Afternoon Tea

You might find something interesting here ^_^

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.