Kubernetes
Jobs
CronJob
ScheduledTasks
DevOps

Kubernetes - how to run job only once

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Understanding Kubernetes Jobs

Kubernetes, often abbreviated as K8s, is a powerful orchestration platform for managing containerized applications across a cluster of machines. Among its many features, Kubernetes provides a resource called a Job specifically designed to run a given containerized task until completion. Unlike other Kubernetes controllers like Deployments that are intended for long-running applications, a Job is meant to run tasks such as batch processing or data analysis, which are finite and need to run to completion only once.

How to Run a Kubernetes Job Only Once

Running a Kubernetes Job only once is straightforward thanks to the declarative nature of Kubernetes. A Job is configured to create one or more Pods and ensures that a specified number of them successfully complete.

Creating a Job Manifest

A Kubernetes Job is defined in a YAML manifest. Below is an example of a basic Job manifest:

yaml
1apiVersion: batch/v1
2kind: Job
3metadata:
4  name: example-job
5spec:
6  template:
7    spec:
8      containers:
9      - name: example-container
10        image: busybox
11        command: ["echo", "Hello Kubernetes!"]
12      restartPolicy: Never

Technical Breakdown

  • apiVersion: batch/v1: Specifies the API version for Job resources.
  • kind: Job: Indicates the creation of a Job.
  • metadata: Contains metadata about the Job, including the name.
  • spec.template.spec: Under this section, the Pod template is defined, specifying the container(s) to run.
  • containers: Under this, you specify the container image and the command to run.
  • restartPolicy: Never: Ensures the Pod does not restart if it exits. This is crucial for running the Job only once.

Deploying the Job

To deploy this job, save the above YAML to a file, example-job.yaml, and run the following command:

bash
kubectl apply -f example-job.yaml

To verify that the Job has run, you can use:

bash
kubectl get jobs
kubectl get pods

The Job controller will create a new Pod, execute it, and then mark the Job as completed once the task is finished.

Additional Notes

Completion and Parallelism

By default, a Job runs with one Pod instance. You can configure parallelism if your task can be divided into smaller, parallel tasks. However, to ensure the job runs only once, you maintain the parallelism at zero or one.

Cleanup Policy

Once a Job is complete, the associated Pods remain in the system unless explicit cleanup is performed. Configure automatic cleanup with a ttlSecondsAfterFinished field:

yaml
1apiVersion: batch/v1
2kind: Job
3metadata:
4  name: example-job
5spec:
6  ttlSecondsAfterFinished: 100
7  template:
8    spec:
9      containers:
10      - name: example-container
11        image: busybox
12        command: ["echo", "Hello Kubernetes!"]
13      restartPolicy: Never

The Kubernetes controller will remove the Job and Pods after 100 seconds once the Job is complete.

Handling Backoff and Retries

If your Job encounters an error, Kubernetes will retry it, subject to a backoff limit. To limit retries, modify the backoffLimit:

yaml
spec:
  backoffLimit: 3

This ensures the Job will retry only three times before giving up.

Summary

The following table provides a summary of key points for running a job only once in Kubernetes:

FeatureDescription
Job Kindkind: Job
Restart PolicyrestartPolicy: Never
ParallelismMaintain parallelism at zero or one
Cleanup PolicyttlSecondsAfterFinished: <seconds> ensures job cleanup
Backoff LimitLimits the number of job retries on failure

Conclusion

Using a Job in Kubernetes to run tasks only once is a straightforward process, but requires attention to detail regarding configurations related to retries, parallelism, and cleanup processes. Properly configuring these aspects ensures that tasks run efficiently without unnecessary retry cycles or leftover resources lingering post-execution. Kubernetes Jobs provide powerful mechanisms for handling batch processes, allowing users to manage stateful workloads like never before.


Course illustration
Course illustration

All Rights Reserved.