Periodic Cleanup of Old Logs With Kubernetes CronJob
Introduction
In this guide, we will set up a Kubernetes CronJob using Pulumi to periodically clean up old logs. Kubernetes CronJobs are useful for running tasks at scheduled intervals, similar to cron jobs in Unix-like systems. We will use AWS and TypeScript as per the organization’s system prompts.
Step-by-Step Explanation
Step 1: Install Pulumi and Required Packages
Ensure you have Pulumi installed and set up. You will also need the Pulumi Kubernetes package.
npm install @pulumi/pulumi @pulumi/kubernetes
Step 2: Create a Pulumi Project
Create a new Pulumi project if you don’t already have one.
pulumi new typescript
Step 3: Define the Kubernetes CronJob
In your Pulumi program, define the Kubernetes CronJob resource. This will include the schedule and the job template for cleaning up old logs.
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
const cronJob = new k8s.batch.v1.CronJob("log-cleanup", {
metadata: {
name: "log-cleanup",
namespace: "default",
},
spec: {
schedule: "0 0 * * *", // Runs at midnight every day
jobTemplate: {
spec: {
template: {
spec: {
containers: [{
name: "log-cleanup",
image: "busybox",
command: ["sh", "-c", "find /var/log -type f -mtime +30 -exec rm -f {} \;"],
}],
restartPolicy: "OnFailure",
},
},
},
},
},
});
Step 4: Deploy the CronJob
Deploy the CronJob to your Kubernetes cluster.
pulumi up
Summary
In this guide, we created a Kubernetes CronJob using Pulumi to periodically clean up old logs. This involves defining the CronJob resource with the appropriate schedule and job template, and then deploying it to the Kubernetes cluster. By following these steps, you can automate log cleanup tasks efficiently.
Full Code Example
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
const cronJob = new k8s.batch.v1.CronJob("log-cleanup", {
metadata: {
name: "log-cleanup",
namespace: "default",
},
spec: {
schedule: "0 0 * * *", // Runs at midnight every day
jobTemplate: {
spec: {
template: {
spec: {
containers: [{
name: "log-cleanup",
image: "busybox",
command: ["sh", "-c", "find /var/log -type f -mtime +30 -exec rm -f {} \;"],
}],
restartPolicy: "OnFailure",
},
},
},
},
},
});
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.