Kubernetes Jobs is a feature designed to execute tasks within a Kubernetes cluster. These tasks are typically short-lived and run to completion, ensuring that they are executed reliably and successfully. It’s a valuable resource for managing batch processing or one time execution tasks that has a defined start and end point in a Kubernetes environment.
A Kubernetes job is a way to tell Kubernetes to do a task, and when that task is finished, it stops doing it. So, the jobs are successfully terminated when it completes the job. A simple use case for a Kubernetes Job is to ensure that a single pod runs to completion reliably. If the pod fails or is deleted, the Job controller automatically starts a new pod to replace it. This is particularly useful for tasks that need to be executed exactly once or need to be retried until they succeed.
Jobs can be categorized into
Jobs: Sometimes, you need to run a one-time task, like running database migrations or applying configuration changes to your infrastructure. Jobs are ideal for such scenarios.
Cron Jobs: Cron Jobs allow you to automate tasks at specific times or intervals, such as database backups or log rotation.
Let’s take an example: create a single pod based on the “alpine:latest” image. Inside that pod, it runs a simple shell script that prints the numbers from 1 to 9. Once this script completes, the Job itself is considered done.
Step 1: Create a Job Yaml:
- apiVersion and kind specify that this is a Kubernetes Job.
- metadata provides a name for the Job, which is “test-job”.
- spec defines the Job’s specification.
- template describes the pod that the Job creates.
- containers specifies the container that performs the job.
- name assigns a name to the container, which is “test” here.
- image specifies the Docker image to use. In this example, it uses the “alpine:latest” image.
- command defines the command to run inside the container. Here, it runs a simple shell script: for i in 1 2 3 4 5 6 7 8 9 ; do echo $i ; done.
- restartPolicy is set to “Never,” which means the Job won’t automatically restart if it fails or completes.
Step 2: Apply the Job:
Apply the job to the cluster with following command
kubectl apply -f [filename].yaml
Save the example file as , test-job.yaml and use the following command:
Kubectl apply – f test-job.yaml
Step 3: Monitor the Job:
To check the progress of the job, run:
kubectl describe pod [pod-name]
in our example, Kubectl describe job test-job.yaml
So, this way you can create the Kubernetes job.
Embrace Kubernetes Jobs to make your container tasks easier. They’ll help you get things done smoothly. Happy Job running!!