1. Answers
  2. Periodic Cleanup of Old Logs with Kubernetes CronJob

How do I set up a periodic cleanup of old logs with Kubernetes CronJob?

In this guide, we will set up a Kubernetes CronJob to periodically clean up old logs from a specific directory. A CronJob in Kubernetes allows you to run jobs on a time-based schedule, similar to cron jobs in Unix-like operating systems. This is useful for tasks like log rotation, backups, and other repetitive maintenance tasks.

The following Pulumi program will create a Kubernetes CronJob that runs a cleanup script every day at midnight. The script will delete log files older than 7 days from the /var/logs directory.

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

// Define the cleanup command
const cleanupCommand = [
    "sh",
    "-c",
    "find /var/logs -type f -mtime +7 -name '*.log' -exec rm {} \\;"
];

// Create the CronJob
const logCleanupCronJob = new k8s.batch.v1beta1.CronJob("logCleanupCronJob", {
    metadata: {
        name: "log-cleanup-cronjob",
    },
    spec: {
        schedule: "0 0 * * *", // Run every day at midnight
        jobTemplate: {
            spec: {
                template: {
                    spec: {
                        containers: [{
                            name: "log-cleanup",
                            image: "busybox",
                            command: cleanupCommand,
                        }],
                        restartPolicy: "OnFailure",
                    },
                },
            },
        },
    },
});

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

Key Points:

  • Kubernetes CronJob: The main resource used to schedule the periodic task.
  • Schedule: The schedule field uses cron syntax to define when the job should run. In this case, it runs daily at midnight (0 0 * * *).
  • Job Template: The jobTemplate field defines the job that will be run. Here, it uses a busybox container to execute a shell command that deletes logs older than 7 days.
  • Command: The command array specifies the shell command to find and delete old log files.

Summary

In this guide, we created a Kubernetes CronJob using Pulumi to automate the cleanup of old log files. The CronJob runs a busybox container daily at midnight to delete log files older than 7 days from the /var/logs directory. This setup helps in maintaining a clean log directory and preventing disk space issues caused by old log files.

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