Kubernetes Job: Difference between revisions
(12 intermediate revisions by the same user not shown) | |||
Line 10: | Line 10: | ||
A Kubernetes Job is a workload resource that can be used to start and manage Pods to successful completion. | A Kubernetes Job is a workload resource that can be used to start and manage Pods to successful completion. | ||
A Pod is considered completed, and thus its execution counted against the completion counter only if finishes successfully. This counter can be configured with <code>[[Kubernetes_Job_Manifest#completions|.spec.completions]]</code>. When the specified number of successful completions is reached, the Job is complete. The Job will attempt to restart failed Pods, which don't count against successful executions. The number of retires until the attempts are dropped can be configured with <code>[[Kubernetes_Job_Manifest#backoffLimit|.spec.backoffLimit]]</code>. More details on a Job's life cycle is available in the [[#Job_Lifecycle|Job Lifecycle]] section. | A Pod is considered completed, and thus its execution counted against the completion counter only if finishes successfully. This counter can be configured with <code>[[Kubernetes_Job_Manifest#completions|.spec.completions]]</code>. When the specified number of successful completions is reached, the Job is complete. The Job will attempt to restart failed Pods, which don't count against successful executions. The number of retires until the attempts are dropped can be configured with <code>[[Kubernetes_Job_Manifest#backoffLimit|.spec.backoffLimit]]</code>. For example, to configure a Job to run a Pod once, and give up immediately if it fails, use: | ||
<syntaxhighlight lang='yaml'> | |||
kind: Job | |||
spec: | |||
completions: 1 | |||
backoffLimit: 0 | |||
[...] | |||
</syntaxhighlight> | |||
More details on a Job's life cycle is available in the [[#Job_Lifecycle|Job Lifecycle]] section. | |||
The Pods managed by a Job will carry a <code>job-name</code> | The Pods managed by a Job will carry a <code>job-name</code> label with the name of the job, and also a <code>controller-uid</code> label. | ||
Pods can be executed in parallel. The [[#Parallel_Execution|Parallel Execution]] section provides more details on that. | Pods can be executed in parallel. The [[#Parallel_Execution|Parallel Execution]] section provides more details on that. | ||
Line 20: | Line 29: | ||
=<span id='Kubernetes_Job_Manifest'></span>Manifest= | =<span id='Kubernetes_Job_Manifest'></span>Manifest= | ||
{{Internal|Kubernetes Job Manifest|Job Manifest}} | {{Internal|Kubernetes Job Manifest|Job Manifest}} | ||
=Get the Log of a Completed Job= | |||
<syntaxhighlight lang='bash'> | |||
kubectl -n <namespace> logs job/<job-name> | |||
</syntaxhighlight> | |||
<syntaxhighlight lang='bash'> | |||
kubectl -n test logs job/test-job | |||
</syntaxhighlight> | |||
=Job Lifecycle= | =Job Lifecycle= | ||
= | ==Pod and Container Failures== | ||
{{External|https://kubernetes.io/docs/concepts/workloads/controllers/job/#handling-pod-and-container-failures}} | |||
==Completion== | |||
{{External|https://kubernetes.io/docs/concepts/workloads/controllers/job/#completion-mode}} | |||
==Suspending a Job== | |||
{{External|https://kubernetes.io/docs/concepts/workloads/controllers/job/#suspending-a-job}} | |||
=Clean up Finished Jobs Automatically= | ==Termination and Cleanup== | ||
{{External|https://kubernetes.io/docs/concepts/workloads/controllers/job/#job-termination-and-cleanup}} | |||
===Clean up Finished Jobs Automatically=== | |||
{{External|https://kubernetes.io/docs/concepts/workloads/controllers/job/#clean-up-finished-jobs-automatically}} | {{External|https://kubernetes.io/docs/concepts/workloads/controllers/job/#clean-up-finished-jobs-automatically}} | ||
Line 35: | Line 59: | ||
ttlSecondsAfterFinished: 100 | ttlSecondsAfterFinished: 100 | ||
... | ... | ||
</syntaxhighlight> | |||
=Parallel Execution= | |||
{{External|https://kubernetes.io/docs/concepts/workloads/controllers/job/#parallel-jobs}} | |||
=Job Alternatives= | |||
{{External|https://kubernetes.io/docs/concepts/workloads/controllers/job/#alternatives}} | |||
=Set Annotations or Labels on the Job-generated Pods= | |||
The labels and annotations set in the <code>.spec.template.metadata</code> section of the job declaration will be injected into the created pods: | |||
<syntaxhighlight lang='yaml'> | |||
apiVersion: batch/v1 | |||
kind: Job | |||
spec: | |||
template: | |||
metadata: | |||
labels: | |||
color: blue | |||
annotations: | |||
example.com/color: red | |||
spec: | |||
[...] | |||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 21:25, 12 December 2023
External
Internal
Overview
A Kubernetes Job is a workload resource that can be used to start and manage Pods to successful completion.
A Pod is considered completed, and thus its execution counted against the completion counter only if finishes successfully. This counter can be configured with .spec.completions
. When the specified number of successful completions is reached, the Job is complete. The Job will attempt to restart failed Pods, which don't count against successful executions. The number of retires until the attempts are dropped can be configured with .spec.backoffLimit
. For example, to configure a Job to run a Pod once, and give up immediately if it fails, use:
kind: Job
spec:
completions: 1
backoffLimit: 0
[...]
More details on a Job's life cycle is available in the Job Lifecycle section.
The Pods managed by a Job will carry a job-name
label with the name of the job, and also a controller-uid
label.
Pods can be executed in parallel. The Parallel Execution section provides more details on that.
If the Job needs to execute on a schedule, a CronJob is a better tool.
Manifest
Get the Log of a Completed Job
kubectl -n <namespace> logs job/<job-name>
kubectl -n test logs job/test-job
Job Lifecycle
Pod and Container Failures
Completion
Suspending a Job
Termination and Cleanup
Clean up Finished Jobs Automatically
Jobs that have finished, either "Complete" or "Failed" can be automatically be deleted by using the TTL mechanism provided by the [TTL Controller ]. Set the .spec.ttlSecondsAfterFinished
field in the job manifest. If the field is set to 0, the Job will be eligible to be automatically deleted immediately after it finishes.
apiVersion: batch/v1
kind: Job
spec:
ttlSecondsAfterFinished: 100
...
Parallel Execution
Job Alternatives
Set Annotations or Labels on the Job-generated Pods
The labels and annotations set in the .spec.template.metadata
section of the job declaration will be injected into the created pods:
apiVersion: batch/v1
kind: Job
spec:
template:
metadata:
labels:
color: blue
annotations:
example.com/color: red
spec:
[...]