Easily Create Kubernetes k8s CronJob|CronTab with Kubectl

Posted April 24, 2024
Easily Create Kubernetes k8s CronJob|CronTab with Kubectl

Get ready to Set up cron jobs in Kubernetes using CronJobs and automate tasks within your cluster. Sometimes, having to rerun a Kubernetes task is tiresome. Imagine having to open and cess your Kubernetes terminal just to run a recurrent task. This is where Kubernetes k8s and CronJob|CronTab comes in.

Kubernetes uses CronJobs to schedule and automate the execution. A Kubernetes CronJob works similarly to traditional cron jobs in Unix-like operating systems. This way you run Jobs/one-time tasks periodically, but based on a schedule.

CronJobs in Kubernetes are objects that create Jobs on a schedule. They use a format similar to traditional cron jobs to define when to execute a task. For CronJob to work Kubernetes will treat a CronJobs as an object. This object will create Jobs on a schedule to execute a task.

Are you ready? Dive into Complete Guide to Kubernetes CronJob|CronTab Step-by-Step and learn:

  • How to create Kubernetes CronJob|CronTab
  • Handling Completions and failures to CronJobs
  • How to automatically delete completed CronJobs

Understanding CronJobs and Kubernetes

CronJobs are objects in Kubernetes. They automate the execution of Jobs on a schedule. For CronJob to work, Kubernetes created Kubernetes cronjob objects with schedule expression

A cron format defines a schedule expression field representing minute, hour, day of the month, month, and day of the week. In this case, you might say, creating a kubernetes cronjob schedule every 5 minutes. This way your task will get scheduled to run every 5 minutes

The Key Key Components of Kubernetes CronJobs

Kubernetes uses jobTemplate in your deployments. This way, you create the spec.jobTemplate.spec object to specify how the CronJob handles executions.

To let Kubernetes run the CronJob, spec.schedule creates the first expression based on the CronJob time intervals. The spec.jobTemplate.spec.template.spec.containers will create the container(s) to run in the job. This way you will have:

  • Schedule to set when the CronJob should run in Cron format.
  • Job Template as the task (Job) to be executed.
  • Concurrency Policy to decide what happens if a new job is scheduled while the previous one is still running.
  • Starting Deadline Seconds to determine how long Kubernetes should wait for a Job to start before considering it a failure.

Let’s practically use this example to get Kubernetes CronJobs up and running.

Understanding Kubernetes Cron Schedule Format

Before you create a Kubernetes CronJob make sure you know the following five fields in a Cron schedule format:

  • Minute (0-59)
  • Hour (0-23)
  • Day of the month (1-31)
  • Month (1-12)
  • Day of the week (0-6, with 0 representing Sunday)

Creating your First Kubernetes CronJob

Just like any other Kubernetes YAML manifest, you create your instructions but this time with a CronJob as your Kubernetes API version kind. Go ahead and create a cronjob.yml file as follows:

apiVersion: batch/v1
kind: CronJob
metadata:
 name: nginx-cronjob
spec:
 schedule: "*/5 * * * *"       # Run every 5 minutes
 jobTemplate:
  spec:
   template:
    spec:
     containers:
     - name: nginx-container
      image: nginx         # NGINX image
      ports:
      - containerPort: 80     # Expose port 80
     restartPolicy: OnFailure

At this point, you just need to run this manifest file and create a CronJob:

kubectl apply -f cronjob.yml
cronjob.batch/nginx-cronjob created

Easily Create Kubernetes k8s CronJob|CronTab with Kubectl

Make sure you check and ensure the CronJob is created:

kubectl get cronjobs

NAME      SCHEDULE   SUSPEND ACTIVE LAST SCHEDULE AGE
nginx-cronjob */5 * * * * False  0    <none>     9s 

Easily Create Kubernetes k8s CronJob|CronTab with Kubectl

To confirm if the CronJob is working, re-run kubectl get cronjobs and check the ACTIVE and LAST SCHEDULE to know how many times the crons have executed and when last respectively:

NAME      SCHEDULE   SUSPEND ACTIVE LAST SCHEDULE AGE 
nginx-cronjob */5 * * * * False  2    20s      6m43s

A better way is to use the kubectl get jobs --watch command to catch the exact time the Kubernetes CronJob is executed:

Easily Create Kubernetes k8s CronJob|CronTab with Kubectl

Easily Create Kubernetes k8s CronJob|CronTab with Kubectl

A Detailed Kubernetes CronJob

You just create a basic Kubernetes CronJob. However, there many components that a CronJob will need to execute at optimal levels. Consider Concurrency Policy.

A CronTab runs based Concurrency Policy. Concurrency policy dictates how your Kubernetes cluster handles multiple executions of the same CronJob. Give that. the previous one hasn’t finished yet. In this case, you have three options:

  • Allow concurrent executions of the same job.
  • Forbid to Forbids concurrent executions; if a job is already running, the new one is skipped.
  • Replace to cancel the previous job if a new one is scheduled.

At the same time, these policies work well with completions, This way, you define the Job Completions and Failures with spec.jobTemplate.spec.completion and spec.jobTemplate.spec.failedJobsHistoryLimit.

For example:

  • The total successful completions required before marking the CronJob as complete.
  • The number of failed jobs to retain in the history is added using the failedJobsHistoryLimit object.

Another option you need is the startingDeadlineSeconds option to determine how long Kubernetes should wait for the job to start before considering it failed.

To represent these options, you will update your cronjob.yml file as such:

apiVersion: batch/v1
kind: CronJob
metadata:
 name: nginx-cronjob
spec:
 schedule: "*/5 * * * *" # Run every 5 minutes
 concurrencyPolicy: Forbid  # Forbid concurrent executions
 startingDeadlineSeconds: 300 # Wait 5 minutes for the job to start
 failedJobsHistoryLimit: 5 # Retain up to 5 failed job history
 jobTemplate:
  spec:
   completions: 3 # Expect 3 successful completions
   template:
    spec:
     containers:
     - name: nginx-container
      image: nginx # NGINX image
      ports:
      - containerPort: 80 # Expose port 80
      # Command and args for your task
     restartPolicy: OnFailure

Now you have:

  • Concurrency Policy as:
spec:
 concurrencyPolicy: Allow # Allows concurrent executions of the same job
# concurrencyPolicy: Forbid # Forbids concurrent executions
# concurrencyPolicy: Replace # Cancels the previous job if a new one is scheduled.
  • Starting Deadline Seconds with specific duration in second:
spec:
 startingDeadlineSeconds: 300
  • Job Completions with successful completions are required before marking the CronJob as complete
spec:
 jobTemplate:
  spec:
   completions: 3
  • Failed Jobs History Limit
spec:
 failedJobsHistoryLimit: 5

How to Delete the CronJob

If you run the following command, you will get a list of Kubernetes CronJobs overtime:

kubectl get jobs --watch

Easily Create Kubernetes k8s CronJob|CronTab with Kubectl

They will pile up as time goes on. At this point, you need to prevent CronJobs from consuming excessive resources. This way, you use ttlSecondsAfterFinished to add how long completed jobs should be retained before being automatically deleted.

spec:
 ttlSecondsAfterFinished: 3600

This way, once a Job created by your CronJob is completed, it will be retained for 1 hour before being automatically deleted (3600 seconds = 1 hour).

Conclusion

Kubernetes CronJobs automate repetitive tasks within your cluster. I hope you have learned:

  • How to create Kubernetes CronJob|CronTab
  • Handling Completions and failures to CronJobs
  • How to automatically delete completed CronJobs
Easily Create Kubernetes k8s CronJob|CronTab with Kubectl

Written By:

Joseph Chege