|NO.Z.00199|——————————|CloudNative|——|KuberNetes&高级调度.V02|——|CronJob.v02|创建计划任务|

一、通过命令创建ConJob
### --- 创建一个CronJob(新版kubectl可能无此命令,需要使用下面的yaml创建):

[root@k8s-master01 ~]# kubectl run hello --schedule="*/2 * * * *" --restart=OnFailure --image=nginx --image-pull-policy=IfNotPresent -- /bin/sh -c "date"
kubectl run --generator=cronjob/v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
cronjob.batch/hello created
[root@k8s-master01 ~]# kubectl get cj
NAME    SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
hello   */2 * * * *   False     0        <none>          5s
二、通过yaml配置文件创建ConJob
### --- 创建hello-cronjob.nginx.yaml配置文件
~~~     conjob的yaml文件

[root@k8s-master01 ~]# vim hello-cronjob.nginx.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  labels:
    run: hello
  name: hello
  namespace: default
spec:
  startingDeadlineSeconds: 30           # 比如这个计划2分钟执行一次,若是执行失败了,就会用到这个参数,就是在30s之内再调用一次,若是失败,直到调用成功即可。
  concurrencyPolicy: Allow              # 并发调度策略:Allow运行同时运行过个任务。
                                        # Forbid:不运行并发执行。
                                        # Replace:替换之前的任务
  failedJobsHistoryLimit: 1             # 保留失败的任务数。
  jobTemplate:
    metadata:
      creationTimestamp: null
    spec:
      template:
        metadata:
          creationTimestamp: null
          labels:
            run: hello
        spec:
          containers:
          - args:
            - /bin/sh
            - -c
            - date
            image: nginx
            imagePullPolicy: IfNotPresent
            name: hello
            resources: {}
            terminationMessagePath: /dev/termination-log
            terminationMessagePolicy: File
          dnsPolicy: ClusterFirst
          restartPolicy: OnFailure
          schedulerName: default-scheduler
          securityContext: {}
          terminationGracePeriodSeconds: 30
  schedule: '*/2 * * * *'               # 调度的策略 分时日月周
  successfulJobsHistoryLimit: 3         # 成功的Job保留的次数
  suspend: false                        # 挂起,true:cronjob不会被执行。
status: {}
### --- 注释说明:

apiVersion: batch/v1beta1           // apiVersion的版本
  labels:                           // labels
  namespace: default                // 也有namespace隔离的概念
spec:
  startingDeadlineSeconds: 30       // 比如这个计划2分钟执行一次,若是执行失败了,就会用到这个参数,就是在30s之内再调用一次,若是失败,直到调用成功即可。
  concurrencyPolicy: Allow          // 并发调度策略:Allow运行同时运行过个任务。
                                    // 设置为: Forbid:不运行并发执行;上一个任务没有执行完成,不会执行下一个任务。
                                    // 设置为:Replace:替换之前的任务
  failedJobsHistoryLimit: 1         // 保留失败的任务数;任务数设置长一些,若是你的job失败了,需要追踪原因
          restartPolicy: OnFailure  // 与pod设置一致即可。重启策略
  schedule: '*/2 * * * *'           // 调度的策略 分时日月周
  successfulJobsHistoryLimit: 3     // 成功的Job保留的次数
  suspend: false                    // 挂起,true:cronjob不会被执行。
status: {}
三、创建hello-cronjob.nginx容器
### --- 创建并查看cronjob

[root@k8s-master01 ~]# kubectl create -f hello-nginx.yaml 
[root@k8s-master01 ~]# kubectl get cj
NAME    SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
hello   */2 * * * *   False     1        11s             2m4s 
### --- 验证执行结果

[root@k8s-master01 ~]# kubectl get po
NAME                          READY   STATUS      RESTARTS   AGE
busybox                       1/1     Running     125        9d
demo-nginx-67ff7f7bb5-984xl   2/2     Running     0          3h18m
demo-nginx-67ff7f7bb5-sz9nv   2/2     Running     0          3h18m
hello-1619695680-bt7bz        0/1     Completed   0          5m42s
hello-1619695800-cz7wr        0/1     Completed   0          3m39s
hello-1619695920-tb9q8        0/1     Completed   0          107s
~~~     时间误差几秒钟,因为容器启动它是需要时间的,这几秒的误差也是可以的,
~~~     即使你在宿主机上执行也会有误差的。

[root@k8s-master01 ~]# kubectl logs -f hello-1619695680-bt7bz           
Thu Apr 29 11:28:10 UTC 2021
[root@k8s-master01 ~]# kubectl logs -f hello-1619695800-cz7wr 
Thu Apr 29 11:30:12 UTC 2021
[root@k8s-master01 ~]# kubectl logs -f hello-1619695920-tb9q8 
Thu Apr 29 11:32:05 UTC 2021

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(30)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示