Kubegres allows to back-up the Primary PostgreSql database to a given volume in a scheduled way.
If the back-up feature is enabled in Kubegres YAML, behind the scenes a Kubernetes Cronjob resource is created which regularly executes a backup bash script. It is possible to override that bash script to have a better control of the backup process, as explained in the doc: Override the default configurations.
Create a PVC
If you do not have a PVC to store the contents to back-up, you can follow the example below.
Create a file:
vi my-backup-pvc.yamlAdd the following contents:
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-backup-pvc namespace: default spec: storageClassName: "standard" accessModes: - ReadWriteOnce resources: requests: storage: 20MiApply the changes:
kubectl apply -f my-backup-pvc.yaml
Enable the backup feature
Once the PVC is created, to enable the backup feature for a Postgres cluster, add the property "backup" to its Kubegres YAML, as in the example below:
backup: schedule: "0 */1 * * *" pvcName: my-backup-pvc volumeMount: /var/lib/backup
The above config has the following properties:
- "backup.schedule: "0 */1 * * *" : it schedules a back-up every hour on the hour. Its format follows the standard Cron spec.
- "backup.pvcName: my-backup-pvc" : the Persistence-Volume-Claim (PVC) defining where the back-up data will be stored. A volume backed by a PVC is mounted into the path defined by "backup.volumeMount" (see below).
- "backup.volumeMount: /var/lib/backup" : the location where the backup is stored in the docker image. A volume is mounted on that path using the PVC name defined by "database.pvcName".
Please see below an example of a full Kubegres YAML config with those back-up properties:
apiVersion: kubegres.reactive-tech.io/v1 kind: Kubegres metadata: name: mypostgres namespace: default spec: replicas: 3 image: postgres:14.1 port: 5432 database: size: 200Mi backup: schedule: "0 */1 * * *" pvcName: my-backup-pvc volumeMount: /var/lib/backup env: - name: POSTGRES_PASSWORD valueFrom: secretKeyRef: name: mySecretResource key: superUserPassword - name: POSTGRES_REPLICATION_PASSWORD valueFrom: secretKeyRef: name: mySecretResource key: replicationUserPassword
Check the backup is workingOnce you applied the YAML changes above, you can monitor the CronJob as follows:
$ kubectl get CronJob NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE backup-mypostgres */1 * * * * False 0 15s 14mEach time a backup job is running, Kubernetes will create a Pod in the same namespace as your cluster of Postgres. You can check the logs of those pods to see if a backup task run successfully.
Example of backup jobs:
$ kubectl get pods NAME READY STATUS RESTARTS AGE backup-mypostgres-27223836-25lzt 0/1 Completed 0 2m31s backup-mypostgres-27223837-bshqr 0/1 Completed 0 91s ...Checking the logs of a backup job:
$ kubectl logs backup-mypostgres-27223836-25lzt 05/10/2021 10:36:00 - Starting DB backup of Kubegres resource mypostgres into file: /var/lib/backup/mypostgres-backup-05_10_2021_10_36_00.gz 05/10/2021 10:36:00 - Running: pg_dumpall -h mypostgres-replica -U postgres -c | gzip > /var/lib/backup/mypostgres-backup-05_10_2021_10_36_00.gz 05/10/2021 10:36:00 - DB backup completed for Kubegres resource mypostgres into file: /var/lib/backup/mypostgres-backup-05_10_2021_10_36_00.gz