Application Consistent PostgreSQL Backup

If it hasn't been done already, the bitnami Helm repository needs to be added to your local configuration:

# Add bitnami helm repo
$ helm repo add bitnami https://charts.bitnami.com/bitnami

Install the PostgreSQL chart from the bitnami Helm repository:

$ kubectl create namespace postgresql
$ helm install --namespace postgresql postgres bitnami/postgresql --version 11.9.13

Note

This is an example workflow that has been validated with PostgreSQL chart version 11.9.13. For different versions of PostgreSQL and other requirements, modify the postgresql-hooks.yaml blueprint below as required.

Next create a file postgresql-hooks.yaml with the following contents

apiVersion: cr.kanister.io/v1alpha1
kind: Blueprint
metadata:
  name: postgresql-hooks
actions:
  backupPrehook:
    phases:
    - func: KubeExec
      name: makePGCheckPoint
      args:
        namespace: "{{ .StatefulSet.Namespace }}"
        pod: "{{ index .StatefulSet.Pods 0 }}"
        container: postgresql
        command:
        - bash
        - -o
        - errexit
        - -o
        - pipefail
        - -c
        - |
          export POSTGRES_USER='postgres'
          PGPASSWORD=${POSTGRES_PASSWORD} psql -U $POSTGRES_USER -c "select pg_start_backup('app_cons');"
  backupPosthook:
    phases:
    - func: KubeExec
      name: afterPGBackup
      args:
        namespace: "{{ .StatefulSet.Namespace }}"
        pod: "{{ index .StatefulSet.Pods 0 }}"
        container: postgresql
        command:
        - bash
        - -o
        - errexit
        - -o
        - pipefail
        - -c
        - |
          export POSTGRES_USER='postgres'
          PGPASSWORD=${POSTGRES_PASSWORD} psql -U $POSTGRES_USER -c "select pg_stop_backup();"

And then apply the file using:

$ kubectl --namespace=kasten-io create -f postgresql-hooks.yaml

Finally add the following annotation to the PostgreSQL StatefulSets to instruct K10 to use the above hooks when performing operations on this PostgreSQL instance.

$ kubectl annotate statefulset postgres-postgresql kanister.kasten.io/blueprint='postgresql-hooks' \
     --namespace=postgresql

Note

The PostgreSQL backup example provided above serves as a blueprint template for achieving application-consistent backups. Please note that these examples may need to be modified for specific production environments and setups. As a result, it is highly recommended to carefully review and modify the blueprints as needed before deploying them for production use.

Finally, use K10 to backup and restore the application.