This article provides a guide on scheduling and executing recurring jobs in Kubernetes using CronJobs.
Welcome to this comprehensive guide on CronJobs in Kubernetes. In this article, you’ll learn how to schedule and execute recurring jobs in your Kubernetes cluster, similar to using a Cron tab in Linux.A CronJob allows you to run tasks on a recurring schedule. For example, you might have a job that generates reports and sends emails. Rather than triggering the job immediately with the “kubectl create” command, you can schedule it with a CronJob to occur periodically.
To convert this into a CronJob, you need to update the API version to “batch/v1beta1” and change the kind to “CronJob”. Then, add a schedule field with a Cron-formatted string, and embed the original Job template within the CronJob definition under the jobTemplate section.
The schedule field uses a standard Cron format. In this example, "*/1 * * * *" schedules the job to run every minute.
Below is the complete YAML definition for a CronJob that runs every minute:
After saving your CronJob definition (for example, as cron-job-definition.yaml), you can create the CronJob in your Kubernetes cluster with the following command:
Copy
Ask AI
kubectl create -f cron-job-definition.yaml
To verify that your CronJob has been created correctly, run:
With this guide, you now understand how to transform a standard Kubernetes Job into a CronJob, enabling you to automate and schedule recurring tasks. Practice with different scheduling configurations to get the most out of CronJobs in your Kubernetes environment.For further reading, explore additional resources: