Kubernetes CronJob

CronJob

CronJob用於執行常規的計劃操作(如備份、報告生成等)。

格式

* * * * * 分時日月周

創建一個 Job

[root@master01 job]# kubectl create -f cronjob.yaml 
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox:1.28
            imagePullPolicy: IfNotPresent
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

查看執行結果

[root@master01 job]# kubectl get jobs --watch
NAME               COMPLETIONS   DURATION   AGE
hello-1673882700   1/1           2s         94s
hello-1673882760   1/1           1s         33s

刪除

kubectl delete cronjob hello

參數介紹

[root@master01 job]# kubectl get cj hello -oyaml
kubectl get cj hello -oyaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  labels:
    run: hello
  name: hello
  namespace: default
spec:
  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: '*/1 * * * *'  #調度的策略 分時日月周
  successfulJobsHistoryLimit: 3 # 成功的Job保留的次數
  suspend: false # 掛起,true:cronjob不會被執行。
status: {}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章