• CronJob运行自动化任务


    CronJob运行自动化任务

    创建

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

    cronjob.yml

    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    创建

    root@k8s-master:~# vim cronjob.yml
    root@k8s-master:~# kubectl apply -f cronjob.yml
    cronjob.batch/hello created
    
    
    • 1
    • 2
    • 3
    • 4

    获取状态

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

    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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

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

    可以看到当前有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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    root@k8s-master:~# kubectl logs hello-28293233--1-m9bzx
    Wed Oct 18 01:53:01 UTC 2023
    Hello from the Kubernetes cluster
    
    
    • 1
    • 2
    • 3
    • 4

    删除

    使用名称删除

    root@k8s-master:~# kubectl delete cronjob hello
    cronjob.batch "hello" deleted
    
    
    • 1
    • 2
    • 3
  • 相关阅读:
    为了提前预测比赛结果,于是我用Python获取比赛球员数据进行分析,结果...
    threadx netxduo stm32f407上实现http server
    IntE IDEA下载及安装
    云贝教育 | 【技术文章】pg中的两阶段提交
    贪吃蛇和俄罗斯方块游戏
    内网渗透之Socks代理简介
    Yocto Beaglebone-Black 编译记录
    JVM类加载机制
    Linux sed命令增删改查 附代码
    Java面试、面经丨从试题到面试讲个遍
  • 原文地址:https://blog.csdn.net/weixin_51882166/article/details/133910953