![]() |
VOOZH | about |
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.
Table of Content
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.
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
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.
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
1. Create a YAML file for the Job:
nano job-test.yaml2. 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.yaml4. Verify the Job
kubectl get jobs
kubectl describe job job-test
5. Check Logs of the Job Pod
kubectl get pods --selector=job-name=job-test
kubectl logs <pod-name>
1. Create a YAML file for the CronJob
nano cronjob.yaml2. 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.yaml4. Get list of jobs
kubectl get jobs4. Check Logs for the Pod
kubectl logs cron-test-27935094In 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.