git clone https://github.com/cloudnative-pg/cnpg-playground.git
cd cnpg-playground
REGISTRY_CACHE=true ./scripts/setup.sh
export KUBECONFIG=$PWD/k8s/kube-config.yaml
k8s-eu, k8s-na), one per "region", each with a RustFS S3-compatible object storekubectl, the cnpg plugin, Kind and jq (optional: Headlamp Desktop)REGISTRY_CACHE=true runs pull-through registry caches

main, both regionsgit checkout main && git pull
TRUNK=true ./demo/setup.sh
TRUNK=true: CloudNativePG and the Barman Cloud plugin from main
pg-eu (primary) in the EU cluster and pg-na (replica cluster) in the NA clusterpg-eu, pg-na and the objectstore-eu / objectstore-na object stores belong to the demoapp database as much as you likepg-eu
pg-na follows it, and the demo can't be redeployed quicklypgWhat do you expect from this workshop,
and what would you like to cover?

cloudnative-pg, api, machinerypostgres-containers, postgis-containers, pgbouncer-containers
postgres-extensions-containers, artifacts (image catalogs)cnpg-i, plugin-barman-cloud, kliocharts, grafana-dashboards, cnpg-playground, cnpg-headlamp-plugindocs, governance, artworkCluster references them by name (CNPG-I)Cluster, Pooler, Backup, ScheduledBackup and Database resourcespsql terminal and live metricscnpg-headlamp-pluginkubectl./scripts/info.sh)KUBECONFIG is set and kubectl --context kind-k8s-eu get nodes shows six Ready nodeskubectl get pods -n cnpg-system)pg-eu and pg-na are healthy (kubectl cnpg status pg-eu)kubectl config use-context kind-k8s-euCluster resource describes the desired stateapiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: pg
spec:
instances: 2
storage:
size: 1Gi
kubectl apply -f pg.yaml
kubectl cnpg status pg
pg now to prepare for the high availability section
pg-eu cluster insteadkubectl apply view-last-applied cluster pg-eu
kubectl get cluster pg-eu -o yaml
kubectl get mutatingwebhookconfigurations
pg-eukubectl get pods -o wide -l cnpg.io/cluster=pg-eu \
-L cnpg.io/instanceRole,topology.kubernetes.io/zone
kubectl get pvc,services,endpointslices,secrets,pdb -l cnpg.io/cluster=pg-eu
cnpg.io/cluster=<cluster name>: use -l to select itpg-eukubectl cnpg psql pg-eu -- -c '\du' -c '\l'
kubectl run psql --rm -it --restart=Never \
--image=ghcr.io/cloudnative-pg/postgresql:18-minimal-trixie -- \
psql "$(kubectl get secret pg-eu-app -o jsonpath='{.data.uri}' | base64 -d)"
kubectl cnpg logs cluster pg-eu -f | kubectl cnpg logs pretty
psql inside the primary's Pod, so it requires pods/exec permissionpg-eu-rw Service, like an application would
pg-eu-app Secretkubectl cnpg status pg
kubectl get pods,pvc,services,secrets -l cnpg.io/cluster=pg-eu
pg is a healthy two-instance clusterpg-eu
psql Pod of the previous exercisepg-eughcr.io/cloudnative-pg/postgresql
18-minimal-trixie and 18-standard-trixieamd64 and arm64ClusterImageCatalog maps a major version to an image
kubectl get clusterimagecatalogs.postgresql.cnpg.io
kubectl get clusterimagecatalog postgresql-minimal-trixie -o yaml
postgresql-minimal-trixie image catalog
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: pgvector
spec:
imageCatalogRef:
apiGroup: postgresql.cnpg.io
kind: ClusterImageCatalog
name: postgresql-minimal-trixie
major: 18
instances: 1
storage:
size: 1Gi
postgresql:
extensions:
- name: pgvector
kubectl apply -f pgvector-cluster.yaml
app database (pgvector-database.yaml)apiVersion: postgresql.cnpg.io/v1
kind: Database
metadata:
name: pgvector-app
spec:
name: app
owner: app
cluster:
name: pgvector
extensions:
- name: vector
version: '0.8.7'
kubectl apply -f pgvector-database.yaml
kubectl cnpg status pgvector
kubectl cnpg psql pgvector -- app -c '\dx'
pgvector is a healthy clustervector extension is installed in the app databaseLease named after the cluster: whoever holds it runs as the primarypgkubectl get lease -w pg
kubectl cnpg logs cluster pg -f | kubectl cnpg logs pretty
kubectl cnpg status pg
kubectl cnpg promote pg <replica-pod-name>
kubectl cnpg status pg
kubectl get lease pg -o jsonpath='{.metadata.annotations.cnpg\.io/position}'
Lease, and when did it change?cnpg.io/position annotation: LSN, timeline, state and Pod. What happened to the timeline?kubectl cnpg status pg
kubectl delete pod --now -l cnpg.io/cluster=pg,cnpg.io/instanceRole=primary
kubectl cnpg status pg
Lease now, and when did it change?kubectl cnpg status pg
kubectl get lease pg
Lease holder is the current primarykubectl get objectstore objectstore-eu -o yaml
kubectl get cluster pg-eu -o json | jq '.spec.plugins'
pg-eu point at the Barman Cloud plugin?serverName?pg-eu (imperative backups)kubectl cnpg backup pg-eu --method=plugin \
--plugin-name=barman-cloud.cloudnative-pg.io
kubectl get backup -w
completed before the next exercise--dry-run to the first command to see the Backup manifest
app database of pg-eukubectl cnpg psql pg-eu -- app -c 'create table t (id int)'
kubectl cnpg psql pg-eu -- app -c 'insert into t values (1), (2)'
kubectl cnpg psql pg-eu -- app -c 'select now()'
kubectl cnpg psql pg-eu -- app -c 'delete from t where id = 2'
kubectl cnpg psql pg-eu -- app -c 'select pg_switch_wal()'
id = 2 is the "mistake"pg_switch_wal() closes the current WAL segment
pg-recall, recovered to the noted time (abridged: full manifest)spec:
bootstrap:
recovery:
source: pg-eu
recoveryTarget:
targetTime: "<the time you noted>"
externalClusters:
- name: pg-eu
plugin:
name: barman-cloud.cloudnative-pg.io
parameters:
barmanObjectName: objectstore-eu
serverName: pg-eu
# first save the full manifest and replace targetTime with the time you noted
kubectl apply -f pg-recall.yaml
kubectl get cluster pg-recall -w
kubectl get backup pg-eu-backup
kubectl cnpg status pg-eu
kubectl cnpg status pg-recall
kubectl cnpg psql pg-recall -- app -c 'select * from t'
completedpg-eu workspg-recall recovered and is healthyid = 2) is back in pg-recall: both rows are therekubectl --context kind-k8s-eu get cluster pg-eu -o jsonpath='{.spec.replica}'
kubectl --context kind-k8s-na get cluster pg-na -o jsonpath='{.spec.replica}'
kubectl --context kind-k8s-na cnpg status pg-na
pg-na get its data from?kubectl --context kind-k8s-na cnpg status pg-na
pg-na follows pg-eu from the second regionDatabaseRole that creates and rotates its own password (abridged: full manifest)spec:
cluster:
name: pg
name: angus
login: true
password:
mode: generate
duration: 720h
renewBefore: 24h
databaseRoleReclaimPolicy: delete
kubectl apply -f angus-role.yaml
kubectl get secret angus-password
angus-password Secret and rotates it before it expireskubectl annotate databaserole angus cnpg.io/rotatePassword=nowLease hardening for failover and switchover (#11721, in progress)

Build, operate and scale production-grade databases with CloudNativePG
Gabriele Bartolini and Leonardo Cecchi, Packt
# either: remove the clusters, keep the registry caches
./scripts/teardown.sh
# or: remove the clusters and the registry caches
REMOVE_REGISTRY_CACHE=true ./scripts/teardown.sh
ClusterLease
