k8s v1.16设置Job ttlSecondsAfterFinished不生效

Completed的job默认不会清理
Job在处于Completed后,默认是不会被清理的。

https://v1-16.docs.kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/#job-termination-and-cleanup

When a Job completes, no more Pods are created, but the Pods are not deleted either. Keeping them around allows you to still view the logs of completed pods to check for errors, warnings, or other diagnostic output.

配置自动清理job
https://v1-16.docs.kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/#clean-up-finished-jobs-automatically

ttl机制自动清理完成的job
https://v1-16.docs.kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/#ttl-mechanism-for-finished-jobs

就是给Job的spec增加一个`ttlSecondsAfterFinished`字段,这个字段的意思是当job完成后的保留xx秒就自动清理这个job。

当ttl controller清理job的时候是级联删除的,会把这个job下的pod一并删除。

官网的这个例子是当job完成后的100s就会自动清理了。

apiVersion: batch/v1
kind: Job
metadata:
  name: pi-with-ttl
spec:
  ttlSecondsAfterFinished: 100
  template:
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never

如果ttlSecondsAfterFinished设置为0,当job完成后则会立即删除。如果没有设置这个字段,那么当job完成后ttl controller是不会自动清理这个job的。

ttl controller
https://v1-16.docs.kubernetes.io/docs/concepts/workloads/controllers/ttlafterfinished/

由于目前还是alpha版本,需要给kube-controller-manager 和 kube-apiserver 开启TTLAfterFinished 才能生效。

Alpha Disclaimer: this feature is currently alpha, and can be enabled with both kube-apiserver and kube-controller-manager feature gate TTLAfterFinished.
开启 TTLAfterFinished
https://v1-16.docs.kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/#feature-gates-for-alpha-or-beta-features

默认是关闭的。

 

找到安装kubernetes的kube-apiserver.yaml和kube-controller-manager.yaml,增加开启TTLAfterFinished的设置。

多个feature-gates之间使用逗号分割。

kube-apiserver开启TTLAfterFinished

kube-controller-manager开启TTLAfterFinished

重启kubelet
systemctl restart kubelet
修改成功后重启kubelet即可生效了。

 

这个job完成时间是2020 08:47:12,我给job配置的ttlSecondsAfterFinished是120s,120s后这个job就被自动清理了。完成!

 

posted @ 2022-05-09 13:22  linhaifeng  阅读(781)  评论(0编辑  收藏  举报