How Do I Schedule PostgreSQL Backups in Kubernetes With CronJobs?
Introduction
In a Kubernetes environment, automating database backups is crucial for ensuring data safety and availability. This guide demonstrates how to use Kubernetes CronJobs
to schedule regular backups of a PostgreSQL database. By the end of this guide, you will have a setup where backups are automatically created and stored in a persistent volume, ensuring your data is secure and recoverable.
Step-by-Step Explanation
To achieve automated PostgreSQL backups, we will set up three main components:
- PersistentVolumeClaim (PVC): This component will be used to store the PostgreSQL backups securely.
- ConfigMap: This will hold the backup script necessary for executing the database dump.
- CronJob: This schedules and runs the backup script at defined intervals.
Detailed Steps:
PersistentVolumeClaim (PVC):
- The PVC is used to request storage resources within Kubernetes. It is configured to store the backup files generated by the PostgreSQL dump.
ConfigMap:
- This stores configuration data as key-value pairs. Here, it contains the backup script that performs the database dump using
pg_dump
.
- This stores configuration data as key-value pairs. Here, it contains the backup script that performs the database dump using
CronJob:
- The CronJob is configured to execute the backup script periodically. It uses a container running the PostgreSQL image to perform the backup operation.
The Program
Below is the Pulumi program in TypeScript that sets up the necessary resources for scheduling PostgreSQL backups.
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;
Key Points
- Persistent Storage: The PVC ensures that backups are stored persistently and securely within the Kubernetes cluster.
- Automated Scheduling: The CronJob allows for automated execution of backups without manual intervention.
- Script Management: ConfigMap efficiently handles the backup script, making it easily manageable and updatable.
Conclusion
By setting up a Kubernetes CronJob
, you can automate the process of backing up PostgreSQL databases. This method not only saves time but also ensures that your data is regularly backed up and stored securely. With the use of PVCs, ConfigMaps, and CronJobs, you can maintain a robust backup strategy within your Kubernetes environment.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.