当前位置:首页 > 编程笔记 > 正文
已解决

CronJob运行自动化任务

来自网友在路上 151851提问 提问时间:2023-10-20 07:26:59阅读次数: 51

最佳答案 问答题库518位专家为你答疑解惑

CronJob运行自动化任务

创建

创建一个cronjob,每一分钟输出日期和指定信息

cronjob.yml

apiVersion: batch/v1
kind: CronJob
metadata:name: hello
spec:schedule: "*/1 * * * *"jobTemplate:spec:template:spec:containers:- name: helloimage: busybox:1.28imagePullPolicy: IfNotPresentcommand:- /bin/sh- -c- date; echo Hello from the Kubernetes clusterrestartPolicy: OnFailure

创建

root@k8s-master:~# vim cronjob.yml
root@k8s-master:~# kubectl apply -f cronjob.yml
cronjob.batch/hello created

获取状态

root@k8s-master:~# kubectl get cronjob
NAME    SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
hello   */1 * * * *   False     0        <none>          24s

CronJob 还没有调度或执行任何任务。大约需要一分钟任务才能创建好。

root@k8s-master:~# kubectl get jobs --watch
NAME             COMPLETIONS   DURATION   AGE
hello-28293226   1/1           2s         2m8s
hello-28293227   1/1           1s         68s
hello-28293228   1/1           2s         8s

看到了一个运行中的任务被 “hello” CronJob 调度。 你可以停止监视这个任务,然后再次查看 CronJob 就能看到它调度任务

root@k8s-master:~# kubectl get cronjob
NAME    SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
hello   */1 * * * *   False     0        19s             4m49s

可以看到当前有0个活跃的任务,意味着任务执行完毕或者执行失败。

查看运行的Pod

root@k8s-master:~# kubectl get pod | grep hello
hello-28293233--1-m9bzx                   0/1     Completed   0              2m7s
hello-28293234--1-czd7t                   0/1     Completed   0              67s
hello-28293235--1-jh7l5                   0/1     Completed   0              7s
root@k8s-master:~# kubectl logs hello-28293233--1-m9bzx
Wed Oct 18 01:53:01 UTC 2023
Hello from the Kubernetes cluster

删除

使用名称删除

root@k8s-master:~# kubectl delete cronjob hello
cronjob.batch "hello" deleted
查看全文

99%的人还看了

相似问题

猜你感兴趣

版权申明

本文"CronJob运行自动化任务":http://eshow365.cn/6-20173-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!