CronJob
CronJob
Create the new file with the command:
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: cronjob-hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: cron-hello
image: r.deso.tech/library/alpine
args:
- /bin/sh
- -c
- date; echo Hello, Desotech Student!
restartPolicy: OnFailure
For cronjob schedulation you can follow this link: https://crontab.guru/
Save and close the file, apply it.
kubectl apply -f cronjob-hello.yaml
cronjob.batch/cronjob-hello created
You can ensure the cronjob task is running with the command:
kubectl get cronjobs
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE cronjob-hello */10 * * * * False 0 <none> 28s
How to watch for the cronjob? Let’s say you’ve deployed a container with an associated cronjob and you want to watch for it. To do that, you’d issue the command:
kubectl get jobs --watch
The output of that command will include a running list of every deployed job
NAME COMPLETIONS DURATION AGE cronjob-hello-1607447700 0/1 0s cronjob-hello-1607447700 0/1 0s 0s cronjob-hello-1607447700 1/1 4s 4s cronjob-hello-1607447760 0/1 0s cronjob-hello-1607447760 0/1 0s 0s cronjob-hello-1607447760 0/1 0s 1s cronjob-hello-1607447760 1/1 3s 4s
To end that watch, use the ++ctrl++ + ++c++ combination. A new job will be started every minute.
kubectl get pod
NAME READY STATUS RESTARTS AGE cronjob-hello-1607447700-5mqsq 0/1 Completed 0 2m28s cronjob-hello-1607447760-cnbjs 0/1 Completed 0 98s
Delete cronjob:
kubectl delete cronjob cronjob-hello
cronjob.batch "cronjob-hello" deleted
By deleting the cronjob you will also delete the completed pods.
kubectl get pod
No resources found in namespace01 namespace.