Open In App

Kubernetes Jobs and CronJobs for Scheduled Tasks

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Kubernetes is a platform, that is used to manage the application in containerized form. It offers specific resources to execute the batch jobs and recurring tasks on a time or a Time Interval basis.

In this post, I am gonna show you the 2 main features Kubernetes Jobs and Cron Jobs, to effectively task scheduling within a Kubernetes cluster.

What Is A Job In Kubernetes?

Kubernetes is an open-source container platform that manages applications in containers to run on a cluster of hosts. Kubernetes Jobs is parallel, used to run in short lives and sequential batch jobs within a cluster. Kubernetes Job provides a mechanism for tracking completion, automatic rerouting, and task parallelization, making them ideal for performing one-time or on-demand tasks.

A Kubernetes job creates one or more pods and will attempt to execute them until a certain number of them are completed successfully. For example, the Kubernetes job is for creating a single task object to complete Pod sequentially. If this primary of removed for failure, this job object will start a new job.

Core Concepts Of Kubernetes Jobs

  • Completion: To be considered the task completed, Pods are required to for confirming the Specifies the desired number of completed.
  • Pod Templates: To create the Pods that will perform the actual tasks, it is required to Tasks use a pod template.
  • Parallelism: This handles the maximum number of pods that can run to execute tasks.
  • Restart Policy: This is very useful, for the behavior of Pods, when the task fails, it can restart the task.

Use Cases For Kubernetes Jobs

  • Kubernetes jobs are used to periodically back up and restore data.
  • Performing data processing or data analysis tasks in a distributed manner.
  • It runs batch jobs such as report generation or image processing.
  • Performing complex or resource-intensive tasks that require parallelism.

Here is an example of what the YAML file of Kubernetes Jobs looks like;

apiVersion: batch/v1          ## Version of  Kubernetes API
kind: Job ## The type of object for jobs
metadata:
name: job-test
spec:
template:
metadata:
name: job-test
spec:
containers:
- name: job
image: busybox
command: ["echo", "job-test"]
restartPolicy: OnFailure ## Restart Policy in case container failed
Kubectl get pods

What Is Cronjobs In Kubernetes?

CronJobs in Kubernetes is used for the scheduling and automation of recurring jobs. CronJobs is especially used for periodic maintenance automatically, data synchronization, or any task that needs to run in any specific time interval.

Core Concept Of Kubernetes Cronjobs

  • Job Templates: cronjobs will execute jobs using a pod template-like jobs.
  • Schedule: Specifies the time and frequency at which the cronjob should run.
  • Start Timeout Seconds: Specifies the maximum amount of time a cronjob should run before it is considered missed or failed.

Use Cases For Kubernetes CronJobs

  • In Different systems, Cronjobs schedule backup or data synchronization.
  • Routine system maintenance tasks such as log rotation or database cleanup.
  • Periodic data aggregation or reporting tasks.

Here is an example of what the YAML file of Kubernetes CronJobs looks like;

apiVersion: batch/v1beta1           
kind: CronJob
metadata:
name: cron-test
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: cron-test
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello this is Cron test
restartPolicy: OnFailure ## Restart Policy
Output of kubectl get cronjob cron-test

Setting Up and Configuring Kubernetes Jobs and CronJobs: Step-by-Step Guide

Step-by-Step Guide for Kubernetes Jobs

1. Create a YAML file for the Job:

nano job-test.yaml

2. Add Job configuration: Add the following content to your job-test.yaml file:

apiVersion: batch/v1          ## Version of  Kubernetes API
kind: Job ## The type of object for jobs
metadata:
name: job-test
spec:
template:
metadata:
name: job-test
spec:
containers:
- name: job
image: busybox
command: ["echo", "job-test"]
restartPolicy: OnFailure ## Restart Policy in case container failed

3. Apply the Job

kubectl apply -f job-test.yaml
Screenshot-from-2024-09-17-19-02-13

4. Verify the Job

kubectl get jobs
kubectl describe job job-test
Screenshot-from-2024-09-17-19-04-54

5. Check Logs of the Job Pod

kubectl get pods --selector=job-name=job-test
kubectl logs <pod-name>
Screenshot-from-2024-09-17-19-09-40

Step-by-Step Guide for Kubernetes Jobs

1. Create a YAML file for the CronJob

nano cronjob.yaml

2. Add CronJob configuration: Add the following configuration to the cron-test.yaml file.

apiVersion: batch/v1beta1           
kind: CronJob
metadata:
name: cron-test
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: cron-test
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello this is Cron test
restartPolicy: OnFailure ## Restart Policy

3. Apply the CronJob

kubectl apply -f cron-test.yaml
Screenshot-from-2024-09-17-19-16-44

4. Get list of jobs

kubectl get jobs
Screenshot-from-2024-09-17-19-29-59

4. Check Logs for the Pod

kubectl logs cron-test-27935094
Screenshot-from-2024-09-17-19-33-43

Conclusion

In Conclusion, Kubernetes Jobs and CronJobs are robust tools that facilitate the effective scheduling and automation of both batch processes and recurring tasks. Jobs provide a mechanism for running one-time tasks, on the other hand, cronjobs execute tasks at regular intervals basis.


Article Tags :

Explore