Kubernetes provides two different controllers to manage batch workloads: Jobs and CronJobs. Let’s explore their definitions and use cases below.
Kubernetes Jobs
A Job in Kubernetes is a controller object that runs a pod or set of pods to completion. It is used for performing batch processing, such as running a script or a container that performs a task and exits when it is completed. Once the task is finished, the pod or set of pods is terminated.
The Job controller ensures that the pod or set of pods are created and scheduled correctly. It monitors the status of the pods and retries them if they fail, ensuring that the task is completed successfully. Jobs are typically used for running one-time or intermittent tasks, such as running a backup or running a script to update data in a database.
Here’s an example of a Job manifest file:
apiVersion: batch/v1
kind: Job
metadata:
name: myjob
spec:
template:
spec:
containers:
- name: myjob
image: myimage:latest
command: ["python"]
args: ["myjob.py"]
restartPolicy: OnFailure
backoffLimit: 4
In this example, we define a Job called myjob that runs a container based on the myimage:latest image, which executes a Python script called myjob.py. The restartPolicy is set to OnFailure, which means that if the pod fails to run, it will be restarted up to four times before the Job is marked as failed.
Kubernetes CronJobs
A CronJob in Kubernetes is a controller object that runs a Job on a scheduled basis. It is used for running periodic or recurring tasks, such as running a backup every night or running a data analysis job every week.
CronJobs are created using a cron expression, which specifies the schedule for running the Job. The Job controller ensures that the Job is created and scheduled at the correct time based on the cron expression.
Here’s an example of a CronJob manifest file:
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: mycronjob
spec:
schedule: "*/5 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: myjob
image: myimage:latest
command: ["python"]
args: ["myjob.py"]
restartPolicy: OnFailure
successfulJobsHistoryLimit: 3
In this example, we define a CronJob called mycronjob that runs a Job every five minutes. The Job runs a container based on the myimage:latest image, which executes a Python script called myjob.py. The restartPolicy is set to OnFailure, and the successfulJobsHistoryLimit is set to 3, which means that up to three successful Job runs will be kept in the history.
In conclusion, Kubernetes Jobs and CronJobs are two powerful controllers that enable batch processing and periodic or recurring tasks in a Kubernetes cluster. Jobs are used for running one-time or intermittent tasks, while CronJobs are used for running periodic or recurring tasks based on a schedule. By leveraging these controllers, you can easily automate various tasks in your Kubernetes cluster.