Skip to main content
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.

Converting a Basic Job to a CronJob

Consider the following basic Job template:
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:

Creating and Verifying Your CronJob

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:
To verify that your CronJob has been created correctly, run:
A typical output might resemble:
Ensure your YAML file is properly formatted and saved before executing the kubectl create command to avoid any deployment issues.

Conclusion

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: Happy scheduling and see you in the next article!

Watch Video

Practice Lab