Jobs & Cronjobs in Kubernetes Cluster

Kubernetes Advocate
Mar 22 · 4 min read
Jobs & Cron jobs

In this article, we will learn how to schedule jobs and cronjobs to Kubernetes Cluster.

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 are completed, the Job tracks the successful completions. When a specified number of successful completions is reached, the task (ie, Job) is complete. Deleting a Job will clean up the Pods it created.

A simple case is to create one Job object to reliably run one Pod to completion. The Job object will start a new Pod if the first Pod fails or is deleted (for example due to a node hardware failure or a node reboot).

You can also use a Job to run multiple Pods in parallel.

You might also need to run large computation or batch processing workloads in your clusters. For this, Job controller is useful.

A job creates one or more pods running in parallel. You can specify how many pods need to complete in this Job.

When a specified number of pods completed, the job itself is complete.

If the first pod fails or is deleted, the Job controller will start a new Pod.

The job is designed for parallel processing of independent but related work items like sending emails, rendering frames, transcoding files, scanning database keys, etc...

Here is the manifest for a Job:

apiVersion: batch/v1
kind: Job
name: example-job
name: example-job
- "-Mbignum=bpi"
- "-wle"
- "print bpi(2000)"
- perl
image: perl
name: pi
restartPolicy: Never

Create a Job:

kubectl apply -f example-job.yamljob.batch "example-job" created

Display your jobs:

kubectl get jobsNAME          DESIRED   SUCCESSFUL   AGEexample-job   1         0            33s
kubectl get pods --watch
NAME READY STATUS RESTARTS AGEexample-job-pzxhn 0/1 ContainerCreating 0 23sexample-job-pzxhn 0/1 Completed 0 51s

kubectl get jobs:

example-job 1 1 1m

Get details of a job:

kubectl describe job example-job

Edit a job:

kubectl edit job example-job

Delete a job:

kubectl delete job example-job

job. batch “example-job” deleted

Cron Jobs

A CronJob object is just like an entry in crontab in Unix/Linux. It runs a job periodically on a given schedule.

You need a working Kubernetes cluster at version >= 1.8 (for CronJob).

For previous versions of the cluster (< 1.8) you need to explicitly enable batch/v2alpha1 API by passing — runtime-config=batch/v2alpha1=true to the API server.

Here is the manifest for Cronjob

apiVersion: batch/v1beta1
kind: CronJob
name: hello
- /bin/sh
- "-c"
- "date; echo Hello from the Kubernetes cluster"
image: busybox
name: hello
restartPolicy: OnFailure
schedule: "*/1 * * * *"

Create a Cron Job:

kubectl create -f cronjob.yaml
cronjob.batch “hello” created

kubectl get cronjobs hello

hello */1 * * * * False 0 < none > 28s
kubectl get jobs --watchNAME DESIRED SUCCESSFUL AGEhello-1550406120 1 1 35shello-1550406180 1 0 0shello-1550406180 1 0 0shello-1550406180 1 1 2shello-1550406240 1 0 0shello-1550406240 1 0 0shello-1550406240 1 1 1shello-1550406300 1 0 0shello-1550406300 1 0 0shello-1550406300 1 1 1shello-1550406120 1 1 3m

Get details of a cronjob:

kubectl describe cronjob hello

Edit a cronjob:

kubectl edit cronjob hello

Delete a cronjob:

kubectl delete cronjob hello

Writing a Cron Job Spec

As with all other Kubernetes configs, a cron job needs apiVersion, kind, and metadata fields.


The .spec.schedule is a required field of the .spec. It takes a Cron format string, such as 0 * * * * or @hourly, as schedule time of its jobs to be created and executed.

👋 Join us today !!

️Follow us on LinkedIn, Twitter, Facebook, and Instagram

If this post was helpful, please click the clap 👏 button below a few times to show your support! ⬇

AVM Consulting Blog

AVM Consulting — Clear strategy for your cloud