前言

容器分为两类

  1. 在线业务 - 服务类容器

    需要持续不中断的提供服务,容器需要一直运行

  2. 离线业务 - 工作类容器

    一般执行一次性任务,比如统计日志数据等,运行完成后容器即可关闭

更新历史

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 或者 onFailure

启动

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

运行状态

[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

查看运行结果

[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 会进行阻止

[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

微信扫码关注


左程立
71 声望13 粉丝