Operations
Backup And Restores
Custom Secret
Monitoring
tpl
This page describes how KubeBlocks deploys a PostgreSQL high-availability (HA) cluster on Kubernetes — covering the resource hierarchy, pod internals, traffic routing, and automatic failover.
pg-cluster-postgresql:5432pg-cluster-postgresql:6432 (pgbouncer)pod-N.xxx-headless:5432kubeblocks.io/role=primarypod-N.headless.svc → stable DNSrole=primary updatedrole=primaryKubeBlocks models a PostgreSQL cluster as a hierarchy of Kubernetes custom resources:
Cluster → Component → InstanceSet → Pod × N
| Resource | Role |
|---|---|
| Cluster | User-facing declaration — specifies topology, replicas, storage, and resources |
| Component | Generated automatically; references a ComponentDefinition that describes container specs, lifecycle actions, and services |
| InstanceSet | KubeBlocks custom workload (replaces StatefulSet); manages pods with stable identities and role awareness |
| Pod | Actual running instance; each pod gets a unique ordinal and its own PVC |
Every PostgreSQL pod runs four containers:
| Container | Port | Purpose |
|---|---|---|
postgresql (Patroni + Spilo) | 5432 (PG), 8008 (Patroni API) | Database engine with built-in HA via Patroni |
pgbouncer | 6432 | Per-pod connection pool (always enabled, no on/off switch) |
dbctl (kbagent) | 5001 | Role probe endpoint — KubeBlocks queries GET /v1.0/getrole every second |
pg-exporter | 9187 | Prometheus metrics exporter |
An init container (pg-init-container) runs postgres-pre-setup.sh before the main containers start to initialize the data directory and configuration files.
Each pod mounts its own PVC (data-{ordinal}, default 20 Gi) at /home/postgres/pgdata, providing independent persistent storage.
KubeBlocks uses Patroni for PostgreSQL HA, with the Kubernetes API as the DCS (Distributed Configuration Store):
| Kubernetes Object | Purpose |
|---|---|
ConfigMap {scope}-config | Cluster-wide Patroni configuration; TTL 30 s, loop interval 10 s |
Endpoints {scope} | Leader lease — the node holding this lease is primary |
Secret account-* | Auto-generated passwords for system accounts |
On startup, every pod joins the Patroni cluster under the same scope. Patroni holds a leader election by acquiring the Endpoints lease; the winner becomes primary and the others stream WAL as replicas.
KubeBlocks creates two services for each PostgreSQL cluster:
| Service | Type | Ports | Selector |
|---|---|---|---|
{cluster}-postgresql | ClusterIP | 5432 (PG), 6432 (pgbouncer) | kubeblocks.io/role=primary |
{cluster}-postgresql-headless | Headless | — | all pods |
The key mechanism is roleSelector: primary on the ClusterIP service. KubeBlocks probes each pod's role via dbctl every second and updates the pod label kubeblocks.io/role. The service's Endpoints therefore always point at the current primary — no VIP or external load balancer required.
{cluster}-postgresql:5432 or :6432 (pgbouncer)pod-N.{cluster}-postgresql-headless.{namespace}.svc.cluster.localpgbouncer proxies connections to the local pod's PostgreSQL instance (its own pod IP), not to the primary. Traffic steering to the primary is handled by the ClusterIP service's role selector, not by pgbouncer.
When the primary pod fails, the following sequence restores service automatically:
Endpoints object wins and promotes itself to primarydbctl role probe returns primary for the new winnerkubeblocks.io/role=primary is applied to the new primary podTotal failover time is typically within 30–60 seconds, bounded by Patroni's TTL and the role probe interval.
For a planned switchover (e.g., maintenance), KubeBlocks calls the Patroni switchover API via switchover.sh, which performs a graceful demotion of the current primary and promotion of a chosen replica with no data loss.
KubeBlocks automatically creates and manages the following PostgreSQL system accounts. Passwords are auto-generated and stored in Secrets named {cluster}-{component}-account-{name}.
| Account | Role | Purpose |
|---|---|---|
postgres | Superuser | Default admin account |
kbadmin | Superuser | KubeBlocks internal management |
kbdataprotection | Superuser | Backup and restore operations |
kbprobe | Monitor (read-only) | Health checks |
kbmonitoring | Monitor | Prometheus metrics collection |
kbreplicator | Replication | Streaming replication from replicas |