DEV Community

Naveen Jayachandran
Naveen Jayachandran

Posted on

Kubernetes - Jobs

In the Kubernetes world, jobs are considered an object that acts as a supervisor or controller for a task. The Kubernetes Job will create a Pod, monitor the task, and recreate another one if that Pod fails for some reason. Upon completion of the task, it will terminate the Pod. Unlike Deployments and Pods, you can specify a Job in Kubernetes which can be an always-running job, a time-based job, or a task-based job. This allows you to tolerate errors or failures that can cause unexpected Pod termination.

When you submit a Job, it will create one or more Pods based on the requirement, complete the defined task, and keep the Pods running until the task is finished. The Job keeps track of successful completions when Pods finish execution. When a Job is suspended, all of its active Pods are deleted until the Job is restarted.

Job Types

  1. Non-Parallel Job
    A simple job where a single task is defined. It will create one Pod, and upon successful completion, the Pod will terminate.

  2. Parallel Job with Fixed Completion Count
    A more complex task that requires multiple Pods to complete. These Pods run in parallel, and each Pod receives a unique index between 0 and .spec.completions - 1, based on the number specified in the configuration. The Job is considered successful when the number of successful Pod completions matches .spec.completions.

  3. Parallel Job with a Work Queue
    In this type, multiple Pods run in parallel to complete complex workloads with dependencies. To decide what each Pod should work on, the Pods cooperate with one another or with an external service.
    For instance, a Pod might pull up to N items in a batch from a work queue. Each Pod autonomously determines whether all peers are finished and whether the entire Job is complete.

Use Cases
A simple use case for a Job is to perform system operations tasks. For example, when setting up a cluster or service that requires repeated setup tasks, you can create a Job and reuse it to bring up the same service or perform similar tasks.

Another use case is performing data backups or computation tasks.

A more complex scenario involves a series of tasks that must be executed in order, where Jobs create and manage Pods until the specified number of completions is reached.

Helm Charts also employ Jobs to run installation, setup, or test commands on clusters during service provisioning.

Key Terminologies
Kubernetes: An open-source system from Google for orchestrating containers, automating most operational tasks around containerized applications.

Pods: The smallest deployable compute units in Kubernetes, which can contain one or more containers.

Minikube: A local version of Kubernetes that helps you start and test your workloads locally.

Steps to Set Up a Job
Let’s consider an example of setting up a Job using the Docker BusyBox image that pings asktech.org.

Step 1. Start your Minikube
$ minikube start
Step 2. Create the Job definition file in YAML format
$ cat ping-job.yaml
Job Definition file in YAML format

Step 3. Submit the Job definition to Kubernetes
$ kubectl apply -f ping-job.yaml
You should see a message indicating that the Job has been created.

Step 4. List all Jobs
$ kubectl get jobs
You can see the number of completions, duration, and age of the Job.

Step 5. Get Job Details
$ kubectl describe job ping
This command displays detailed information about the Job.

Step 6. Get the Pods Running for the Job
$ kubectl get pods
You can view the Pod name, container count, status, restart count, and age.

Step 7. Check Container Logs
You can also check the logs of the running container to see the output of your Job.

Step 8. Delete the Job
To delete the Job and its associated Pods:

$ kubectl delete job ping
$ kubectl get jobs
Once deleted, the Pods associated with the Job are also removed.

✅ Final Note:
Kubernetes Jobs are powerful tools for running finite, batch-oriented tasks. Whether you’re performing database migrations, running test suites, or doing scheduled backups, Jobs provide reliability and fault tolerance through their built-in restart and completion tracking mechanisms.

Top comments (0)