2
2
title : 已完成 Job 的自动清理
3
3
content_type : concept
4
4
weight : 70
5
+ description : >-
6
+ 一种用于清理已完成执行的旧 Job 的 TTL 机制。
5
7
---
6
8
<!--
7
- title: Automatic Clean-up for Finished Jobs
9
+ reviewers:
10
+ - janetkuo
11
+ title: Automatic Cleanup for Finished Jobs
8
12
content_type: concept
9
13
weight: 70
14
+ description: >-
15
+ A time-to-live mechanism to clean up old Jobs that have finished execution.
10
16
-->
11
17
12
18
<!-- overview -->
13
19
14
20
{{< feature-state for_k8s_version="v1.23" state="stable" >}}
15
21
16
22
<!--
17
- TTL-after-finished {{<glossary_tooltip text="controller" term_id="controller">}} provides a
18
- TTL (time to live) mechanism to limit the lifetime of resource objects that
19
- have finished execution. TTL controller only handles
20
- {{< glossary_tooltip text="Jobs" term_id="job" >}}.
23
+ When your Job has finished, it's useful to keep that Job in the API (and not immediately delete the Job)
24
+ so that you can tell whether the Job succeeded or failed.
25
+
26
+ Kubernetes' TTL-after-finished {{<glossary_tooltip text="controller" term_id="controller">}} provides a
27
+ TTL (time to live) mechanism to limit the lifetime of Job objects that
28
+ have finished execution.
21
29
-->
22
- TTL-after-finished {{<glossary_tooltip text="控制器" term_id="controller">}} 提供了一种 TTL 机制来限制已完成执行的资源对象的生命周期。
23
- TTL 控制器目前只处理 {{< glossary_tooltip text=" Job" term_id="job" >}} 。
30
+ 当你的 Job 已结束时,将 Job 保留在 API 中(而不是立即删除 Job)很有用,
31
+ 这样你就可以判断 Job 是成功还是失败 。
24
32
33
+ Kubernetes TTL-after-finished {{<glossary_tooltip text="控制器" term_id="controller">}}提供了一种
34
+ TTL 机制来限制已完成执行的 Job 对象的生命期。
25
35
26
36
<!-- body -->
27
37
28
38
<!--
29
- ## TTL-after- finished Controller
39
+ ## Cleanup for finished Jobs
30
40
31
- The TTL-after-finished controller is only supported for Jobs. A cluster operator can use this feature to clean
41
+ The TTL-after-finished controller is only supported for Jobs. You can use this mechanism to clean
32
42
up finished Jobs (either `Complete` or `Failed`) automatically by specifying the
33
43
`.spec.ttlSecondsAfterFinished` field of a Job, as in this
34
44
[example](/docs/concepts/workloads/controllers/job/#clean-up-finished-jobs-automatically).
35
45
-->
36
- ## TTL-after -finished 控制器
46
+ ## 清理已完成的 Job {#cleanup-for -finished-jobs}
37
47
38
- TTL-after-finished 控制器只支持 Job。集群操作员可以通过指定 Job 的 ` .spec.ttlSecondsAfterFinished `
39
- 字段来自动清理已结束的作业(` Complete ` 或 ` Failed ` ),如
40
- [ 示例] ( /zh-cn/docs/concepts/workloads/controllers/job/#clean-up-finished-jobs-automatically )
41
- 所示。
48
+ TTL-after-finished 控制器只支持 Job。你可以通过指定 Job 的 ` .spec.ttlSecondsAfterFinished `
49
+ 字段来自动清理已结束的 Job(` Complete ` 或 ` Failed ` ),
50
+ 如[ 示例] ( /zh-cn/docs/concepts/workloads/controllers/job/#clean-up-finished-jobs-automatically ) 所示。
42
51
43
52
<!--
44
- The TTL-after-finished controller will assume that a job is eligible to be cleaned up
45
- TTL seconds after the job has finished, in other words, when the TTL has expired. When the
53
+ The TTL-after-finished controller assumes that a Job is eligible to be cleaned up
54
+ TTL seconds after the Job has finished. The timer starts once the
55
+ status condition of the Job changes to show that the Job is either `Complete` or `Failed`; once the TTL has
56
+ expired, that Job becomes eligible for
57
+ [cascading](/docs/concepts/architecture/garbage-collection/#cascading-deletion) removal. When the
46
58
TTL-after-finished controller cleans up a job, it will delete it cascadingly, that is to say it will delete
47
- its dependent objects together with it. Note that when the job is deleted,
48
- its lifecycle guarantees, such as finalizers, will be honored.
59
+ its dependent objects together with it.
49
60
-->
50
- TTL-after-finished 控制器假设作业能在执行完成后的 TTL 秒内被清理,也就是当 TTL 过期后。
51
- 当 TTL 控制器清理作业时,它将做级联删除操作,即删除资源对象的同时也删除其依赖对象。
52
- 注意,当资源被删除时,由该资源的生命周期保证其终结器(Finalizers)等被执行。
61
+ TTL-after-finished 控制器假设 Job 能在执行完成后的 TTL 秒内被清理。一旦 Job
62
+ 的状态条件发生变化表明该 Job 是 ` Complete ` 或 ` Failed ` ,计时器就会启动;一旦 TTL 已过期,该 Job
63
+ 就能被[ 级联删除] ( /zh-cn/docs/concepts/architecture/garbage-collection/#cascading-deletion ) 。
64
+ 当 TTL 控制器清理作业时,它将做级联删除操作,即删除 Job 的同时也删除其依赖对象。
53
65
54
66
<!--
55
- The TTL seconds can be set at any time. Here are some examples for setting the
67
+ Kubernetes honors object lifecycle guarantees on the Job, such as waiting for
68
+ [finalizers](/docs/concepts/overview/working-with-objects/finalizers/).
69
+
70
+ You can set the TTL seconds at any time. Here are some examples for setting the
56
71
`.spec.ttlSecondsAfterFinished` field of a Job:
57
72
-->
58
- 可以随时设置 TTL 秒。以下是设置 Job 的 ` .spec.ttlSecondsAfterFinished ` 字段的一些示例:
73
+ Kubernetes 尊重 Job 对象的生命周期保证,例如等待
74
+ [ Finalizer] ( /zh-cn/docs/concepts/overview/working-with-objects/finalizers/ ) 。
75
+
76
+ 你可以随时设置 TTL 秒。以下是设置 Job 的 ` .spec.ttlSecondsAfterFinished ` 字段的一些示例:
59
77
60
78
<!--
61
- * Specify this field in the job manifest, so that a Job can be cleaned up
79
+ * Specify this field in the Job manifest, so that a Job can be cleaned up
62
80
automatically some time after it finishes.
63
- * Set this field of existing, already finished jobs, to adopt this new feature.
81
+ * Manually set this field of existing, already finished Jobs, so that they become eligible
82
+ for cleanup.
64
83
* Use a
65
84
[mutating admission webhook](/docs/reference/access-authn-authz/extensible-admission-controllers/#admission-webhooks)
66
- to set this field dynamically at job creation time. Cluster administrators can
85
+ to set this field dynamically at Job creation time. Cluster administrators can
67
86
use this to enforce a TTL policy for finished jobs.
87
+ -->
88
+ * 在 Job 清单(manifest)中指定此字段,以便 Job 在完成后的某个时间被自动清理。
89
+ * 手动设置现有的、已完成的 Job 的此字段,以便这些 Job 可被清理。
90
+ * 在创建 Job 时使用[ 修改性质的准入 Webhook] ( /zh-cn/docs/reference/access-authn-authz/extensible-admission-controllers/#admission-webhooks )
91
+ 动态设置该字段。集群管理员可以使用它对已完成的作业强制执行 TTL 策略。
92
+ <!--
68
93
* Use a
69
94
[mutating admission webhook](/docs/reference/access-authn-authz/extensible-admission-controllers/#admission-webhooks)
70
- to set this field dynamically after the job has finished, and choose
71
- different TTL values based on job status, labels, etc.
95
+ to set this field dynamically after the Job has finished, and choose
96
+ different TTL values based on job status, labels. For this case, the webhook needs
97
+ to detect changes to the `.status` of the Job and only set a TTL when the Job
98
+ is being marked as completed.
99
+ * Write your own controller to manage the cleanup TTL for Jobs that match a particular
100
+ {{< glossary_tooltip term_id="selector" text="selector-selector" >}}.
72
101
-->
73
- * 在作业清单(manifest)中指定此字段,以便 Job 在完成后的某个时间被自动清除。
74
- * 将此字段设置为现有的、已完成的作业,以采用此新功能。
75
- * 在创建作业时使用 [ mutating admission webhook] ( /zh-cn/docs/reference/access-authn-authz/extensible-admission-controllers/#admission-webhooks )
76
- 动态设置该字段。集群管理员可以使用它对完成的作业强制执行 TTL 策略。
77
- * 使用 [ mutating admission webhook] ( /zh-cn/docs/reference/access-authn-authz/extensible-admission-controllers/#admission-webhooks )
78
- 在作业完成后动态设置该字段,并根据作业状态、标签等选择不同的 TTL 值。
102
+ * 使用[ 修改性质的准入 Webhook] ( /zh-cn/docs/reference/access-authn-authz/extensible-admission-controllers/#admission-webhooks )
103
+ 在 Job 完成后动态设置该字段,并根据 Job 状态、标签等选择不同的 TTL 值。
104
+ 对于这种情况,Webhook 需要检测 Job 的 ` .status ` 变化,并且仅在 Job 被标记为已完成时设置 TTL。
105
+ * 编写你自己的控制器来管理与特定{{< glossary_tooltip term_id="selector" text="选择算符" >}}匹配的
106
+ Job 的清理 TTL。
79
107
80
108
<!--
81
- ## Caveat
109
+ ## Caveats
82
110
83
- ### Updating TTL Seconds
111
+ ### Updating TTL for finished Jobs
84
112
85
- Note that the TTL period, e.g. `.spec.ttlSecondsAfterFinished` field of Jobs,
86
- can be modified after the job is created or has finished. However, once the
87
- Job becomes eligible to be deleted (when the TTL has expired), the system won't
88
- guarantee that the Jobs will be kept , even if an update to extend the TTL
89
- returns a successful API response.
113
+ You can modify the TTL period, e.g. `.spec.ttlSecondsAfterFinished` field of Jobs,
114
+ after the job is created or has finished. If you extend the TTL period after the
115
+ existing `ttlSecondsAfterFinished` period has expired, Kubernetes doesn't guarantee
116
+ to retain that Job , even if an update to extend the TTL returns a successful API
117
+ response.
90
118
-->
91
- ## 警告
119
+ ## 警告 {#caveats}
92
120
93
- ### 更新 TTL 秒数
121
+ ### 更新已完成 Job 的 TTL {#updating-ttl-for-finished-jobs}
94
122
95
- 请注意, 在创建 Job 或已经执行结束后,仍可以修改其 TTL 周期,例如 Job 的
123
+ 在创建 Job 或已经执行结束后,你仍可以修改其 TTL 周期,例如 Job 的
96
124
` .spec.ttlSecondsAfterFinished ` 字段。
97
- 但是一旦 Job 变为可被删除状态(当其 TTL 已过期时),即使你通过 API 增加其 TTL
98
- 时长得到了成功的响应,系统也不保证 Job 将被保留。
125
+ 如果你在当前 ` ttlSecondsAfterFinished ` 时长已过期后延长 TTL 周期,
126
+ 即使延长 TTL 的更新得到了成功的 API 响应,Kubernetes 也不保证保留此 Job,
99
127
100
128
<!--
101
- ### Time Skew
129
+ ### Time skew
102
130
103
- Because TTL-after-finished controller uses timestamps stored in the Kubernetes resources to
131
+ Because the TTL-after-finished controller uses timestamps stored in the Kubernetes jobs to
104
132
determine whether the TTL has expired or not, this feature is sensitive to time
105
- skew in the cluster, which may cause TTL-after-finished controller to clean up resource objects
133
+ skew in your cluster, which may cause the control plane to clean up Job objects
106
134
at the wrong time.
107
135
-->
108
136
### 时间偏差 {#time-skew}
109
137
110
- 由于 TTL-after-finished 控制器使用存储在 Kubernetes 资源中的时间戳来确定 TTL 是否已过期,
111
- 因此该功能对集群中的时间偏差很敏感,这可能导致 TTL-after-finished 控制器在错误的时间清理资源对象 。
138
+ 由于 TTL-after-finished 控制器使用存储在 Kubernetes Job 中的时间戳来确定 TTL 是否已过期,
139
+ 因此该功能对集群中的时间偏差很敏感,这可能导致控制平面在错误的时间清理 Job 对象 。
112
140
113
141
<!--
114
142
Clocks aren't always correct, but the difference should be
@@ -120,9 +148,13 @@ very small. Please be aware of this risk when setting a non-zero TTL.
120
148
## {{% heading "whatsnext" %}}
121
149
122
150
<!--
123
- * [Clean up Jobs automatically](/docs/concepts/workloads/controllers/jobs-run-to-completion/#clean-up-finished-jobs-automatically)
124
- * [Design doc](https://github.com/kubernetes/enhancements/blob/master/keps/sig-apps/592-ttl-after-finish/README.md)
151
+ * Read [Clean up Jobs automatically](/docs/concepts/workloads/controllers/job/#clean-up-finished-jobs-automatically)
152
+
153
+ * Refer to the [Kubernetes Enhancement Proposal](https://github.com/kubernetes/enhancements/blob/master/keps/sig-apps/592-ttl-after-finish/README.md)
154
+ (KEP) for adding this mechanism.
125
155
-->
126
- * [ 自动清理 Job] ( /zh-cn/docs/concepts/workloads/controllers/job/#clean-up-finished-jobs-automatically )
127
- * [ 设计文档] ( https://github.com/kubernetes/enhancements/blob/master/keps/sig-apps/592-ttl-after-finish/README.md )
156
+ * 阅读[ 自动清理 Job] ( /zh-cn/docs/concepts/workloads/controllers/job/#clean-up-finished-jobs-automatically )
157
+
158
+ * 参阅 [ Kubernetes 增强提案] ( https://github.com/kubernetes/enhancements/blob/master/keps/sig-apps/592-ttl-after-finish/README.md )
159
+ (KEP) 了解此机制的演进过程。
128
160
0 commit comments