如何自动删除由 CronJob 创建的已完成的 Kubernetes 作业? [英] How to automatically remove completed Kubernetes Jobs created by a CronJob?

查看:34
本文介绍了如何自动删除由 CronJob 创建的已完成的 Kubernetes 作业?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了创建一个 CronJob 来清理已完成的 Jobs 之外,还有没有办法自动删除已完成的 Jobs?

Is there a way to automatically remove completed Jobs besides making a CronJob to clean up completed Jobs?

K8s 作业文档指出已完成作业的预期行为是为他们准备的保持完成状态直到手动删除.因为我每天通过 CronJobs 运行数千个作业,我不想保留已完成的作业.

The K8s Job Documentation states that the intended behavior of completed Jobs is for them to remain in a completed state until manually deleted. Because I am running thousands of Jobs a day via CronJobs and I don't want to keep completed Jobs around.

推荐答案

您现在可以设置历史限制,或完全禁用历史,这样失败或成功的 CronJobs 不会无限期地保留.在此处查看我的回答.文档位于此处.

You can now set history limits, or disable history altogether, so that failed or successful CronJobs are not kept around indefinitely. See my answer here. Documentation is here.

设置历史限制:

.spec.successfulJobsHistoryLimit.spec.failedJobsHistoryLimit 字段是可选的.这些字段指定应保留多少已完成和失败的作业.默认情况下,它们分别设置为 3 和 1.将限制设置为 0 对应于在完成后不保留任何相应类型的作业.

The .spec.successfulJobsHistoryLimit and .spec.failedJobsHistoryLimit fields are optional. These fields specify how many completed and failed jobs should be kept. By default, they are set to 3 and 1 respectively. Setting a limit to 0 corresponds to keeping none of the corresponding kind of jobs after they finish.

0 限制的配置看起来像:

The config with 0 limits would look like:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  successfulJobsHistoryLimit: 0
  failedJobsHistoryLimit: 0
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

这篇关于如何自动删除由 CronJob 创建的已完成的 Kubernetes 作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆