How to create a cron job in Kubernetes

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.

In this tutorial you will learn:

  • How to create a cron job YAML file
  • How to schedule a cron job in Kubernetes
  • How to check on cron jobs in Kubernetes
How to create a cron job in Kubernetes
How to create a cron job in Kubernetes
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Kubernetes
Other 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.

  1. 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:
    apiVersion: batch/v1
    kind: CronJob
    metadata:
      name: hello-world
    spec:
      schedule: "* * * * *"
      jobTemplate:
        spec:
          template:
            spec:
              containers:
              - name: hello
                image: busybox:1.28
                imagePullPolicy: IfNotPresent
                command:
                - /bin/sh
                - -c
                - date; echo Hello Kubernetes!
              restartPolicy: OnFailure
    NOTE
    See our crontab reference guide for help with the scheduling syntax of cron, such as by minute, hour, day, etc. Crontab guru is also a handy way to quickly get the needed syntax for a specific time interval.
  2. Then, run the new cron job with the kubectl command:
    $ kubectl create -f ./cronjob.yaml
    cronjob.batch/hello-world created
    
  3. See the cronjob by executing:
    $ kubectl get cronjob hello-world
    

    Viewing our scheduled cron job in Kubernetes
    Viewing our scheduled cron job in Kubernetes



  4. To see the cron jobs being executed in real time, use the following command:
    $ kubectl get jobs --watch
    
    The --watch parameter allows us to see the cron jobs as they are being executed
    The –watch parameter allows us to see the cron jobs as they are being executed
  5. We can also view the pods which have been spawned to execute the cron job:
    $ kubectl get pods
    
    Viewing the pods that have been created to accommodate our cron jobs
    Viewing the pods that have been created to accommodate our cron jobs
  6. We can see the results of the cron job (in our case, just echoed text) by inspecting the logs for one of the pods:
    $ kubectl logs hello-world-27982521-bgb46
    
    Viewing the logs from one of the cron job pods
    Viewing the logs from one of the cron job pods
  7. 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.



Comments and Discussions
Linux Forum