k8s-cronjob-自动删除

使用 K8s CronJob 记录

在公司私有集群中部署了一个定时服务,结果一段时间之后产生了 大量的 UnavailablePods.
总结下操作的步骤

  1. 直接在 rancher 服务上创建 CronJob:

    • 配置了 镜像地址 , 服务名字,最大失败/成功数量…

    但结果就是 Pod 无限增长,所以开始另辟蹊径. 通过 yaml 创建 CronJob

  2. 初始 application/job/cronjob.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    apiVersion: batch/v1
    kind: CronJob
    metadata:
    name: hello
    spec:
    schedule: "*/1 * * * *"
    jobTemplate:
    spec:
    template:
    spec:
    containers:
    - name: hello
    image: busybox
    imagePullPolicy: IfNotPresent
    command:
    - /bin/sh
    - -c
    - date; echo Hello from the Kubernetes cluster
    restartPolicy: OnFailure

    执行结果:

    - `batch/v1` 不存在 , 当前k8s环境只有 `batch/v1beta1` 🤦‍♂️

    修改后结果一样,不会自动删除

  3. 查询涉及 Pod 自动删除的 配置项

    • ttlSecondsAfterFinished 执行完 过指定时间后 自动删除 Pod :

      结果是第一次放错 .spec, 正确路径 .jobTemplate.spec.ttlSecondsAfterFinished.
      但是使用后还是不OK

    • backoffLimit 允许失败次数;无啥用

    • failedJobsHistoryLimit: 保留失败 Pod 数: 无啥用

    • successfulJobsHistoryLimit: 成功 Pod

      修改后的 yaml,依然不会自动删除 Pod !

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      apiVersion: batch/v1beta1
      kind: CronJob
      metadata:
      name: saas-report-cronjob-wek8
      spec:
      schedule: "0 8 * * 1"
      successfulJobsHistoryLimit: 1
      failedJobsHistoryLimit: 1
      jobTemplate:
      spec:
      ttlSecondsAfterFinished: 10
      backoffLimit: 0
      template:
      spec:
      containers:
      - name: saas-report-cronjon-wek8
      image: iiiii:dev
      imagePullPolicy: Always
      command:
      - producer
      - wek_8
      restartPolicy: Never
  4. 排查 Pod 状态

    • 任务 Pod 上的状态 一直都是 NotReady
      . 猜测因为启用了 istio 网关, 其中代理网关一直都是 running. 所以健康检查一直都是 未就绪 的状态,这里 也就不存在执行失败/成功的状态了

    • failedJobsHistoryLimit: 保留失败 Pod 数: 无啥用了

    • successfulJobsHistoryLimit: 成功 Pod 数: 无啥用了

      启用 健康检查:也无效,因为代理网关是不会退出的.当任务执行完成后 状态还是会被标记为 NotReady

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      ... ...

      command:
      - producer
      - mth_8
      livenessProbe:
      failureThreshold: 100
      httpGet:
      path: /metrics
      port: 9900
      scheme: HTTP
      initialDelaySeconds: 5
      periodSeconds: 2
      successThreshold: 1
      timeoutSeconds: 2
      readinessProbe:
      failureThreshold: 100
      httpGet:
      path: /metrics
      port: 9900
      scheme: HTTP
      initialDelaySeconds: 5
      periodSeconds: 2
      successThreshold: 2
      timeoutSeconds: 2

  5. 解决思路 通过最大存活时间 activeDeadlineSeconds, 不管 Pod 执行状态.到时间销毁.配置后生效:done

  6. 后期通过 配置 yaml 指定不启用代理网关,来保证了 PodPodCompleted 状态的变更,也让上面的配置生效了

总结:

  • 文档对照着来,查找相关配置;
  • Pod 健康状态检查添加:
  • 网关代理 导致的 Terminated/Running = > Unavailable ; Pod NotReady
  • Pod 的最大存活时间:

最终 yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: saas-report-cronjob-day8
spec:
schedule: "*/1 * * * *"
successfulJobsHistoryLimit: 1
failedJobsHistoryLimit: 1
jobTemplate:
spec:
ttlSecondsAfterFinished: 10
activeDeadlineSeconds: 172800
backoffLimit: 0
template:
metadata:
annotations:
sidecar.istio.io/inject: "false"
spec:
restartPolicy: Never
containers:
- name: saas-report-cronjon-day8
image: report:dev
imagePullPolicy: Always
command:
- producer
- day_8
livenessProbe:
failureThreshold: 100
httpGet:
path: /metrics
port: 9900
scheme: HTTP
initialDelaySeconds: 5
periodSeconds: 2
successThreshold: 1
timeoutSeconds: 2
readinessProbe:
failureThreshold: 100
httpGet:
path: /metrics
port: 9900
scheme: HTTP
initialDelaySeconds: 5
periodSeconds: 2
successThreshold: 2
timeoutSeconds: 2