The Cronjob is running in Kubernetes like a Linux or Windows Cron schedule, this post will walk you through to understand the Cron Jobs and configure your Kubernetes cluster.
A CronJob can creates Jobs on a repeating schedule as your have configured in the K8S cluster. And its performing regular scheduled actions such as backups, report generation, and so on
Create a YAML file with CronJob code: hello-cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello-world
spec:
schedule: "*/10 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello-world
image: hello-world-text:1.20.1
command:
- /bin/sh
- -c
- date; echo Hello-World from the K8S cluster
restartPolicy: OnFailure
Execute the YAML code (hello-cronjob.yaml) using the command below,
$ kubectl create -f ./hello-cronjob.yaml
cronjob "hello-world" created
After creating the cron job, if you want to view the running CronJob with the command below,
$ kubectl get cronjob hello-world
NAME SCHEDULE SUSPEND ACTIVE LAST-SCHEDULE
hello-world */10 * * * * False 0
hello-world */10 * * * * False 0
And also, watch the running jobs which is triggered by CronJob around 10 minutes:
$ kubectl get jobs --watch
NAME DESIRED SUCCESSFUL AGE
hello-world-206356 1 1 2s
If you would like to delete the CronJob and job use the command below,
$ kubectl delete cronjob hello-world
cronjob "hello-world" deleted
This stops new jobs from being created. However, running jobs won't be stopped, and no jobs or their pods will be deleted. To clean up those jobs and pods, you need to list all jobs created by the cron job, and delete them all.
$ kubectl delete jobs hello-world-206356
job "hello-world-206356" deleted
Comments (0)