Webhook icon
Request icon
OutputValues icon
If icon
Fail icon
SlackIncomingWebhook icon
Pause icon
LoopUntil icon
Set icon

Patroni Planned Switchover with Pre-Checks, Approval and Verification

A gated Patroni switchover. REST API pre-checks, a Slack approval, a re-check right before POST /switchover, and verification of the new leader and timeline.

Categories
Infrastructure

A planned Patroni switchover (before OS patching, a minor upgrade, or moving the primary to another zone) is one patronictl switchover away. It is also easy to get wrong at 2 a.m.: switching to a replica that is minutes behind, during a DCS outage, or an hour after the plan was approved when the cluster no longer looks the same. Afterwards someone still has to check that the replicas actually follow the new leader.

This blueprint does the checks, the approval and the verification around the Patroni REST API.

How it works

  1. read_cluster (GET /cluster) and plan evaluate the cluster with jq:

    • there is a leader, and the cluster is not paused;
    • no switchover is already scheduled;
    • the candidate is streaming, on the leader's timeline, within max_lag_bytes, and has no nofailover tag.

    Without a candidate input, the streaming replica with the least lag is picked. Any problem refuses the switchover before anyone is asked.

  2. request_approval posts the plan and the cluster to Slack. approve (Pause) waits for a yes; a decline or a timeout leaves the leader where it is.

  3. After approval, everything is checked again. The flow refuses if the leader changed, or if the candidate stopped streaming or fell behind while it waited.

  4. switchover calls POST /switchover with the leader and candidate, using basic auth from secrets. If Patroni answers with an error, the flow reports Patroni's reason and the current leader.

  5. verify (LoopUntil) polls /cluster until the candidate is leader on timeline + 1 and every other member streams on that timeline, within verify_timeout. The result is stored in the KV store and posted to Slack.

Error handling

These are the messages from the tests:

  • Lagging candidate, a replica whose VM froze while the leader kept writing: "SWITCHOVER REFUSED before approval: candidate pg2 is 57911312 bytes behind the leader (limit 1048576)".
  • Candidate stopped after the approval: "SWITCHOVER REFUSED after approval, the cluster changed while waiting: candidate pg3 is stopped, not streaming".
  • DCS (etcd) down: GET /cluster is served from the DCS, so the flow stops at read_cluster before anything else happens.
  • Maintenance mode (patronictl pause): refused, since Patroni would not manage the switchover.
  • Patroni cannot complete the switchover: Patroni's HTTP status and answer are quoted, together with the cluster as it is now.
  • A topology that does not settle (a replica that does not reconnect) fails verify after verify_timeout, listing every member's state and timeline.
  • One run at a time. concurrency (limit 1, QUEUE) makes a new run wait while another one waits for approval; answer or kill the waiting run first.

What this blueprint teaches about Kestra

  • jq in Pebble as a rules engine: the pre-check and the re-check are the same jq program over two API responses, stored once in variables.
  • HTTP Request with allowFailed, so a non-2xx answer from Patroni becomes a clear message instead of a stack trace, and with basic auth from secrets.
  • LoopUntil as a convergence check after an operation that settles asynchronously.
  • Pause followed by a re-check: an approval is a decision about the cluster as it was then, so the flow checks again before it acts.

Prerequisites

  • A Patroni cluster (3.x or 4.x) whose REST API the Kestra worker can reach, with restapi.authentication configured for the POST endpoints.
  • A Slack incoming webhook.

Secrets

  • PATRONI_API_USERNAME, PATRONI_API_PASSWORD: the restapi.authentication credentials.
  • SLACK_WEBHOOK_URL.
  • PATRONI_SWITCHOVER_WEBHOOK_KEY: a long random string for the change-management webhook.

Inputs

  • patroni_url: REST API of any member, e.g. http://pg-node-1:8008.
  • candidate: member to promote; empty picks the streaming replica with the least lag.
  • max_lag_bytes (1048576; keep it at or below Patroni's maximum_lag_on_failover), approval_timeout (PT1H), verify_timeout (PT2M).

Outputs

  • KV patroni_last_switchover: time, old and new leader, the new timeline and the approver's note. outputs.settle.values.state holds the final topology.

Quick start

  1. Add the secrets and set patroni_url. Run the flow with no candidate.
  2. Slack shows the plan, e.g. "pg1 -> pg2 (candidate lag 0 bytes, timeline 3)". Resume with Switch over now? checked.
  3. The run ends with "Patroni switchover DONE: pg1 -> pg2, timeline 3 -> 4, 2 of 2 replicas streaming from the new leader."

How to extend

  • Drain connection pools (PgBouncer PAUSE) before the switchover and RESUME after verify.
  • Run a write probe against the new leader at the end, through its JDBC URL.
  • Move the leader back after maintenance with a second run that names the old leader as candidate.

Plugins used

See How

New to Kestra?

Use blueprints to kickstart your first workflows.