How To Create Kubernetes Job/Cron Job

Kubernetes Jobs and CronJobs are essential tools for managing workloads within your Kubernetes cluster. Jobs enable you to execute one-time tasks, while CronJobs automates repetitive tasks based on a defined schedule. This comprehensive guide will walk you through creating and configuring both Jobs and CronJobs, empowering you to streamline your Kubernetes workflows.

What is Kubernetes?

Kubernetes often abbreviated as K8s is an open-source platform designed to automate the deployment, scaling, and operation of containerized applications. Containers are lightweight, portable units that bundle an application and its dependencies, ensuring consistency across different environments.

What are Workloads?

A workload is an application running on the cluster. This application can be a single component or multiple components that work together, all running inside a set of pods. A Pod in Kubernetes is the smallest deployable unit and represents a group of one or more containers that share storage, network, and specifications on how to run them.

Pods have a defined lifecycle in Kubernetes. For example, once a pod is running in your cluster, if a critical fault occurs on the node where that pod is running, all the pods on that node will fail. Kubernetes treats such a failure as final, meaning it will not try to recover the failed pods on the faulty node. Instead, you would need to create new pods to replace the failed ones, even if the node becomes healthy again later.

However, managing each Pod individually can be complex and time-consuming. To make this easier, Kubernetes provides workload resources that manage sets of pods on your behalf. These resources configure controllers, which are responsible for ensuring that the correct number of the correct type of pods are always running, based on the state you have specified.

For example, a Deployment is a type of workload resource that manages a set of identical pods. If one of these pods fails or the node it’s running on fails, the Deployment controller will automatically create a new pod to replace the failed one, ensuring that the desired number of pods is always running.

Kubernetes provides several built-in workload resources:

  • Deployment and Replica set
  • Statefulset
  • Daemonset
  • Job and CronJob

In this article, we will focus on the Job/Cron Job workload.

What are Jobs?

A Job in Kubernetes is designed to manage the execution of a task by creating one or more Pods and ensuring that they run to completion. A Job creates Pods to perform a specific task and continues to retry execution of these Pods until a specified number of them successfully terminate. The Job tracks these successful completions, and once the desired number is reached, the Job is considered complete.

  • When you create a Job, Kubernetes will start creating Pods to perform the task specified in the Job’s configuration.
  • If a Pod fails (e.g., due to a kuberneets node hardware failure, a node reboot, or the Pod being deleted), the Job will automatically start a new Pod to replace it. This ensures that the task is reliably completed even in the face of individual Pod failures.
  • The Job continues to create and monitor Pods until the specified number of successful completions is reached. At this point, the Job is marked as complete, and no more Pods are created.
  • Deleting a Job will also delete all the Pods it created, ensuring that no unnecessary resources are consumed.
  • Suspending a Job will delete its active Pods, effectively pausing the task. When the Job is resumed, new Pods are created to continue from where the Job left off.

Steps to Create a Kubernetes Job

Prerequsites:

Here, for the demo purpose i am using Minikube.

Follow the steps mentioned below:

Step 1: Create a YAML file

nano <filename.yaml>
#nano job1.yaml

Step 2: In the below we have created a job which runs a pod based on the alpine image, and inside the container the job prints the number from 1 to 9 and then it terminates, and we have specified the restartPolicy: Never which means the pod will not be restarted if it exits.

apiVersion: batch/v1
kind: Job
metadata:
name: k8s-job
spec:
template:
metadata:
name: k8s-job
spec:
containers:
- name: job-demo
image: alpine:latest
command:
- "bin/sh"
- "-c"
- "for i in 1 2 3 4 5 6 7 8 9 ; do echo $i ; done"
restartPolicy: Never

Step 3: Create the yaml file using:

kubectl apply -f job1.yaml

Step 4: Check the state of the pods in the cluster

kubectl get pod

After executing the above command, the status coloumn will show that its completed. You can also use describe command to see a detailed information about the pod

kubectl describe pod <podname>

The Containers section of the output will lists the container state as Terminated due to the completion of Operation

Step 5: You can also check the job by –

kubectl get job

and the completion coloumn will show that the job is completed sucessfully.

What are CronJobs?

A CronJob in Kubernetes is used to run Jobs on a scheduled basis. It allows you to schedule tasks to be executed at specified times or intervals. CronJobs are useful for recurring tasks such as backups, periodic report generation, and maintenance tasks.

kubernetes Job/CronJob – FAQs

What’s the difference between a Kubernetes Job and a CronJob?

Both Jobs and CronJobs manage Pods in Kubernetes, but they serve different purposes:

  • Jobs: Designed for running one-time tasks to completion. They create Pods, ensure they run successfully a specified number of times, and then clean up the Pods. Useful for batch processing, data transformation, or any task that needs to run to completion once.
  • CronJobs: Schedule Jobs to run at defined intervals. They use the Cron syntax for scheduling, allowing you to automate recurring tasks like backups, report generation, or maintenance routines. Essentially, CronJobs trigger Jobs on a set schedule.

How do I know when a Job is finished?

Jobs track successful completions of Pods. You specify the desired number of successful completions in the Job definition. Once that number is reached, the Job considers itself complete and stops creating new Pods. You can use kubectl get job to check the completion status of a Job.

My Job keeps restarting, even though the Pod completes successfully. Why?

By default, Pods have a restartPolicy set to Always. This means they will automatically restart if they exit. However, Jobs have their own logic for managing Pod completions. When creating a Job, consider setting the restartPolicy of the Pods within the Job definition to Never. This ensures the Pods terminate after completing their task, contributing to the overall Job completion count.



Contact Us