PostgreSQL DC/DR with Crunchy
A Crunchy PostgresCluster on OpenShift with Patroni replicas, pgBackRest backups to S3, a standby cluster for the DR site, and a promote you can rehearse.
Two different failures get called "DR". A node dies inside one OpenShift cluster: Patroni, which Crunchy Postgres for Kubernetes runs inside the PostgresCluster, promotes another replica, and the payments-primary Service follows it. A site dies: a second PostgresCluster, on the DR OpenShift, has been replaying the same WAL from a pgBackRest repository. You promote that standby, and it becomes the only writer. This lab builds both. The repository sits on S3-compatible storage. StorageGRID is the object store when the platform is NetApp; any S3 bucket you can reach from both sites works the same way.
The operator's API is postgres-operator.crunchydata.com/v1beta1. PostgreSQL 16 matches the vector-databases lab on this cluster. Ask the operator for a major it actually ships before you change that number.
Prerequisites
- OpenShift 4.x and
ocon the DC cluster. The DR half needs a second cluster, or a second namespace if you are only rehearsing the objects. A second namespace proves the YAML and the promote. It does not prove that a site failure is survivable - Crunchy Postgres for Kubernetes installed on each cluster (
oc get crd postgresclusters.postgres-operator.crunchydata.com) - A StorageClass. On the NetApp lab that is
ontap-san, from the Trident SAN lab - A bucket, an access key, and the endpoint hostname from the StorageGRID lab. Create the bucket before the PostgresCluster, or pgBackRest will retry until it exists
oc new-project payments
oc get crd postgresclusters.postgres-operator.crunchydata.com
The repository secret
pgBackRest reads S3 credentials from a secret key named s3.conf. The key names inside the file are fixed: repo1-s3-key and repo1-s3-key-secret.
oc create secret generic payments-pgbackrest -n payments --from-file=s3.conf=<(
printf '[global]\nrepo1-s3-key=%s\nrepo1-s3-key-secret=%s\n' \
"$AWS_ACCESS_KEY_ID" "$AWS_SECRET_ACCESS_KEY"
)
Copy this secret to the DR cluster later, with the same contents. Both clusters must see the same objects. A key that can only read is not enough once the DR side is promoted and starts archiving.
The DC cluster
replicas: 3 means three PostgreSQL pods in the instance set. One is the primary. The other two are replicas. It does not mean "one primary plus three replicas". replicas: 1, as in the vector lab, is a single pod and has nothing to promote.
apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
name: payments
namespace: payments
spec:
postgresVersion: 16
users:
- name: app
databases: [payments]
instances:
- name: instance1
replicas: 3
dataVolumeClaimSpec:
accessModes: ["ReadWriteOnce"]
resources: {requests: {storage: 20Gi}}
backups:
pgbackrest:
configuration:
- secret: {name: payments-pgbackrest}
global:
repo1-path: /pgbackrest/payments/repo1
repo1-s3-uri-style: path
repo1-retention-full: "4"
repo1-retention-full-type: count
repos:
- name: repo1
schedules:
full: "0 1 * * 0"
incremental: "0 1 * * 1-6"
s3:
bucket: pg-backups
endpoint: s3.grid.lab
region: us-east-1
repo1-path, the bucket and the endpoint must be identical on the DR cluster. repo1-s3-uri-style: path is the setting StorageGRID and most non-AWS endpoints need. Four retained full backups is a lab choice; size it from the disk you have and from how far back a restore has to reach. The schedule is weekly full, nightly incremental, in the cluster's timezone.
oc apply -f payments.yaml
oc wait -n payments pod \
-l postgres-operator.crunchydata.com/cluster=payments,postgres-operator.crunchydata.com/role=master \
--for=condition=Ready --timeout=600s
POD=$(oc get pod -n payments -o name \
-l postgres-operator.crunchydata.com/cluster=payments,postgres-operator.crunchydata.com/role=master)
oc exec -n payments -c database "$POD" -- \
psql -d payments -c "CREATE TABLE probe (id int primary key); INSERT INTO probe VALUES (1);"
The primary Service is payments-primary. Applications use that DNS name. They do not use a pod IP.
Take a backup now, rather than waiting for the schedule. The annotation value only has to change each time you want a new one.
oc annotate -n payments postgrescluster payments --overwrite \
postgres-operator.crunchydata.com/pgbackrest-backup="$(date +%s)"
Watch that job finish before you build the standby. A standby can only replay WAL that reached the repository.
A pod dies on the DC
oc delete pod -n payments -l postgres-operator.crunchydata.com/role=master
oc get pods -n payments -l postgres-operator.crunchydata.com/role=master -w
Patroni elects one of the remaining instances. payments-primary follows it. The deleted pod returns as a replica and catches up. Run SELECT * FROM probe; on the new primary, the same way you inserted the row. Leave synchronous replication off: the default keeps accepting writes when a replica is down.
The DR standby
On the DR cluster, in namespace payments, apply the same secret, then a PostgresCluster that only replays. standby.enabled: true with repoName: repo1 is a repo-based standby: it restores from the bucket and keeps applying WAL as segments arrive. It does not need a route to the DC primary. A streaming standby (standby.host and standby.port, plus custom TLS) is the lower-lag option when the two sites can reach each other on 5432. Build the repo-based one first; it is the one that still works when they cannot.
apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
name: payments-dr
namespace: payments
spec:
postgresVersion: 16
instances:
- name: instance1
replicas: 2
dataVolumeClaimSpec:
accessModes: ["ReadWriteOnce"]
resources: {requests: {storage: 20Gi}}
backups:
pgbackrest:
configuration:
- secret: {name: payments-pgbackrest}
global:
repo1-path: /pgbackrest/payments/repo1
repo1-s3-uri-style: path
repos:
- name: repo1
s3:
bucket: pg-backups
endpoint: s3.grid.lab
region: us-east-1
standby:
enabled: true
repoName: repo1
Do not set dataSource. The standby block is what follows the repository. Copy the secret payments-pguser-app from the DC cluster if the application will connect here: the role is already inside the restored database, and defining users again on a read-only standby fights that. Two writers archiving to the same repo1-path make the repository unusable.
oc apply -f payments-dr.yaml
POD=$(oc get pod -n payments -o name \
-l postgres-operator.crunchydata.com/cluster=payments-dr,postgres-operator.crunchydata.com/role=master)
oc exec -n payments -c database "$POD" -- psql -d payments -c "SELECT pg_is_in_recovery();"
oc exec -n payments -c database "$POD" -- psql -d payments -c "TABLE probe;"
pg_is_in_recovery returns t. The role label still says master because that pod leads the standby. TABLE probe shows the row once the WAL segment has been archived and replayed. Compare pg_current_wal_lsn() on the DC primary with pg_last_wal_replay_lsn() on the standby. The open segment is the gap. An insert on the standby fails while recovery is on.
Promote
Do this only when the DC primary is stopped. If the DC site is gone, it is already stopped. If this is a drill, shut it down first. shutdown: true scales the DC workloads to zero. Wait until those pods are gone.
oc patch postgrescluster payments -n payments --type merge \
--patch '{"spec":{"shutdown": true}}'
# DR cluster. The DC workloads must already be scaled to zero.
oc patch postgrescluster payments-dr -n payments --type merge \
--patch '{"spec":{"standby":{"enabled": false}}}'
Wait for the pods to roll. pg_is_in_recovery() returns f. Insert INSERT INTO probe VALUES (2);. The DR cluster is the writer now, and it is archiving to the same repository. Leave the old DC cluster shut down. Starting it as a writer against that repo path splits the brain.
Returning is a rebuild. Take a backup on payments-dr first (the same pgbackrest-backup annotation). Then delete the shut-down DC cluster's data volumes and create it again with standby.enabled: true and the same repo1. The old data directory is a previous timeline; the repository is the copy you are keeping. Point applications at payments-dr-primary only after the new row commits.
What's next
StorageGRID is the bucket this repo uses, including object retention so a backup cannot be deleted early. ONTAP Select and the Trident series are the volumes under the Postgres pods. Trident data protection is the storage-level copy (SnapMirror, Trident Protect) and is a separate path from WAL replay. Redis in front of this database is the caching lab.