← All posts

Managing PostgreSQL availability with CloudNativePG

Several copies of an application can all stop when one database disappears. That's the part I need to address for services in Zebra, my Kubernetes cluster, that keep their state in PostgreSQL.

CloudNativePG, or CNPG, manages PostgreSQL in Kubernetes. It understands primary and standby roles and coordinates replication and failover. That lets the layer which understands database writes manage the copies, rather than asking shared storage to solve every availability problem.

Where the copies live

Authentik, my sign-in service, has a database configured with two instances, local-path storage, and pod anti-affinity. Local-path keeps each instance's files on its node; anti-affinity asks Kubernetes to spread the pods. This is the relevant excerpt:

spec:
  instances: 2
  storage:
    storageClass: local-path
    size: 20Gi
  affinity:
    enablePodAntiAffinity: true

It isn't a complete installation. It needs the CNPG operator, a suitable SSD-backed StorageClass, credentials, backups, and surrounding configuration. Placement still needs checking: two pods don't prove independent power or networking.

Local SSDs suit the low-latency, high-I/O database work without sending each disk operation to the NAS. Actual performance depends on the hardware and contention. PostgreSQL keeps the other instance's copy through replication; the lost disk doesn't have to move to a standby before it can take over.

Primary -> live replication -> standby
   |
   +-> base backup + transaction history -> Garage
                                              |
                                    NAS off-site backup path

A node-local volume remains tied to its node. Database pods ask the autoscaler not to evict them automatically, but that isn't protection against every node failure or removal. And with most equipment in one room, independent disks still share physical risks. These are storage choices per workload, not universal redundancy.

What failover means to an application

If the primary disappears, CNPG can promote an eligible standby and redirect the read-write service. Applications still need to reconnect. Detection and promotion take time, and a stable service address doesn't guarantee an existing transaction completes.

Replication policy also affects data loss. An asynchronous standby may not have received every acknowledged write. Synchronous policies can reduce particular loss windows while changing latency and availability when the required standby cannot participate. I haven't established a failover-time or data-loss bound for this article.

A useful exercise records successful test writes, loses the primary in a controlled way, reconnects through the normal application path, and checks which writes survived. A planned switchover doesn't prove abrupt node-loss or network-partition behaviour. Those cases also need to establish that the former primary cannot continue accepting conflicting writes.

For a service that can wait for a restore, a single database with a tested backup may be enough. Replicas bring placement, capacity, and operational work of their own.

Recovering an earlier state

Replication can copy an accidental deletion or bad migration just as effectively as a useful write. Recovery needs a base backup and the relevant write-ahead logs, or WAL: the recorded changes used to recover forwards from that backup.

My archives go to Garage, which depends on TrueNAS. The database can therefore survive some NAS outages while its backup destination is unavailable. That is not a healthy recovery path indefinitely.

I run automated database restores. The drill rotates through targets, restores a separate instance, and inspects tables and sampled rows. It omits backup archiving on the restored instance so it cannot overwrite the source archive. Current per-target results aren't established here, and a drill using live Garage doesn't test losing the whole room.

The remaining question is whether I can recover a useful application at the required point in time. That's why database drills belong alongside the wider backup checks, rather than making a green operator status the end of the story.

← Back to the notebook