Kubernetes CronJob – Effective for Iterative Jobs

Filed Under: Random
Kubernetes Cronjob

Hello, readers! In this article, we will be focusing on Kubernetes CronJob in detail.

So, let us begin!! 馃檪


What is a Kubernetes CronJob?

Kubernetes provides us with various resources such as deployments, pods, etc to have the data in place and the application as up and running plus being orchestrated by k8.

But, what if I have some redundant tasks within my application? Or to be more precise, what if I want my application to perform a certain task after every 2 hours? What can be the solution? Obviously, looking at the current knowledge of ours, we will need to run the application every two hours i.e. trigger the pipeline every two hours.

Any easy solution? Kubernetes is there for us!

Kubernetes provides us with CronJob to have things automated for us in a liable manner. That is, a CronJob is a k8 object that creates and triggers jobs on a repetitive schedule. It triggers and runs certain sets of job periodically over the estimated time bracket.

Remember, the kubernetes kube-controller-manager decides the time zone for the cronjob schedules. That is, the time zone of the schedules is truly dependent on the time zone in which the controller manager falls.

So, with CronJobs, we can create recurring tasks that can save the manpower and introduce efficiency within the system.

For example, we can use cronjobs to schedule a task which would eventually get the backups from the virtual machines every 24 hours.

Let us now focus on the creation of a CronJob in the upcoming section!


Creation of a Kubernetes CronJob

Have a look at the below CronJob YAML schema:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: demo
spec:
  schedule: "0 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: demo-app
            image: hello-world
          restartPolicy: OnFailure

  • We include the kind of the task as CronJob.
  • Further, the schedule key is used to set the time intervals for recursion of the tasks.
  • We also specify the Pod template within the CronJob to make the trigger aware of which task under the pool of Pods to be triggered.
  • The specification of the containers is also provided as a part of Pod template.

Understanding the scheduling of a CronJob

Let us now understand the syntax of a CronJob schedule:

Cron Schedule
CronJob Schedule

As seen above, it includes 5 asterisks ‘* * * * *’, out of which:

  1. The first * is the minute value which lies between 0-59
  2. The second * is the hour value which falls between 0-23
  3. Third being days of the month which is between 1-31
  4. Fourth * is the month value which falls between 1-12
  5. Finally the last (fifth) * is the day number which falls between 0-6 i.e. Sunday to Saturday.

In the above section’s example, we have scheduled a CronJob as 0 * * * * *, which simply means that the task would be triggered every one hour (at the beginning of an hour) for every day, every month, every day of the week.


Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question. For more such posts related to Kubernetes, stay tuned with us.

Till then, Happy Learning!! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content