Jobs run to completion and then stop and exit.
apiVersion: batch/v1
kind: Job
metadata:
name: math-add-jod
spec:
template:
spec:
containers:
- name: math-add
image: ubuntu
command: ['expr', '3','+','2']
restartPolicy: Never
The above will run the command 3+2 and then the pod will go into a completed state.
> kubectl create -f job-def.yml
job created
> kubectl get jobs
NAME Desired SUCCESSFUL AGE
math-add-job 1 1 38s
> kubectl get pods
NAME READY STATUS RESTARTS AGE
math-add-job-1087sn 0/1 Completed 0 1m
To get the output of the job, you can use the logs command.
> kubectl logs math-add-job-1078sn
5
> kubectl delete job math-add-job
job.batch "math-add-job" deleted
apiVersion: batch/v1
kind: Job
metadata:
name: random-error-job
spec:
completions: 3
parallelism: 3
template:
spec:
containers:
- name: random-error
image: kodekloud/random-error
restartPolicy: Never
This job will create 3 jobs at once (in parallel) and continue to run until 3 have completed successfully.
NAME READY STATUS RESTARTS
random-exit-job-kadre 0/1 Completed 0
random-exit-job-dadre 0/1 Error 0
random-exit-job-35ade 0/1 Error 0
random-exit-job-aedb3 0/1 Completed 0
random-exit-job-da5k3 0/1 Error 0
random-exit-job-33dgd 0/1 Completed 0
Once there have been 3 jobs that completed successfully, the job will end.
The activeDeadlineSeconds
applies to the duration of the job, no matter how many Pods are created. Once a Job reaches activeDeadlineSeconds, all of its running Pods are terminated and the Job status will become type: Failed with reason: DeadlineExceeded
.
A cluster operator can use this feature to clean up finished Jobs (either Complete or Failed) automatically by specifying the .spec.ttlSecondsAfterFinished
field of a Job. The TTL controller will assume that a resource is eligible to be cleaned up TTL seconds after the resource has finished, in other words, when the TTL has expired.
The TTL seconds can be set at any time. Here are some examples for setting the .spec.ttlSecondsAfterFinished field of a Job:
the .spec.completions
will ensure that the job completes the specified number of times and then kill the job.
Use the .spec.parallelism
to specify how many jobs to run in parallel. This will start up the number of jobs specified at the same time.