How do you set up high availability for self-managed PostgreSQL on Kubernetes?

AdvancedProductionPostgreSQL
Answer

Short answer

Use an operator — CloudNativePG, Zalando Postgres Operator or Crunchy PGO — that runs a primary plus streaming-replica standbys on separate nodes/zones, performs automatic failover, manages a stable read-write Service, and handles backups to object storage with PITR.

CloudNativePG example

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata: { name: shop-db }
spec:
  instances: 3                              # 1 primary + 2 replicas
  storage: { size: 50Gi, storageClass: gp3 }
  affinity: { topologyKey: topology.kubernetes.io/zone }
  backup:
    barmanObjectStore:
      destinationPath: s3://shop-db-backups/
      s3Credentials: { inheritFromIAMRole: true }
    retentionPolicy: "30d"

The operator creates Services: shop-db-rw (always the primary), shop-db-ro (replicas), shop-db-r (any).

What makes it production-grade

  • Spread across zones with anti-affinity; PodDisruptionBudgets.
  • Continuous WAL archiving + scheduled base backups to S3/Blob, with restore drills.
  • Monitoring — Prometheus exporter, alerts on replication lag, failovers, disk.
  • Connection pooling — operator's PgBouncer pooler.
  • Fast storage with snapshots; volume expansion enabled.

Honest trade-off

A managed service removes most of this work; self-managed on Kubernetes makes sense for portability, cost at scale or special extensions.

What to say in the interview

"I'd use an operator like CloudNativePG: three instances across zones, automatic failover with a stable rw Service, continuous WAL archiving to object storage for PITR, PgBouncer pooling and Prometheus alerting. If the team doesn't need that control, RDS or Azure Flexible Server is simpler."

All Databases (MySQL, PostgreSQL, MongoDB) questions Roadmap: PostgreSQL basics

More on PostgreSQL