K8S實戰(八)| Job 和 CronJob

前言

容器分爲兩類html

  1. 在線業務 - 服務類容器

    須要持續不中斷的提供服務,容器須要一直運行linux

  2. 離線業務 - 工做類容器

    通常執行一次性任務,好比統計日誌數據等,運行完成後容器便可關閉api

更新歷史

Job

cat job.yaml微信

apiVersion: batch/v1
kind: Job
metadata:
  name: job-test
spec:
  template:
    metadata:
      name: job-test
    spec:
      containers:
      - name: test-job
        image: busybox
        command: ["echo", "test job!"]
      restartPolicy: Never

參數
restartPolicy 只能是 Never 或者 onFailureapp

啓動oop

[root@master01 ~]# kubectl apply -f job.yaml 
job.batch/job-test created

運行狀態spa

[root@master01 ~]# kubectl get pod
NAME             READY   STATUS              RESTARTS   AGE
job-test-t2gbw   0/1     ContainerCreating   0          12s

[root@master01 ~]# kubectl get pod
NAME             READY   STATUS      RESTARTS   AGE
job-test-t2gbw   0/1     Completed   0          24s

查看運行結果rest

[root@master01 ~]# kubectl logs job-test-t2gbw
test job!

Job 執行失敗如何處理

將 command 胡亂設置致使 job 沒法成功啓動日誌

command: ["echo123", "test job!"]
  restartPolicy: Never

當 restartPolicy: Never 時,能夠看到 k8s 在不斷的開新的 pod
但不會讓它一直開下去,默認參數 spec.backoffLimit: 6 會進行阻止code

[root@master01 ~]# kubectl get pod
NAME             READY   STATUS               RESTARTS   AGE
job-test-2zrt4   0/1     ContainerCannotRun   0          87s
job-test-5z884   0/1     ContainerCannotRun   0          77s
job-test-cxgmm   0/1     ContainerCannotRun   0          107s
job-test-nt9gt   0/1     ContainerCannotRun   0          57s
job-test-wxv7l   0/1     ContainerCreating    0          17s

當 restartPolicy: OnFailure 時,k8s 不開新 pod,只會不斷重啓 pod

[root@master01 ~]# kubectl get pod
NAME             READY   STATUS             RESTARTS   AGE
job-test-bd695   0/1     CrashLoopBackOff   4          3m15s

若是 job 沒有報錯,可是它一直不結束怎麼辦?

參數 spec.activeDeadlineSeconds: 100 會在 100s 後將 Job 中全部 Pod 進行關閉
此時 Pod 關閉狀態爲 reason: DeadlineExceeded

並行 Job

cat job.yaml

apiVersion: batch/v1
kind: Job
metadata:
  name: job-test
spec:
  parallelism: 2
  ......

參數
parallelism: 2 僅容許最多兩個 Pod 同時運行

查看運行狀況

[root@master01 ~]# kubectl get pod
NAME             READY   STATUS              RESTARTS   AGE
job-test-6ttz9   0/1     ContainerCreating   0          8s
job-test-mknjc   0/1     ContainerCreating   0          8s

[root@master01 ~]# kubectl get pod
NAME             READY   STATUS      RESTARTS   AGE
job-test-6ttz9   0/1     Completed   0          68s
job-test-mknjc   0/1     Completed   0          68s

[root@master01 ~]# kubectl get job
NAME       COMPLETIONS   DURATION   AGE
job-test   0/1 of 2      10s        10s
[root@master01 ~]# kubectl get job
NAME       COMPLETIONS   DURATION   AGE
job-test   2/1 of 2      19s        66s

增長參數 completions

apiVersion: batch/v1
kind: Job
metadata:
  name: job-test
spec:
  parallelism: 2
  completions: 8

參數
completions: 8 表示至少成功運行 8 個 pod
若是不是8個成功,那麼會根據 restartPolicy 的策略進行處理
能夠認爲是一種檢查機制

查看運行狀況

[root@master01 ~]# kubectl get pod
NAME             READY   STATUS              RESTARTS   AGE
job-test-6xwpt   0/1     ContainerCreating   0          8s
job-test-grk4q   0/1     ContainerCreating   0          8s
[root@master01 ~]# kubectl get job
NAME       COMPLETIONS   DURATION   AGE
job-test   0/8           11s        11s


[root@master01 ~]# kubectl get job
NAME       COMPLETIONS   DURATION   AGE
job-test   6/8           76s        76s

[root@master01 ~]# kubectl get pod
NAME             READY   STATUS      RESTARTS   AGE
job-test-2jxqx   0/1     Completed   0          28s
job-test-6xwpt   0/1     Completed   0          90s
job-test-gndq7   0/1     Completed   0          70s
job-test-grk4q   0/1     Completed   0          90s
job-test-n96k6   0/1     Completed   0          70s
job-test-np4n7   0/1     Completed   0          49s
job-test-scc9c   0/1     Completed   0          28s
job-test-zcnbp   0/1     Completed   0          49s

[root@master01 ~]# kubectl get job
NAME       COMPLETIONS   DURATION   AGE
job-test   8/8           82s        91s

CronJob

CronJob 會定時 create 一個 job 對象

cat cronjob.yaml

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            command: ["echo","test cron job!"]
          restartPolicy: OnFailure

查看運行結果

[root@master01 ~]# kubectl get cronjob
NAME    SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
hello   */1 * * * *   False     0        67s             3m45s

[root@master01 ~]# kubectl get jobs
NAME               COMPLETIONS   DURATION   AGE
hello-1595319480   1/1           27s        3m23s
hello-1595319540   1/1           19s        2m23s
hello-1595319600   1/1           24s        83s
hello-1595319660   0/1           22s        22s

[root@master01 ~]# kubectl get pod
NAME                     READY   STATUS              RESTARTS   AGE
hello-1595319480-2kl8h   0/1     Completed           0          3m21s
hello-1595319540-gwthv   0/1     Completed           0          2m21s
hello-1595319600-54548   0/1     Completed           0          81s
hello-1595319660-8xqn6   0/1     ContainerCreating   0          20s

能夠看到,就是把前面說過的 Job 對象進行定時的運行。

結束語

Job/CronJob 控制器能夠進行離線業務的處理,不須要長期運行一個 Pod。

這類控制器管理的 Pod 不對外提供持續在線服務,任務運行完成後,即會將容器進行關閉,能夠用於一些定時數據處理類的工做。

聯繫我

微信公衆號:zuolinux_com

微信掃碼關注

相關文章
相關標籤/搜索