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
相关推荐
凡间客1 天前
Ansible安装与入门
linux·运维·ansible
_OP_CHEN1 天前
Linux网络编程:(八)GCC/G++ 编译器完全指南:从编译原理到实战优化,手把手教你玩转 C/C++ 编译
linux·运维·c++·编译和链接·gcc/g++·编译优化·静态链接与动态链接
阿乐艾官1 天前
【十一、Linux管理网络安全】
linux·运维·web安全
整列机厂家-东莞唯思特1 天前
异形零件自动化排列:整列机与传统振动盘的 5 大技术维度解析
科技·自动化·制造
LoneEon1 天前
告别手动操作:用 Ansible 统一管理你的 Ubuntu 服务器集群
运维·服务器·ansible
百***67031 天前
Nginx搭建负载均衡
运维·nginx·负载均衡
梁正雄1 天前
17、grafana安装
运维·grafana·prometheus·监控
摘星|1 天前
架设一台NFS服务器,并按照以下要求配置
linux·运维·服务器
做运维的阿瑞1 天前
Linux环境变量持久化完全指南
linux·运维·服务器
Mr_Dwj1 天前
【运维】GNU/Linux 入门笔记
linux·运维·gnu