JOBS & CRONJOBS IN KUBERNETES

Amit Sharma
6 min readNov 10, 2021

Hello guys, hope you are learning and implementing kubernetes in your domain. Back with another article that mainly focuses on jobs and cronjobs. Hope this article will helps you while learning jobs and cronjobs in kuberenetes.

𝐓𝐚𝐤𝐞 𝐚𝐰𝐚𝐲 𝐟𝐫𝐨𝐦 𝐭𝐡𝐢𝐬 𝐚𝐫𝐭𝐢𝐜𝐥𝐞:

1. What is a Job ?
2. How to create a job using kubectl ?
3. How we can run the job in sequence and what is Completions in job ?
4. How we can run the job parallelly and understanding parallelism in job ?
5. How we can suspending a particular job ?
6. What is CronJob ?
7. Difference between job and cronjob ?

What is jobs ?

A Job creates one or more Pods and will continue to retry execution of the Pods until a specified number of them successfully terminate. As pods successfully complete, the Job tracks the successful completions. When a specified number of successful completions is reached, the task (i.e, Job) is complete. Deleting a Job will clean up the Pods it created. Suspending a Job will delete its active Pods until the Job is resumed again.

1. Running a Job

First we will create a job using command line using

# creating a job and saving into myjob.yaml filekubectl create job myjob --image=centos:7 --dry-run -o yaml > myjob.yaml

After creating myjob.yaml you can launch the job using kubectl create -f myjob.yaml. You can find the detailed hand-on part in the below gif image.

2. Completions in Jobs:

A Job is completed when a specific number of Pods terminate successfully. By default, a non-parallel Job with a single Pod completes as soon as the Pod terminates successfully.

If you have a parallel Job, you can set a completion count using the optional completions field. This field specifies how many Pods should terminate successfully before the Job is complete. The completions field accepts a non-zero, positive value.

As you can find that i have just created a simple job that will complete the work and create a new job if its failed by any reason. But it will create only one job. If you had a requirement then you can create multiple jobs using completions keyword in myjob.yaml file. The completions will helps to launch as many jobs as we have requested.

apiVersion: batch/v1
kind: Job
metadata:
creationTimestamp: null
name: myjob1
spec:
completions: 6
template:
metadata:

Here i have set the completions=6 in the spec section of the job which will launch 6 jobs sequentially.

Execution:

3. Parallelism in Jobs:

By default, Job Pods do not run in parallel. The optional parallelism field specifies the maximum desired number of Pods a Job should run concurrently at any given time.

The actual number of Pods running in a steady state might be less than the parallelism value if the remaining work is less than the parallelism value. If you have also set completions, the actual number of Pods running in parallel does not exceed the number of remaining completions. A Job may throttle Pod creation in response to excessive Pod creation failure.

apiVersion: batch/v1
kind: Job
metadata:
creationTimestamp: null
name: myjob1
spec:
parallelism: 6
template:
metadata:

Here i have set the parallelism=6 in the spec section of the job which will launch 6 jobs parallelly.

Execution:

4. BackOffLimit in Jobs:

When a job fails repeatedly, it will eventually reach the configured backoffLimit. Once this limit is reached, the job will no longer be retried. The actual cause of failure for the job may be available in the logs of its pods, or in other events related to the job failure. If you wish to have a job that will retry indefinitely, you can set the restartPolicy to OnFailure. This will ensure the job restarts when it fails, and the backoffLimit will never be reached, essentially creating a Job that retries until successful.

We need to add backoffLimit in myjob.yaml file in the spec section.

apiVersion: batch/v1
kind: Job
metadata:
creationTimestamp: null
name: myjob1
spec:
backoffLimit: 4
template:
metadata:

As you see that i have set backoffLimit to 4. i.e., if the jobs fails more than 4 times then it will not launch 5th job. A demop you can see in the below gif.

Execution:

What is CronJobs?

CronJobs are meant for performing regular scheduled actions such as backups, report generation, and so on. Each of those tasks should be configured to recur indefinitely (for example: once a day / week / month); you can define the point in time within that interval when the job should start.

The job yaml file created using two ways.

  1. You can create using command line.
  2. You can edit the myjob.yaml file.

5. Creating cronjob using command line.

kubecvtl create cronjob mycronjob --image=centos:7 --schedule='* * * * *' --dry-run -o yaml > mycronjob.yaml

6. Creating cronjob by editing myjob.yaml file.

In the myjob.yaml file you have to edit the following things to create cronjob.

  1. Change the Kind name from Job to CronJob.
  2. Add a jobTemplate section in spec section.
spec:
jobTemplate: # changed
metadata:
creationTimestamp: null
name: mycronjob
spec:
template:
metadata:
creationTimestamp: null
spec:
containers:
- image: centos:7
name: mycronjob

3. As cronjob meant for running a job on a schedule. So, we need to add a schedule in the spec section of the cronjob.

spec:
schedule: "* * * * *" #changed
jobTemplate: # changed
metadata:
creationTimestamp: null
name: mycronjob
spec:

Execution:

A detailed demo you can find in the below gif image.

7. Suspending CronJob/Job:

While the amount of parallelism and the conditions for Job completion are configurable, the Kubernetes API lacked the ability to suspend and resume Jobs. This is often desired when cluster resources are limited and a higher priority Job needs to execute in the place of another Job. Deleting the lower priority Job is a poor workaround as Pod completion history and other metrics associated with the Job will be lost.

To suspend a job in cronjob we can use suspend=true in spec section of the mycronjob.yaml file.

spec:
suspend: true
schedule: "* * * * *" #changed
jobTemplate: # changed
metadata:
creationTimestamp: null
name: mycronjob
spec:

After creating the cronjob you can see that the suspend is true while displaying the output using kubectl get cronjob

Execution:

--

--

Amit Sharma

2x RedHat || 2x GCP Certified || DEVOPS Enginner || Terraform || Ansible || Kubernetes || AWS || GCP || GitLab || Jenkins|| Openshift