Managing Jobs
1. Creating Job
| Field | Description | Example |
|---|---|---|
| apiVersion | API version to use | batch/v1 |
| kind | Resource type | Job |
| metadata.name | Name of the Job | my-job |
| spec.template.spec.containers | List of containers to run | [{ name: "worker", image: "myapp:latest" }] |
| spec.completions | Number of successful completions required | 1 |
| spec.parallelism | Number of concurrent pods to run | 1 |
Example: Creating a Job
apiVersion: batch/v1
kind: Job
metadata:
name: my-job
spec:
template:
spec:
containers:
- name: worker
image: myapp:latest
restartPolicy: Never
completions: 1
parallelism: 1
2. Creating Job from CronJob
| Field | Description | Example |
|---|---|---|
| spec.schedule | Cron expression defining when to run the job | 0 0 * * * |
| spec.jobTemplate | Template for the Job to be created | |
| spec.startingDeadlineSeconds | Deadline in seconds for starting the job if it misses its scheduled time | 300 |
| spec.concurrencyPolicy | Policy for handling concurrent executions (Allow, Forbid, Replace) | Forbid |
| spec.suspend | Boolean to suspend the CronJob (true = suspended) | false |
Example: Creating a CronJob that runs a Job daily
apiVersion: batch/v1
kind: CronJob
metadata:
name: daily-job
spec:
schedule: "0 0 * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: my-container
image: my-image:latest
restartPolicy: OnFailure
Note: The CronJob creates a new Job instance based on the
jobTemplate at each scheduled time.
3. Listing Jobs
| Command | Description | Flags |
|---|---|---|
| kubectl get jobs | List all jobs in the current namespace | -A to list across all namespaces |
| kubectl get jobs -o wide | List jobs with additional details like node name and age | -o wide for extended output |
| kubectl get jobs --sort-by=.metadata.creationTimestamp | List jobs sorted by creation timestamp | --sort-by flag with field path |
Example: Listing Jobs in Current Namespace
kubectl get jobs
NAME COMPLETIONS AGE
job1 1/1 5m
job2 0/1 2m
Example: Listing Jobs Across All Namespaces
kubectl get jobs -A
NAMESPACE NAME COMPLETIONS AGE
default job1 1/1 5m
kube-system job2 0/1 2m
Note: The
COMPLETIONS column shows the number of successful completions and total desired completions (e.g., 1/1 means job is complete).
4. Describing Job Details
| Property | Description | Example |
|---|---|---|
| metadata.name | Name of the Job | batch-job |
| metadata.namespace | Namespace where the Job is defined | default |
| spec.template.spec.containers | List of containers in the Pod template | |
| spec.completions | Number of completed pods required to consider the Job successful | 3 |
| spec.parallelism | Number of pods that can run in parallel | 2 |
| spec.backoffLimit | Maximum number of retries before marking the Job as failed | 4 |
| status.conditions | Current conditions of the Job | |
| status.active | Number of active pods | 1 |
| status.succeeded | Number of succeeded pods | 2 |
| status.failed | Number of failed pods | 0 |
Example: Describe Job Details
apiVersion: batch/v1
kind: Job
metadata:
name: batch-job
namespace: default
spec:
template:
spec:
containers:
- name: my-container
image: alpine:latest
command: ["/bin/sh", "-c", "echo 'Hello World'"]
restartPolicy: Never
completions: 3
parallelism: 2
backoffLimit: 4
status:
active: 1
succeeded: 2
failed: 0
5. Setting Completions Count
| Field | Description | Values | Notes |
|---|---|---|---|
| spec.completions | Number of successful finished pods required to consider the Job as completed | integer |
Default is 1 if not specified |
| spec.parallelism | Number of pods that are allowed to run in parallel | integer |
Defaults to 1 if not specified |
| spec.activeDeadlineSeconds | Maximum duration in seconds for the Job to complete | integer |
If exceeded, the Job is terminated |
Example: Setting Completions Count
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
completions: 3
parallelism: 2
template:
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
6. Setting Parallelism Level
| Field | Description | Values | Notes |
|---|---|---|---|
| parallelism | Number of pods running simultaneously | integer |
Default is 1. Set to 0 to suspend job |
| completions | Total number of pods to run | integer |
Must be >= parallelism |
| backoffLimit | Maximum number of retries before marking job as failed | integer |
Default is 6 |
Example: Setting parallelism to 3
apiVersion: batch/v1
kind: Job
metadata:
name: example-job
spec:
parallelism: 3
completions: 6
template:
spec:
containers:
- name: worker
image: busybox
command: ["echo", "Hello from job"]
restartPolicy: Never
7. Configuring Backoff Limit
| Property | Description | Default Value | Notes |
|---|---|---|---|
| backoffLimit | Number of retries before marking the job as failed | 6 |
Set to null for infinite retries |
Example: Setting Backoff Limit
apiVersion: batch/v1
kind: Job
metadata:
name: example-job
spec:
backoffLimit: 5
template:
spec:
containers:
- name: example-container
image: busybox
command: ["sh", "-c", "exit 1"]
restartPolicy: Never
Note: Setting
backoffLimit to null allows infinite retries, which can lead to resource exhaustion if not managed carefully.8. Setting Active Deadline
| Property | Value | Description |
|---|---|---|
| activeDeadlineSeconds | 300 |
Maximum allowed duration in seconds for a Job to run before it is terminated |
Example: Setting Active Deadline on a Job
apiVersion: batch/v1
kind: Job
metadata:
name: example-job
spec:
activeDeadlineSeconds: 300
template:
spec:
containers:
- name: example-container
image: busybox
command: ["sh", "-c", "echo Hello World"]
restartPolicy: Never
9. Using Job Selectors
| Selector Type | Description | Example |
|---|---|---|
| labelSelector | Matches jobs based on labels | metadata.labels.environment: production |
| fieldSelector | Matches jobs based on fields in the object's metadata | status.phase: Failed |
Example: Label Selector
apiVersion: batch/v1
kind: Job
metadata:
name: example-job
labels:
environment: production
team: backend
spec:
template:
spec:
containers:
- name: job-container
image: alpine:latest
command: ["echo", "Hello World"]
restartPolicy: Never
Example: Field Selector
apiVersion: batch/v1
kind: Job
metadata:
name: example-job
spec:
template:
spec:
containers:
- name: job-container
image: alpine:latest
command: ["echo", "Hello World"]
restartPolicy: Never
Note: Label selectors are more commonly used than field selectors in Kubernetes.
10. Viewing Job Logs
| Command | Description | Notes |
|---|---|---|
| kubectl logs job/<job-name> | View logs from a completed job | Useful for debugging job failures |
| kubectl logs -l app=<app-name> | View logs for all pods matching a label | Useful when job has multiple pods |
| kubectl logs --previous job/<job-name> | View logs from previous job instance | Useful for troubleshooting failed jobs |
Note: Logs are only available while the pod is running or has not been garbage collected.
11. Deleting Jobs
| Field | Description | Values |
|---|---|---|
| metadata.name | Name of the Job | my-job |
| metadata.namespace | Namespace where the Job is located | default |
| spec.completions | Number of successful completions required to consider the Job as complete | 1 |
| spec.parallelism | Number of pods that are allowed to run in parallel | 1 |
| spec.backoffLimit | Number of retries before marking the Job as failed | 4 |
Note: Deleting a Job does not delete the Pods it created. You can use
kubectl delete pods --selector=job-name=my-job to clean up the associated Pods.
Warning: Deleting a Job that is currently running will stop the execution of its Pods. Ensure that you have a backup or are okay with losing progress.
12. Cleaning Up Finished Jobs
| Field | Description | Default Value |
|---|---|---|
| ttlSecondsAfterFinished | Number of seconds to retain job after completion | null |
Example: Setting TTL for Job Cleanup
apiVersion: batch/v1
kind: Job
metadata:
name: example-job
spec:
ttlSecondsAfterFinished: 300
template:
spec:
containers:
- name: example-container
image: busybox
command: ["echo", "Hello World"]
restartPolicy: Never
Note: Setting
ttlSecondsAfterFinished allows automatic cleanup of completed jobs, preventing accumulation of finished resources in the cluster.Warning: Once a job is cleaned up, its logs and status information are permanently lost. Ensure logs are collected or archived before cleanup.