CronJob运行自动化任务

CronJob运行自动化任务

创建

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

cronjob.yml

yaml 复制代码
apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox:1.28
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

创建

powershell 复制代码
root@k8s-master:~# vim cronjob.yml
root@k8s-master:~# kubectl apply -f cronjob.yml
cronjob.batch/hello created

获取状态

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

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

powershell 复制代码
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

powershell 复制代码
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
powershell 复制代码
root@k8s-master:~# kubectl logs hello-28293233--1-m9bzx
Wed Oct 18 01:53:01 UTC 2023
Hello from the Kubernetes cluster

删除

使用名称删除

powershell 复制代码
root@k8s-master:~# kubectl delete cronjob hello
cronjob.batch "hello" deleted
相关推荐
loong_XL14 分钟前
docker gpu容器镜像环境构建
运维·docker·容器
杨了个杨89821 小时前
nginx常见功能部署
运维·服务器·nginx
小天源3 小时前
linux漏洞一键扫描
linux·运维·服务器·漏洞扫描
eWidget3 小时前
InfluxDB迁移至金仓数据库的同城容灾实践:性能显著提升、运维效率优化,某能源企业实现RPO_5秒的高可靠时序数据管理
运维·数据库·能源·时序数据库·kingbase·kingbasees·金仓数据库
m0_696212683 小时前
个人微信api
运维·服务器
en-route4 小时前
SSH Key 与 GPG Key 区别详解:Git 使用中的身份与签名机制
运维·git·ssh
小白鸽i6 小时前
【LINUX】将源码驱动文件编译并生效
linux·运维·服务器
做萤石二次开发的哈哈7 小时前
萤石开放平台 音视频 | 产品概述
运维·服务器·萤石云·萤石·萤石开放平台
0思必得07 小时前
[Web自动化] 处理爬虫异常
运维·爬虫·python·selenium·自动化·web自动化
China_Yanhy8 小时前
我的区块链运维日记 · 第 4 日:死掉的“活”节点 —— 攻克“同步滞后(Lag)”
运维·区块链