start
root@ubuntu-200470-1:~/cronjob# cat cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
kubectl create -f cronjob.yaml
00470-1:~/cronjob# kubectl get jobs --watch
NAME COMPLETIONS DURATION AGE
hello-28276275 1/1 21s 3m1s
hello-28276276 1/1 19s 2m1s
hello-28276277 1/1 22s 61s
hello-28276278 0/1 1s 1s
process-item-apple 1/1 65s 15m
process-item-banana 1/1 68s 15m
process-item-cherry 1/1 66s 15m
^Croot@ubuntu-200470-1:~/cronjob# pods=$(kubectl get pods --selector=job-name=hello-28276276 --output=jsonpath={.items[*].metadata.name})
root@ubuntu-200470-1:~/cronjob#
root@ubuntu-200470-1:~/cronjob# echo $pods
hello-28276276-fn86r
root@ubuntu-200470-1:~/cronjob# kubectl logs $pods
Fri Oct 6 07:16:07 UTC 2023
Hello from the Kubernetes cluster
root@ubuntu-200470-1:~/cronjob# kubectl create -f cronjob.yaml
Warning: batch/v1beta1 CronJob is deprecated in v1.21+, unavailable in v1.25+; use batch/v1 CronJob
cronjob.batch/hello created