https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/
https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/
https://kubernetes.io/docs/concepts/configuration/secret/
https://kubernetes.io/docs/concepts/containers/container-environment-variables/
https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/
- Jobs are constructs within Kubernetes that can be used to create pods and run a predefined task then exits the pod when the task completes
- Jobs are created via a spec file and can be monitored using kubectl commands in the same way as deployments and other items can be
kubectl describe jobs/<job name>
- Examples uses include sending emails and transcoding files, sets of independent by related work items.
- Jobs run uninterrupted unless a Pod fails or a Container exits in error, it will retry up to the backupLimit value specified in the Job spec at which point it will be marked as failed and any running Pods will be terminated. Can also define a time period in seconds as the Job deadline to force it to terminate after a set duration. The deadline value takes precedence over the backoff limit.
- Jobs required a RestartPolicy of Never (Pod Failure) or OnFailure (Container Exits)
- Jobs and their associated Pods are not automatically deleted when they finish they must be manually deleted by the administrator. Deleting a Job also deletes the associated Pods.
kubectl delete jobs/<job name>
- Jobs can be automatically deleted using additional configuration such as Cron Jobs or TTL controllers.
- Jobs have a completion count (spec.completions) and parallelism count (spec.parallelism) value. When they are not explicitly set in the Job spec a default value of 1 is used for each of them. This means the Job must complete only once to be considered successful, and only one pod will be running at any point in time during the execution of the Job.
- If a Job has a completion count value specified this takes precedence over any parallelism value specified if the parallelism value is higher i.e. telling Kubernetes that a Job should have a parallelism of 5 for a Job that has a completion count of 3 will not result in more than 3 Pods being created.
- 3 main types of tasks that are suitable to run as a Job:
- Non-parallel Jobs – Creates one pod and terminates it as soon as the Job completes successfully
- Parallel Jobs with a fixed completion count – The completions value within the spec is set to a value of 1 or greater. The Job is not considered successful until a Pod with each value in the range 1 to the completions value has been successfully created e.g. if completions is set to 3 in the spec then the Job will expect 3 Pods to have been created (values 1,2 and 3)
- Parallel Jobs with a work queue – The parallelism value within the spec is set to a value of 1 or higher. Has a dependency on the Pod configuration or an external service being able to co-ordinate the work across multiple Pods and monitoring the progress to determine when all Pods are finished and the overall Job is complete.
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4