1. Cronjob定时任务

  • CronJob用于实现定时任务,像Linux的Crontab一样。

    • 定时任务
  • 应用场景:通知,备份

  • 示例代码

    [root@k8s-master deployment]# vim cronjob.yaml
    [root@k8s-master deployment]# cat cronjob.yaml 
    apiVersion: batch/v1beta1
    kind: CronJob
    metadata:
      name: hello
    spec:
      schedule: "*/1 * * * *"
      jobTemplate:
        spec:
          template:
            spec:
              containers:
              - name: hello
                image: busybox
                args:
                - /bin/sh
                - -c
                - date; echo Hello aliang
              restartPolicy: OnFailure
    
    

2. 案例

  • 编写定时任务配置

    [root@k8s-master deployment]# vim cronjob.yaml
    [root@k8s-master deployment]# cat cronjob.yaml 
    apiVersion: batch/v1beta1
    kind: CronJob
    metadata:
      name: hello
    spec:
      schedule: "*/1 * * * *"
      jobTemplate:
        spec:
          template:
            spec:
              containers:
              - name: hello
                image: busybox
                args:
                - /bin/sh
                - -c
                - date; echo Hello aliang
              restartPolicy: OnFailure
    
    
  • 执行启动

    [root@k8s-master deployment]# kubectl apply -f cronjob.yaml 
    cronjob.batch/hello created
    
  • 查询运行状态

    [root@k8s-master deployment]# kubectl get cronjob
    NAME    SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
    hello   */1 * * * *   False     0        56s             19m
    
    [root@k8s-master deployment]# kubectl get pods
    NAME                     READY   STATUS      RESTARTS   AGE
    hello-1606896780-rcrm8   0/1     Completed   0          2m21s
    hello-1606896840-tt8dw   0/1     Completed   0          81s
    hello-1606896900-tghzz   0/1     Completed   0          21s
    pi-7bbgz                 0/1     Completed   0          43m
    
  • 查看日志

    [root@k8s-master deployment]# kubectl logs hello-1606896900-tghzz
    Wed Dec  2 08:15:13 UTC 2020
    Hello aliang
    
    

相关文章:

  • 2021-08-30
  • 2021-06-29
  • 2022-12-23
  • 2021-09-23
  • 2022-02-12
  • 2021-12-14
  • 2021-12-18
  • 2021-09-15
猜你喜欢
  • 2021-11-20
  • 2022-12-23
  • 2021-11-26
  • 2021-09-20
  • 2023-04-03
  • 2022-12-23
相关资源
相似解决方案