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:
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:
To verify that the Job has run, you can use:
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:
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:
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:
| Feature | Description |
| Job Kind | kind: Job |
| Restart Policy | restartPolicy: Never |
| Parallelism | Maintain parallelism at zero or one |
| Cleanup Policy | ttlSecondsAfterFinished: <seconds> ensures job cleanup |
| Backoff Limit | Limits 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.

