The cron scheduler in Kubernetes works very similarly to that of a typical Linux system. This should make it a bit easier for seasoned Linux users that have done their share of crontab editing in the past. However, there is still a specific way to create cron jobs in Kubernetes and a syntax that your YAML file must follow.
In this tutorial, we will see how to create a cron job in Kubernetes. This is a handy feature that allows us to schedule and automate tasks at certain intervals or future dates. Those already familiar with Linux crontab will have an advantage since they already know the general syntax of a cron command. Letβs see how to schedule jobs in Kubernetes.
Privileged access to your Linux system as root or via the sudo command.
Conventions
# β requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command $ β requires given linux commands to be executed as a regular non-privileged user
Create a cron job in Kubernetes
These steps assume that you already have your Kubernetes cluster up and running, and have access to the kubectl command.
Cron jobs are managed by the Kubernetes control plane. We can program a new job to run by applying a YAML file. An example syntax looks like the following:
To delete the cron job, use the delete parameter with kubectl:
$ kubectl delete cronjob hello-world
Closing Thoughts
In this tutorial, we saw how to create a cron job for Kubernetes on a Linux system. As seen here, the process is not very different from creating an ordinary cron job on Linux, with the only difference being the YAML file and the kubectl command. Kubernetes even gives us an easy way to check on all of our cron jobs and see the results after they have been executed.