1. Answers
  2. Scheduling PostgreSQL backups in Kubernetes with CronJobs

How do I schedule PostgreSQL backups in Kubernetes with CronJobs?

To schedule PostgreSQL backups in Kubernetes, we can use a Kubernetes CronJob. A CronJob allows us to run jobs on a time-based schedule, similar to cron jobs in Unix-like operating systems. We will create a CronJob that periodically runs a backup script to dump the PostgreSQL database and store the backup in a persistent volume.

Here is a detailed explanation of what we will do:

  1. Create a PersistentVolumeClaim (PVC): This will be used to store the PostgreSQL backups.
  2. Create a ConfigMap: This will store the backup script.
  3. Create a CronJob: This will run the backup script at specified intervals.

Step-by-Step Explanation:

  1. PersistentVolumeClaim (PVC): This is used to request storage resources in Kubernetes. We will use it to store our PostgreSQL backups.

  2. ConfigMap: This is used to store configuration data in key-value pairs. We will use it to store our backup script.

  3. CronJob: This is used to schedule jobs in Kubernetes. We will create a CronJob that runs the backup script periodically.

The Program:

Below is the Pulumi program in TypeScript to set up the above resources.

import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

// Create a PersistentVolumeClaim for storing PostgreSQL backups
const backupPvc = new k8s.core.v1.PersistentVolumeClaim("backup-pvc", {
    metadata: {
        name: "backup-pvc",
    },
    spec: {
        accessModes: ["ReadWriteOnce"],
        resources: {
            requests: {
                storage: "5Gi",
            },
        },
    },
});

// Create a ConfigMap to store the backup script
const backupScriptConfigMap = new k8s.core.v1.ConfigMap("backup-script-configmap", {
    metadata: {
        name: "backup-script-configmap",
    },
    data: {
        "backup.sh": `
        #!/bin/bash
        PGPASSWORD=$POSTGRES_PASSWORD pg_dump -h $POSTGRES_HOST -U $POSTGRES_USER $POSTGRES_DB > /backup/postgres-backup-$(date +\\%Y\\%m\\%d\\%H\\%M\\%S).sql
        `,
    },
});

// Create a CronJob to run the backup script periodically
const backupCronJob = new k8s.batch.v1.CronJob("backup-cronjob", {
    metadata: {
        name: "backup-cronjob",
    },
    spec: {
        schedule: "0 0 * * *", // Runs every day at midnight
        jobTemplate: {
            spec: {
                template: {
                    spec: {
                        containers: [{
                            name: "backup",
                            image: "postgres:13", // Use the PostgreSQL image
                            command: ["/bin/bash", "/scripts/backup.sh"],
                            env: [
                                { name: "POSTGRES_HOST", value: "your-postgres-host" },
                                { name: "POSTGRES_DB", value: "your-database-name" },
                                { name: "POSTGRES_USER", value: "your-database-user" },
                                { name: "POSTGRES_PASSWORD", value: "your-database-password" },
                            ],
                            volumeMounts: [
                                { name: "backup-storage", mountPath: "/backup" },
                                { name: "backup-script", mountPath: "/scripts" },
                            ],
                        }],
                        restartPolicy: "OnFailure",
                        volumes: [
                            { name: "backup-storage", persistentVolumeClaim: { claimName: backupPvc.metadata.name } },
                            { name: "backup-script", configMap: { name: backupScriptConfigMap.metadata.name } },
                        ],
                    },
                },
            },
        },
    },
});

// Export the name of the CronJob
export const cronJobName = backupCronJob.metadata.name;

Explanation:

  1. PersistentVolumeClaim (PVC):

    • We create a PVC named backup-pvc with a storage request of 5Gi. This PVC will be used to store the PostgreSQL backups.
  2. ConfigMap:

    • We create a ConfigMap named backup-script-configmap which contains the backup script backup.sh. This script uses pg_dump to create a backup of the PostgreSQL database.
  3. CronJob:

    • We create a CronJob named backup-cronjob that runs every day at midnight (schedule: "0 0 * * *").
    • The CronJob runs a container using the postgres:13 image and executes the backup script.
    • Environment variables for PostgreSQL connection details are set.
    • The backup-storage volume mounts the PVC to /backup inside the container.
    • The backup-script volume mounts the ConfigMap to /scripts inside the container, making the backup script available for execution.

This setup ensures that PostgreSQL backups are taken daily and stored in a persistent volume in your Kubernetes cluster.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up