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.
Only one pod per job is started, unless the pod fails. Job is complete as soon as the pod terminates successfully.
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!
$ kubectl apply -f job.yaml
$ kubectl get jobs
$ kubectl get -w pods
$ kubectl get jobs
N.B : Just to check the job is finished.
$ kubectl delete -f job.yaml
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.
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.
$ kubectl apply -f paralleljob.yaml
$ kubectl get -w jobs
N.B : The output shows that 2 pods are created about every 20 seconds.
$ kubectl get -w pods -l job-name=wait
$ kubectl get jobs
$ kubectl delete -f paralleljob.yaml
https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/