
A Job creates one or more Pods and ensures that a specified number of them successfully terminate. Kubernetes Jobs are created to run pods for a short period of time which are running for completion as opposed to different objects in Kubernetes like Replicasets, Replication Controllers, Demonesets, which is run continuously.
In Kubernetes , we have two types of jobs:
- Run to completion
- Cronjobs
Run to Completion job
Run to completion job are basically ensure that a specified number of pods are successfully complete its task and terminate. As pods successfully complete, the Job tracks the successful completions. When a specified number of successful completions is reached, the Job is complete.
Example of run to completion job
- Create job.yml manifest file
apiVersion: batch/v1
kind: Job
metadata:
name: simple-job
spec:
completions: 5
parallelism: 2
ttlSecondsAfterFinished: 1
template:
spec:
containers:
- name: busybox
image: busybox
command: ["echo", "Kubernetes Job"]
restartPolicy: Never
The above manifest file has following terms:
completions
: This is set to a specified number means this job will run a specified number of pods for completing the task.parallelism
: This means that the number pods run in parallel to complete a task.ttlSecondsAfterFinished
: This is ttl controller which is used to clean up the completed job after a specified time.
2. Let’s deploy job using command:
$ kubectl create -f job.yml
3. To check the job status use command:
$ kubectl get jobs
NAME COMPLETIONS DURATION AGE
simple-job 5/5 41s 104s
4. Checking for pods initiate by jobs:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
simple-job-6949g 0/1 Completed 0 105s
simple-job-829nb 0/1 Completed 0 118s
simple-job-c8l5g 0/1 Completed 0 2m13s
simple-job-hzwnd 0/1 Completed 0 111s
simple-job-p7rlh 0/1 Completed 0 2m13s
The above job completed all the tasks and then terminated.
Cron jobs
A CronJob creates Jobs on a repeating schedule.
One CronJob object is like one line of a crontab (cron table) file. It runs a job periodically on a given schedule, written in Cron format.
CronJobs are useful for creating periodic and recurring tasks, like running backups or sending emails.
CronJobs can also schedule individual tasks for a specific time, such as scheduling a Job for when your cluster is likely to be idle.
Example for cronjob
- Create a cronjob.yml manifest:
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *" # run every minute
successfulJobsHistoryLimit: 1
failedJobsHistoryLimit: 1
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
This YAML will schedule a pod every one minute
schedule:
schedule time of its jobs to be created and executed.
successfulJobsHistoryLimit: 1
failedJobsHistoryLimit: 1
It specifies how many completed and failed jobs should be kept in a cluster.
2. Let’s deploy job using command:
$ kubectl create -f cronjob.yml
3. To check the job status use command:
$ kubectl get cronjobs
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 0 <none> 36s
4. Checking for pods initiate by jobs:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-1603945620-pw2nr 0/1 Completed 0 63s
hello-1603945680-rv6h2 0/1 Completed 0 12s
You can see it basically run the job at every 1 minute.
5. Cleaning up the things
You can use following command to delete cronjobs:
$ kubectl delete cronjobs/hello
Conclusion
Kubernetes Jobs are used when you want to create pods that will do a specific task and then exit. Jobs use completion and parallelism parameters to control the patterns the pods run through. Job pods can run as a single task, several sequential tasks, or some parallel tasks in which the first task that finishes instructs the rest of the pods to complete and exit.
References
https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/
https://kubernetes.io/docs/concepts/workloads/controllers/job/
