Understanding Jobs in Kubernetes

Photo by Growtika on Unsplash

Understanding Jobs in Kubernetes

Kubernetes Jobs

A Kubernetes "Job" is a resource object designed to run one-off or batch tasks within the cluster. Unlike pods managed by other controllers like Deployments, Jobs are specifically tailored for performing tasks with a guaranteed single execution. Once the job is completed, it won't restart the pods.

How Jobs Work

Kubernetes Jobs ensures that each task is accomplished exactly once. This means that if a pod fails during the task's execution, Kubernetes will automatically recreate it until it succeeds or the maximum number of attempts is reached. Once all pods created by the Job have succeeded, the Job is considered complete.

Key attributes of a Job include:

  • completions: The number of times all tasks should be successfully completed.

  • parallelism: The number of tasks run in parallel at any given time.

  • backoffLimit: The maximum number of restart attempts for the pod in case of failure.

  • activeDeadlineSeconds: The maximum duration for which the Job is allowed to run.

Creating a Job

Creating a Job is straightforward. You specify a pod template to run and set the Job's parameters. For instance, let's consider a Job that runs a container to perform a daily database backup. Here's what the YAML definition of such a Job would look like:

apiVersion: batch/v1
kind: Job
metadata:
  name: my-backup-job
  labels:
    app: my-app
spec:
  completions: 3
  parallelism: 2
  backoffLimit: 2
  activeDeadlineSeconds: 3600
  template:
    metadata:
      name: my-backup-job
    spec:
      restartPolicy: OnFailure
      containers:
      - name: my-backup-job
        image: backupdatabaseimage
        command: ["/bin/sh", "-c","backup.sh"]

Execution :

$ kubectl apply -f simplejob.yaml 
job.batch/my-backup-job created
$ kubectl get jobs
NAME            COMPLETIONS   DURATION   AGE
my-backup-job   3/3           16s        23s

Conclusion

Kubernetes Jobs are a potent means of managing one-off or batch tasks within a Kubernetes cluster. They ensure that each task is successfully executed only once, making them ideal for jobs that demand precise and reliable execution.