Operations
Backup And Restores
Custom Secret
Monitoring
tpl
This guide demonstrates how to create and validate full backups for PostgreSQL clusters on KubeBlocks using the pg-basebackup
method through both:
We will cover how to restore data from a backup in the Restore From Full Backup guide.
Before proceeding, ensure the following:
kubectl create ns demo
namespace/demo created
KubeBlocks uses a declarative approach for managing PostgreSQL clusters. Below is an example configuration for deploying a PostgreSQL cluster with 2 replicas (1 primary, 1 replicas).
Apply the following YAML configuration to deploy the cluster:
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
name: pg-cluster
namespace: demo
spec:
terminationPolicy: Delete
clusterDef: postgresql
topology: replication
componentSpecs:
- name: postgresql
serviceVersion: 16.4.0
labels:
apps.kubeblocks.postgres.patroni/scope: pg-cluster-postgresql
disableExporter: true
replicas: 2
resources:
limits:
cpu: "0.5"
memory: "0.5Gi"
requests:
cpu: "0.5"
memory: "0.5Gi"
volumeClaimTemplates:
- name: data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
Monitor the cluster status until it transitions to the Running state:
kubectl get cluster pg-cluster -n demo -w
Expected Output:
NAME CLUSTER-DEFINITION TERMINATION-POLICY STATUS AGE
pg-cluster postgresql Delete Creating 50s
pg-cluster postgresql Delete Running 4m2s
Once the cluster status becomes Running, your PostgreSQL cluster is ready for use.
If you are creating the cluster for the very first time, it may take some time to pull images before running.
Before creating backups, ensure:
Backup repository is configured:
BackupRepo
resource existsBackupRepo
status shows "Ready"Cluster is ready:
Check available backup policies and schedules:
# List backup policies
kubectl get backuppolicy -n demo -l app.kubernetes.io/instance=pg-cluster
# List backup schedules
kubectl get backupschedule -n demo -l app.kubernetes.io/instance=pg-cluster
Expected Output:
NAME BACKUP-REPO STATUS AGE
pg-cluster-postgresql-backup-policy Available 58m
NAME STATUS AGE
pg-cluster-postgresql-backup-schedule Available 60m
View supported backup methods in the BackupPolicy CR 'pg-cluster-postgresql-backup-policy':
kubectl get backuppolicy pg-cluster-postgresql-backup-policy -n demo -oyaml | yq '.spec.backupMethods[].name'
List of Backup methods
KubeBlocks PostgreSQL supports these backup methods:
Feature | Method | Description |
---|---|---|
Full Backup | pg-basebackup | Uses pg_basebackup , a PostgreSQL utility to create a base backup |
Full Backup | wal-g | Uses wal-g to create a full backup (requires WAL-G configuration) |
Continuous Backup | postgresql-pitr | Uploads PostgreSQL Write-Ahead Logging (WAL) files periodically to the backup repository, usually paired with pg-basebackup |
Continuous Backup | wal-g-archive | Uploads PostgreSQL Write-Ahead Logging (WAL) files periodically to the backup repository, usually paired with wal-g |
The pg-basebackup
method uses PostgreSQL's native pg_basebackup
utility.
Apply this manifest to create a backup:
apiVersion: dataprotection.kubeblocks.io/v1alpha1
kind: Backup
metadata:
name: pg-cluster-pg-basebackup
namespace: demo
spec:
backupMethod: pg-basebackup
backupPolicyName: pg-cluster-postgresql-backup-policy
# Determines whether the backup contents stored in the backup repository should be deleted
# when the backup custom resource(CR) is deleted. Supported values are `Retain` and `Delete`.
# - `Retain` means that the backup content and its physical snapshot on backup repository are kept.
# - `Delete` means that the backup content and its physical snapshot on backup repository are deleted.
deletionPolicy: Delete
Track progress until status shows "Completed":
kubectl get backup pg-cluster-pg-basebackup -n demo -w
Example Output:
NAME POLICY METHOD REPO STATUS TOTAL-SIZE DURATION DELETION-POLICY CREATION-TIME COMPLETION-TIME EXPIRATION-TIME
pg-cluster-pg-basebackup pg-cluster-postgresql-backup-policy pg-basebackup <BACKUP_REPO> Completed 4722262 10s Delete 2025-05-16T02:53:45Z 2025-05-16T02:53:55Z
Confirm successful completion by checking:
The Backup
resource records details including:
Execute a backup using the OpsRequest API with the 'pg-basebackup' method:
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: pg-cluster-backup
namespace: demo
spec:
clusterName: pg-cluster
force: false
backup:
backupPolicyName: pg-cluster-postgresql-backup-policy
backupMethod: pg-basebackup
deletionPolicy: Delete
retentionPeriod: 1mo
type: Backup
Track backup progress in real-time:
kubectl get ops pg-cluster-backup -n demo -w
Expected Output:
NAME TYPE CLUSTER STATUS PROGRESS AGE
pg-cluster-backup Backup pg-cluster Succeed -/- 35s
Check the final backup status:
kubectl get backup -n demo -l operations.kubeblocks.io/ops-name=pg-cluster-backup
Example Output:
NAME POLICY METHOD REPO STATUS TOTAL-SIZE DURATION DELETION-POLICY CREATION-TIME COMPLETION-TIME EXPIRATION-TIME
backup-demo-pg-cluster-20250516025810 pg-cluster-postgresql-backup-policy pg-basebackup <BACKUP_REPO> Completed 4725590 10s Delete 2025-05-16T02:58:10Z 2025-05-16T02:58:20Z 2025-06-15T02:58:20Z
Confirm successful completion by checking:
The Backup
resource records details including:
This guide covered:
Your PostgreSQL data is now securely backed up and ready for restoration when needed.