Difficulty: beginner
Estimated Time: 5 minutes

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

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

There are 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 from app containers
  • Init containers always run to completation
  • Each one must complete successfully before the next one is started
  • The application container won't run if any of the configured init containers will not finish the execution successfully

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

Jobs, Init Containers and Cron Jobs

Step 1 of 4


Job resources are used to facilitate the execution of a batch job. Through Job resources, Kubernetes also supports parallel jobs which will finish executing when a specific number of successful completions is reached.

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

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

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

Create Countdown Job

Take a look at the file job.yaml.

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

Notice that the field spec.restartPolicy allow only two values: "OnFailure" or "Never". For further information read here

Note: There are situations where you want to fail a job after a number of retries. To do so, use spec.backoffLimit which, by defauly, is set 6. You can use spec.activeDeadlineSeconds to limit the execution time in case you want to manage the duration of a specific job. If the execution reaches this deadline, the Job and all of its Pods are terminated.

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 name of the Job in question:

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