Difficulty: beginner
Estimated Time: 5 minutes

This scenario provides an overview of Jobs, Init Containers and Cron Jobs in Kubernetes.

Jobs resources create one or more pods and ensures that all of them successfully terminate.

There're two types of jobs:

  • Non-parallel Job: A Job which creates only one Pod (which is re-created if the Pod terminates unsuccessfully), and which is completed when the Pod terminates successfully.
  • Parallel jobs with a completion count: A Job that is completed when a certain number of Pods terminate successfully. You specify the desired number of completions using the completions field.

Cron Jobs create a job object, they are useful for creating periodic and recurring tasks, e.g running backups or sending emails.

Init Containers are regular containers within a pod that run before the app container and they also satisfy the following statements:

  • They can run setup scripts not present in an app container - e.g prepopulate some data, waiting until a specific service is up and running and etc.
  • A pod can have one or more init containers apart of app containers
  • They always run to completation
  • Each one must complete successfully before the next one is started
  • Application container won't run if any init container won't finish successfully

Congratulations! You can start the next exercise in this series.

Jobs, Init Containers and Cron Jobs

Step 1 of 4


Jobs resources are used to running a batch job, supporting parallel jobs until reach a specific number of completions.

Therefore with Jobs, we can run a work items such as frames to be rendered, files to be transcoded, ranges of keys in a NoSQL database to scan, and so on.

Take a look at Jobs Api reference to see how to build a job resource in Kubernetes.

Pods created by jobs are not deleted. Keeping them around allows you to still view the logs of completed pods to check for errors. If you want to remove them, you need to do that manually.

Create Countdown Job

Look at the file job.yaml.

This example creates a job which runs a bash command to countdown from 10 to 1.

Notice that the field spec.restartPolicy just allow two values: "OnFailure" or "Never", for further information read here

Note: There're situations where you want to fail a job after some ammount of retries, to do so use spec.backoffLimit. It is set by default to 6. You could want to manage the duration of the job, not matter how many Pods are created. You can use spec.activeDeadlineSeconds and once a Job reaches this value(in sec), the Job and all of its Pods are terminates.

Create the countdown job:

kubectl apply -f /manifests/job.yaml

Job status

Check the status of the job:

kubectl get jobs

Job Logs

In order to see the job's logs we need to get the job name:

kubectl get pods -o 'jsonpath={.items[0].metadata.name}'; echo

And then execute the following command to get the logs:

kubectl logs `kubectl get pods -o 'jsonpath={.items[0].metadata.name}'`

You will get a result like:

9 8 7 6 5 4 3 2 1 Perfect!

Delete Job

kubectl delete -f /manifests/job.yaml


kubectl delete job countdown