天天看點

名詞解釋:CronJob

CronJob

CronJob Spec

  • .spec.schedule指定任務運作周期,格式同Cron
  • .spec.jobTemplate指定需要運作的任務,格式同Job
  • .spec.startingDeadlineSeconds指定任務開始的截止期限
  • .spec.concurrencyPolicy指定任務的并發政策,支援Allow、Forbid和Replace三個選項
apiVersion: batch/v2alpha1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure      
$ kubectl create -f cronjob.yaml
cronjob "hello" created      
kubectl run hello --schedule="*/1 * * * *" --restart=OnFailure --image=busybox -- /bin/sh -c "date; echo Hello from the Kubernetes cluster"      
$ kubectl get cronjob
NAME      SCHEDULE      SUSPEND   ACTIVE    LAST-SCHEDULE
hello     */1 * * * *   False     0         <none>
$ kubectl get jobs
NAME               DESIRED   SUCCESSFUL   AGE
hello-1202039034   1         1            49s
$ pods=$(kubectl get pods --selector=job-name=hello-1202039034 --output=jsonpath={.items..metadata.name} -a)
$ kubectl logs $pods
Mon Aug 29 21:34:09 UTC 2016
Hello from the Kubernetes cluster

# 注意,删除cronjob的時候不會自動删除job,這些job可以用kubectl delete job來删除
$ kubectl delete cronjob hello
cronjob "hello" deleted