1. Answers
  2. Managing MongoDB backups with CronJobs in Kubernetes

How do I manage MongoDB backups with CronJobs in Kubernetes?

In this guide, we will automate the process of taking MongoDB backups using Kubernetes CronJobs. CronJobs in Kubernetes allow us to schedule jobs that run periodically at fixed times, dates, or intervals. This is particularly useful for tasks like database backups that need to be performed regularly.

We will create a Kubernetes CronJob resource that runs a MongoDB backup command at a scheduled interval. The CronJob will use a container image that includes the necessary tools to perform the MongoDB backup.

Key Points

  • We will define a CronJob resource in Kubernetes.
  • The CronJob will run a MongoDB backup command at a specified schedule.
  • The backup will be stored in a persistent volume.

Steps

  1. Create a PersistentVolume and PersistentVolumeClaim to store the backups.
  2. Define the CronJob resource with the backup command.
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

// Create a PersistentVolume to store backups
const pv = new k8s.core.v1.PersistentVolume("mongo-backup-pv", {
    spec: {
        capacity: {
            storage: "10Gi",
        },
        accessModes: ["ReadWriteOnce"],
        persistentVolumeReclaimPolicy: "Retain",
        hostPath: {
            path: "/mnt/data",
        },
    },
});

// Create a PersistentVolumeClaim to claim the PersistentVolume
const pvc = new k8s.core.v1.PersistentVolumeClaim("mongo-backup-pvc", {
    spec: {
        accessModes: ["ReadWriteOnce"],
        resources: {
            requests: {
                storage: "10Gi",
            },
        },
    },
});

// Create a CronJob to run the MongoDB backup
const cronJob = new k8s.batch.v1beta1.CronJob("mongo-backup-cronjob", {
    spec: {
        schedule: "0 2 * * *", // Run daily at 2 AM
        jobTemplate: {
            spec: {
                template: {
                    spec: {
                        containers: [
                            {
                                name: "mongo-backup",
                                image: "bitnami/mongodb:latest", // MongoDB image with backup tools
                                command: [
                                    "sh",
                                    "-c",
                                    "mongodump --archive=/backup/mongo-backup-$(date +\\%F-\\%T).gz --gzip --uri=mongodb://<username>:<password>@<mongodb-host>:<mongodb-port>/<database>"
                                ],
                                volumeMounts: [
                                    {
                                        name: "backup-storage",
                                        mountPath: "/backup",
                                    },
                                ],
                            },
                        ],
                        restartPolicy: "OnFailure",
                        volumes: [
                            {
                                name: "backup-storage",
                                persistentVolumeClaim: {
                                    claimName: pvc.metadata.name,
                                },
                            },
                        ],
                    },
                },
            },
        },
    },
}, { dependsOn: [pv, pvc] });

Summary

In this guide, we created a Kubernetes CronJob to automate MongoDB backups. We defined a PersistentVolume and PersistentVolumeClaim to store the backups and configured the CronJob to run a MongoDB backup command at a scheduled interval. This setup ensures that MongoDB backups are taken regularly and stored securely.

By using Kubernetes CronJobs, we can automate repetitive tasks and ensure that critical operations like database backups are performed consistently.

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