Job

A job creates one or more pods and ensures that 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 job itself is complete.

Deleting a Job will cleanup the pods it created.

Non-parallel Job

Only one pod per job is started, unless the pod fails. Job is complete as soon as the pod terminates successfully.

  • Lets write the manifest.
apiVersion: batch/v1
kind: Job
metadata:
  name: wait
spec:
  template:
    metadata:
      name: wait
    spec:
      containers:
      - name: wait
        image: ubuntu
        command: ["sleep",  "20"]
      restartPolicy: Never

N.B : It creates an Ubuntu container, sleeps for 20 seconds and that’s it!

  • Lets create the job
$ kubectl apply -f job.yaml
  • Lets check the jobs
$ kubectl get jobs
  • How to watch the pod status to confirm that job is succesful?
$ kubectl get -w pods
  • Now, watch the job status again:
$ kubectl get jobs

N.B : Just to check the job is finished.

  • To delete the job, you can run this command
$ kubectl delete -f job.yaml

Parallel Job

Non-parallel jobs run only one pod per job. This API is used to run multiple pods in parallel for the job. The number of pods to complete is defined by .spec.completions attribute in the configuration file. The number of pods to run in parallel is defined by .spec.parallelism attribute in the configuration file. The default value for both of these attributes is 1.

The job is complete when there is one successful pod for each value in the range in 1 to .spec.completions. For that reason, it is also called as fixed completion count job.

  • Lets write the job specification
apiVersion: batch/v1
kind: Job
metadata:
  name: wait
spec:
  completions: 6
  parallelism: 2
  template:
    metadata:
      name: wait
    spec:
      containers:
      - name: wait
        image: ubuntu
        command: ["sleep",  "20"]
      restartPolicy: Never

This job specification is similar to the non-parallel job specification. It has two new attributes added: .spec.completions and .spec.parallelism. This means the job will be complete when six pods have successfully completed. A maximum of two pods will run in parallel at a given time.

  • Create a parallel job using the command:
$ kubectl apply -f paralleljob.yaml
  • Watch the status of the job as shown:
$ kubectl get -w jobs

N.B : The output shows that 2 pods are created about every 20 seconds.

  • In another terminal window, watch the status of pods created:
$ kubectl get -w pods -l job-name=wait
$ kubectl get jobs
  • Delete the job as:
$ kubectl delete -f paralleljob.yaml
  • To control the number of failure and restart , we can use spec.backoffLimit
  • To start n number of pods that will run in sequential order , we can use `.spec.completions
  • To start multiple pods in parallel , we can use .spec.parallelism
  • To cleanup the completed Jobs automatically , we can use ttlSecondsAfterFinished (1.12 alpha feature)

https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/