容器按照持續運行的時間可分爲兩類:服務類容器和工做類容器。linux
服務類容器一般持續提供服務,須要一直運行,好比 http server,daemon 等。工做類容器則是一次性任務,好比批處理程序,完成後容器就退出。docker
Kubernetes 的 Deployment、ReplicaSet 和 DaemonSet 都用於管理服務類容器;對於工做類容器,咱們用 Job。vim
一、編寫job的yaml文件myjob.yaml:api
[root@ren7 yaml]# cat myjob.yaml apiVersion: batch/v1 kind: Job metadata: name: job spec: template: spec: containers: - name: job image: reg.yunwei.com/learn/busybox:latest command: ["echo","hello kubernetes"] restartPolicy: Never
① batch/v1 是當前 Job 的 apiVersion。app
② 指明當前資源的類型爲 Job。oop
③ restartPolicy 指定什麼狀況下須要重啓容器。對於 Job,只能設置爲 Never 或者 OnFailure。對於其餘 controller(好比 Deployment)能夠設置爲 Always 。ui
二、啓動jobspa
[root@ren7 yaml]# kubectl apply -f myjob.yaml job.batch/job created
三、查看job的狀態rest
[root@ren7 yaml]# kubectl get job NAME COMPLETIONS DURATION AGE job 1/1 3s 84s
四、查看pod的狀態日誌
[root@ren7 yaml]# kubectl get pod NAME READY STATUS RESTARTS AGE job-m9shb 0/1 Completed 0 2m40s
顯示completed已經完成
五、查看pod的標準輸出
[root@ren7 yaml]# kubectl logs job-m9shb hello kubernetes
若是job失敗了會怎麼樣呢?
一、修改myjob.yaml文件,故意引入一個錯誤:
[root@ren7 yaml]# cat myjob.yaml apiVersion: batch/v1 kind: Job metadata: name: job spec: template: spec: containers: - name: job image: reg.yunwei.com/learn/busybox:latest command: ["echorrr","hello kubernetes"] restartPolicy: Never
二、刪除以前的job
[root@ren7 yaml]# kubectl delete -f myjob.yaml job.batch "job" deleted [root@ren7 yaml]# kubectl get pod No resources found.
三、運行新的job並查看狀態
[root@ren7 yaml]# kubectl apply -f myjob.yaml job.batch/job created [root@ren7 yaml]# kubectl get pod NAME READY STATUS RESTARTS AGE job-l9spb 0/1 ContainerCannotRun 0 5s job-mk5fp 0/1 ContainerCreating 0 1s [root@ren7 yaml]# kubectl get pod NAME READY STATUS RESTARTS AGE job-2phbq 0/1 ContainerCreating 0 2s job-l9spb 0/1 ContainerCannotRun 0 16s job-mk5fp 0/1 ContainerCannotRun 0 12s
發現沒有建立成功,顯示0
而且能夠看到有多個pod,狀態均不正常。
四、使用 kubectldescribe pod 查看某個pod的啓動日誌
[root@ren7 yaml]# kubectl describe pods job-2phbq Name: job-2phbq Namespace: default Node: 192.168.11.5/192.168.11.5 Start Time: Fri, 25 Oct 2019 19:02:56 +0800 Labels: controller-uid=f7746782-f716-11e9-867d-000c297d011c job-name=job Annotations: <none> Status: Failed IP: 172.20.72.146 Controlled By: Job/job Containers: job: Container ID: docker://858e1dffa45cad2e43fa096b002a925f15dfc07ad08b406c4c2ad01fe68c2ccd Image: reg.yunwei.com/learn/busybox:latest Image ID: docker-pullable://reg.yunwei.com/learn/busybox@sha256:dd97a3fe6d721c5cf03abac0f50e2848dc583f7c4e41bf39102ceb42edfd1808 Port: <none> Host Port: <none> Command: echorrr hello kubernetes State: Terminated Reason: ContainerCannotRun Message: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"echorrr\": executable file not found in $PATH": unknown Exit Code: 127 Started: Fri, 25 Oct 2019 19:02:58 +0800 Finished: Fri, 25 Oct 2019 19:02:58 +0800 Ready: False Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-qvqql (ro) Conditions: Type Status Initialized True Ready False ContainersReady False PodScheduled True Volumes: default-token-qvqql: Type: Secret (a volume populated by a Secret) SecretName: default-token-qvqql Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: <none> Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 11m default-scheduler Successfully assigned default/job-2phbq to 192.168.11.5 Normal Pulling 11m kubelet, 192.168.11.5 Pulling image "reg.yunwei.com/learn/busybox:latest" Normal Pulled 11m kubelet, 192.168.11.5 Successfully pulled image "reg.yunwei.com/learn/busybox:latest" Normal Created 11m kubelet, 192.168.11.5 Created container job Warning Failed 11m kubelet, 192.168.11.5 Error: failed to start container "job": Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"echorrr\": executable file not found in $PATH": unknown
日誌顯示沒有可執行程序符合咱們的預期。
爲何kubectl get pod 會看到這麼多個失敗的pod?
緣由是:當第一個 Pod 啓動時,容器失敗退出,根據 restartPolicy: Never,此失敗容器不會被重啓,但 Job DESIRED 的 Pod 是 1,目前 SUCCESSFUL 爲 0,不知足,因此 Job controller 會啓動新的 Pod,直到 SUCCESSFUL 爲 1。對於咱們這個例子,SUCCESSFUL 永遠也到不了 1,因此 Job controller 會一直建立新的 Pod。爲了終止這個行爲,只能刪除 Job。
[root@ren7 yaml]# kubectl delete -f myjob.yaml job.batch "job" deleted [root@ren7 yaml]# kubectl get pod No resources found.
若是將 restartPolicy 設置爲 OnFailure 會怎麼樣?下面咱們實踐一下,修改 myjob.yml 後從新啓動。
[root@ren7 yaml]# vim myjob.yaml #依舊是錯誤命令,且改了restartPolicy: OnFailure [root@ren7 yaml]# kubectl apply -f myjob.yaml job.batch/job created [root@ren7 yaml]# kubectl get pod NAME READY STATUS RESTARTS AGE job-9fd6w 0/1 ContainerCreating 0 2s [root@ren7 yaml]# kubectl get pod NAME READY STATUS RESTARTS AGE job-9fd6w 0/1 RunContainerError 0 14s [root@ren7 yaml]# kubectl get pod NAME READY STATUS RESTARTS AGE job-9fd6w 0/1 RunContainerError 1 27s [root@ren7 yaml]# kubectl get pod NAME READY STATUS RESTARTS AGE job-9fd6w 0/1 CrashLoopBackOff 2 36s
完成依然爲0。
這裏只有一個 Pod,不過 RESTARTS 爲 2,並且不斷增長,說明 OnFailure 生效,容器失敗後會自動重啓。
有時,咱們但願能同時運行多個pod,提升job的執行效率。
一、設置parallelism
[root@ren7 yaml]# cat myjob.yaml apiVersion: batch/v1 kind: Job metadata: name: job spec: parallelism: 2 template: spec: containers: - name: job image: reg.yunwei.com/learn/busybox:latest restartPolicy: Never
二、執行該yaml文件
[root@ren7 yaml]# kubectl apply -f myjob.yaml job.batch/job created
三、查看job
[root@ren7 yaml]# kubectl get pod NAME READY STATUS RESTARTS AGE job-5jkpz 0/1 ContainerCreating 0 3s job-svs94 0/1 ContainerCreating 0 3s [root@ren7 yaml]# kubectl get pod NAME READY STATUS RESTARTS AGE job-5jkpz 0/1 Completed 0 10s job-svs94 0/1 Completed 0 10s
job一共啓動了兩個pod,並且AGE相同,可見是並行運行的。
四、還能夠經過 completions 設置 Job 成功完成 Pod 的總數:
[root@ren7 yaml]# cat myjob.yaml apiVersion: batch/v1 kind: Job metadata: name: job spec: parallelism: 2 completions: 6 template: spec: containers: - name: job image: reg.yunwei.com/learn/busybox:latest restartPolicy: Never
五、查看相應的pod和job
[root@ren7 yaml]# kubectl get pod NAME READY STATUS RESTARTS AGE job-82gll 0/1 Completed 0 18s job-gsn55 0/1 Completed 0 18s job-h6gnw 0/1 Completed 0 11s job-nz5vs 0/1 Completed 0 14s job-v48cr 0/1 Completed 0 15s job-vx5gt 0/1 Completed 0 10s [root@ren7 yaml]# kubectl get job NAME COMPLETIONS DURATION AGE job 6/6 11s 72s
若是不指定 completions 和 parallelism,默認值均爲 1。
上面的例子只是爲了演示 Job 的並行特性,實際用途不大。不過現實中確實存在不少須要並行處理的場景。好比批處理程序,每一個副本(Pod)都會從任務池中讀取任務並執行,副本越多,執行時間就越短,效率就越高。這種相似的場景均可以用 Job 來實現。
Linux 中有 cron 程序定時執行任務,Kubernetes 的 CronJob 提供了相似的功能,能夠定時執行 Job。
一、CronJob 配置文件示例以下:
[root@ren7 yaml]# cat myjob1.yaml apiVersion: batch/v1beta1 kind: CronJob metadata: name: timing spec: schedule: "*/1 * * * *" jobTemplate: spec: template: spec: containers: - name: timing image: reg.yunwei.com/learn/busybox:latest command: ["echo","hello k8s cronjob!"] restartPolicy: OnFailure
① batch/v1beta1 是當前 CronJob 的 apiVersion。
② 指明當前資源的類型爲 CronJob。
③ schedule 指定何時運行 Job,其格式與 Linux cron 一致。這裏 */1 * * * * 的含義是每一分鐘啓動一次。
④ jobTemplate 定義 Job 的模板,格式與前面 Job 一致。
二、經過 kubectl apply 建立 CronJob
[root@ren7 yaml]# kubectl apply -f myjob1.yaml cronjob.batch/timing created
三、查看crontab的狀態
[root@ren7 yaml]# kubectl get job No resources found. [root@ren7 yaml]# kubectl get cronjob NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE timing */1 * * * * False 0 <none> 15s
四、等待幾分鐘再次查看jobs的執行狀況
[root@ren7 yaml]# kubectl get job NAME COMPLETIONS DURATION AGE timing-1572005160 1/1 3s 2m29s timing-1572005220 1/1 3s 89s timing-1572005280 1/1 3s 29s
能夠看到每隔一分鐘就會啓動一個job。
過段時間查看pod
[root@ren7 yaml]# kubectl get pod NAME READY STATUS RESTARTS AGE timing-1572005340-5fds8 0/1 Completed 0 2m40s timing-1572005400-8qsd4 0/1 Completed 0 99s timing-1572005460-hnsh7 0/1 Completed 0 39s
五、執行kubectl logs 可查看某個 Job 的pod運行日誌:
[root@ren7 yaml]# kubectl logs timing-1572005340-5fds8 hello k8s cronjob!
查看pod會遺留不少已經完成的pod,只須要刪除便可。
[root@ren7 yaml]# kubectl delete -f myjob1.yaml cronjob.batch "timing" deleted [root@ren7 yaml]# kubectl get pods No resources found.