|
| 1 | +--- |
| 2 | +title: 垃圾收集 |
| 3 | +content_type: concept |
| 4 | +weight: 50 |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- |
| 8 | +title: Garbage Collection |
| 9 | +content_type: concept |
| 10 | +weight: 50 |
| 11 | +--> |
| 12 | + |
| 13 | +<!-- overview --> |
| 14 | + |
| 15 | +<!-- |
| 16 | +{{<glossary_definition term_id="garbage-collection" length="short">}} This |
| 17 | +allows the clean up of resources like the following: |
| 18 | +--> |
| 19 | +{{<glossary_definition term_id="garbage-collection" length="short">}} |
| 20 | +垃圾收集允许系统清理如下资源: |
| 21 | + |
| 22 | +<!-- |
| 23 | +* [Failed pods](/docs/concepts/workloads/pods/pod-lifecycle/#pod-garbage-collection) |
| 24 | +* [Completed Jobs](/docs/concepts/workloads/controllers/ttlafterfinished/) |
| 25 | +* [Objects without owner references](#owners-dependents) |
| 26 | +* [Unused containers and container images](#containers-images) |
| 27 | +* [Dynamically provisioned PersistentVolumes with a StorageClass reclaim policy of Delete](/docs/concepts/storage/persistent-volumes/#delete) |
| 28 | +* [Stale or expired CertificateSigningRequests (CSRs)](/reference/access-authn-authz/certificate-signing-requests/#request-signing-process) |
| 29 | +* {{<glossary_tooltip text="Nodes" term_id="node">}} deleted in the following scenarios: |
| 30 | + * On a cloud when the cluster uses a [cloud controller manager](/docs/concepts/architecture/cloud-controller/) |
| 31 | + * On-premises when the cluster uses an addon similar to a cloud controller |
| 32 | + manager |
| 33 | +* [Node Lease objects](/docs/concepts/architecture/nodes/#heartbeats) |
| 34 | +--> |
| 35 | +* [失败的 Pod](/zh/docs/concepts/workloads/pods/pod-lifecycle/#pod-garbage-collection) |
| 36 | +* [已完成的 Job](/zh/docs/concepts/workloads/controllers/ttlafterfinished/) |
| 37 | +* [不再存在属主引用的对象](#owners-dependents) |
| 38 | +* [未使用的容器和容器镜像](#containers-images) |
| 39 | +* [动态制备的、StorageClass 回收策略为 Delete 的 PV 卷](/zh/docs/concepts/storage/persistent-volumes/#delete) |
| 40 | +* [阻滞或者过期的 CertificateSigningRequest (CSRs)](/zh/docs/reference/access-authn-authz/certificate-signing-requests/#request-signing-process) |
| 41 | +* 在以下情形中删除了的{{<glossary_tooltip text="节点" term_id="node">}}对象: |
| 42 | + * 当集群使用[云控制器管理器](/zh/docs/concepts/architecture/cloud-controller/)运行于云端时; |
| 43 | + * 当集群使用类似于云控制器管理器的插件运行在本地环境中时。 |
| 44 | +* [节点租约对象](/zh/docs/concepts/architecture/nodes/#heartbeats) |
| 45 | + |
| 46 | +<!-- |
| 47 | +## Owners and dependents {#owners-dependents} |
| 48 | +
|
| 49 | +Many objects in Kubernetes link to each other through [*owner references*](/docs/concepts/overview/working-with-objects/owners-dependents/). |
| 50 | +Owner references tell the control plane which objects are dependent on others. |
| 51 | +Kubernetes uses owner references to give the control plane, and other API |
| 52 | +clients, the opportunity to clean up related resources before deleting an |
| 53 | +object. In most cases, Kubernetes manages owner references automatically. |
| 54 | +--> |
| 55 | +## 属主与依赖 {#owners-dependents} |
| 56 | + |
| 57 | +Kubernetes 中很多对象通过[*属主引用*](/zh/docs/concepts/overview/working-with-objects/owners-dependents/) |
| 58 | +链接到彼此。属主引用(Owner Reference)可以告诉控制面哪些对象依赖于其他对象。 |
| 59 | +Kubernetes 使用属主引用来为控制面以及其他 API 客户端在删除某对象时提供一个 |
| 60 | +清理关联资源的机会。在大多数场合,Kubernetes 都是自动管理属主引用的。 |
| 61 | + |
| 62 | +<!-- |
| 63 | +Ownership is different from the [labels and selectors](/docs/concepts/overview/working-with-objects/labels/) |
| 64 | +mechanism that some resources also use. For example, consider a |
| 65 | +{{<glossary_tooltip text="Service" term_id="service">}} that creates |
| 66 | +`EndpointSlice` objects. The Service uses *labels* to allow the control plane to |
| 67 | +determine which `EndpointSlice` objects are used for that Service. In addition |
| 68 | +to the labels, each `EndpointSlice` that is managed on behalf of a Service has |
| 69 | +an owner reference. Owner references help different parts of Kubernetes avoid |
| 70 | +interfering with objects they don’t control. |
| 71 | +--> |
| 72 | +属主关系与某些资源所使用的的[标签和选择算符](/zh/docs/concepts/overview/working-with-objects/labels/) |
| 73 | +不同。例如,考虑一个创建 `EndpointSlice` 对象的 {{<glossary_tooltip text="Service" term_id="service">}} |
| 74 | +对象。Service 对象使用*标签*来允许控制面确定哪些 `EndpointSlice` 对象被该 |
| 75 | +Service 使用。除了标签,每个被 Service 托管的 `EndpointSlice` 对象还有一个属主引用属性。 |
| 76 | +属主引用可以帮助 Kubernetes 中的不同组件避免干预并非由它们控制的对象。 |
| 77 | + |
| 78 | +{{< note >}} |
| 79 | +<!-- |
| 80 | +Cross-namespace owner references are disallowed by design. |
| 81 | +Namespaced dependents can specify cluster-scoped or namespaced owners. |
| 82 | +A namespaced owner **must** exist in the same namespace as the dependent. |
| 83 | +If it does not, the owner reference is treated as absent, and the dependent |
| 84 | +is subject to deletion once all owners are verified absent. |
| 85 | +--> |
| 86 | +根据设计,系统不允许出现跨名字空间的属主引用。名字空间作用域的依赖对象可以指定集群作用域或者名字空间作用域的属主。 |
| 87 | +名字空间作用域的属主**必须**存在于依赖对象所在的同一名字空间。 |
| 88 | +如果属主位于不同名字空间,则属主引用被视为不存在,而当检查发现所有属主都已不存在时, |
| 89 | +依赖对象会被删除。 |
| 90 | + |
| 91 | +<!-- |
| 92 | +Cluster-scoped dependents can only specify cluster-scoped owners. |
| 93 | +In v1.20+, if a cluster-scoped dependent specifies a namespaced kind as an owner, |
| 94 | +it is treated as having an unresolvable owner reference, and is not able to be garbage collected. |
| 95 | +--> |
| 96 | +集群作用域的依赖对象只能指定集群作用域的属主。 |
| 97 | +在 1.20 及更高版本中,如果一个集群作用域的依赖对象指定了某个名字空间作用域的类别作为其属主, |
| 98 | +则该对象被视为拥有一个无法解析的属主引用,因而无法被垃圾收集处理。 |
| 99 | + |
| 100 | +<!-- |
| 101 | +In v1.20+, if the garbage collector detects an invalid cross-namespace `ownerReference`, |
| 102 | +or a cluster-scoped dependent with an `ownerReference` referencing a namespaced kind, a warning Event |
| 103 | +with a reason of `OwnerRefInvalidNamespace` and an `involvedObject` of the invalid dependent is reported. |
| 104 | +You can check for that kind of Event by running |
| 105 | +`kubectl get events -A --field-selector=reason=OwnerRefInvalidNamespace`. |
| 106 | +--> |
| 107 | +在 1.20 及更高版本中,如果垃圾收集器检测到非法的跨名字空间 `ownerReference`, |
| 108 | +或者某集群作用域的依赖对象的 `ownerReference` 引用某名字空间作用域的类别, |
| 109 | +系统会生成一个警告事件,其原因为 `OwnerRefInvalidNamespace`,`involvedObject` |
| 110 | +设置为非法的依赖对象。你可以通过运行 |
| 111 | +`kubectl get events -A --field-selector=reason=OwnerRefInvalidNamespace` |
| 112 | +来检查是否存在这类事件。 |
| 113 | +{{< /note >}} |
| 114 | + |
| 115 | +<!-- |
| 116 | +## Cascading deletion {#cascading-deletion} |
| 117 | +
|
| 118 | +Kubernetes checks for and deletes objects that no longer have owner |
| 119 | +references, like the pods left behind when you delete a ReplicaSet. When you |
| 120 | +delete an object, you can control whether Kubernetes deletes the object's |
| 121 | +dependents automatically, in a process called *cascading deletion*. There are |
| 122 | +two types of cascading deletion, as follows: |
| 123 | +
|
| 124 | +* Foreground cascading deletion |
| 125 | +* Background cascading deletion |
| 126 | +--> |
| 127 | +## 级联删除 {#cascading-deletion} |
| 128 | + |
| 129 | +Kubernetes 会检查并删除那些不再拥有属主引用的对象,例如在你删除了 ReplicaSet |
| 130 | +之后留下来的 Pod。当你删除某个对象时,你可以控制 Kubernetes 是否要通过一个称作 |
| 131 | +级联删除(Cascading Deletion)的过程自动删除该对象的依赖对象。 |
| 132 | +级联删除有两种类型,分别如下: |
| 133 | + |
| 134 | +* 前台级联删除 |
| 135 | +* 后台级联删除 |
| 136 | + |
| 137 | +<!-- |
| 138 | +You can also control how and when garbage collection deletes resources that have |
| 139 | +owner references using Kubernetes {{<glossary_tooltip text="finalizers" term_id="finalizer">}}. |
| 140 | +--> |
| 141 | +你也可以使用 Kubernetes {{<glossary_tooltip text="Finalizers" term_id="finalizer">}} |
| 142 | +来控制垃圾收集机制如何以及何时删除包含属主引用的资源。 |
| 143 | + |
| 144 | +<!-- |
| 145 | +### Foreground cascading deletion {#foreground-deletion} |
| 146 | +
|
| 147 | +In foreground cascading deletion, the owner object you're deleting first enters |
| 148 | +a *deletion in progress* state. In this state, the following happens to the |
| 149 | +owner object: |
| 150 | +--> |
| 151 | +### 前台级联删除 {#foreground-deletion} |
| 152 | + |
| 153 | +在前台级联删除中,正在被你删除的对象首先进入 *deletion in progress* 状态。 |
| 154 | +在这种状态下,针对属主对象会发生以下事情: |
| 155 | + |
| 156 | +<!-- |
| 157 | +* The Kubernetes API server sets the object's `metadata.deletionTimestamp` |
| 158 | + field to the time the object was marked for deletion. |
| 159 | +* The Kubernetes API server also sets the `metadata.finalizers` field to |
| 160 | + `foregroundDeletion`. |
| 161 | +* The object remains visible through the Kubernetes API until the deletion |
| 162 | + process is complete. |
| 163 | +--> |
| 164 | +* Kubernetes API 服务器将对象的 `metadata.deletionTimestamp` |
| 165 | + 字段设置为对象被标记为要删除的时间点。 |
| 166 | +* Kubernetes API 服务器也会将 `metadata.finalizers` 字段设置为 `foregroundDeletion`。 |
| 167 | +* 在删除过程完成之前,通过 Kubernetes API 仍然可以看到该对象。 |
| 168 | + |
| 169 | +<!-- |
| 170 | +After the owner object enters the deletion in progress state, the controller |
| 171 | +deletes the dependents. After deleting all the dependent objects, the controller |
| 172 | +deletes the owner object. At this point, the object is no longer visible in the |
| 173 | +Kubernetes API. |
| 174 | +
|
| 175 | +During foreground cascading deletion, the only dependents that block owner |
| 176 | +deletion are those that have the `ownerReference.blockOwnerDeletion=true` field. |
| 177 | +See [Use foreground cascading deletion](/docs/tasks/administer-cluster/use-cascading-deletion/#use-foreground-cascading-deletion) |
| 178 | +to learn more. |
| 179 | +--> |
| 180 | +当属主对象进入删除过程中状态后,控制器删除其依赖对象。控制器在删除完所有依赖对象之后, |
| 181 | +删除属主对象。这时,通过 Kubernetes API 就无法再看到该对象。 |
| 182 | + |
| 183 | +在前台级联删除过程中,唯一的可能阻止属主对象被删除的依赖对象是那些带有 |
| 184 | +`ownerReference.blockOwnerDeletion=true` 字段的对象。 |
| 185 | +参阅[使用前台级联删除](/zh/docs/tasks/administer-cluster/use-cascading-deletion/#use-foreground-cascading-deletion) |
| 186 | +以了解进一步的细节。 |
| 187 | + |
| 188 | +<!-- |
| 189 | +### Background cascading deletion {#background-deletion} |
| 190 | +
|
| 191 | +In background cascading deletion, the Kubernetes API server deletes the owner |
| 192 | +object immediately and the controller cleans up the dependent objects in |
| 193 | +the background. By default, Kubernetes uses background cascading deletion unless |
| 194 | +you manually use foreground deletion or choose to orphan the dependent objects. |
| 195 | +
|
| 196 | +See [Use background cascading deletion](/docs/tasks/administer-cluster/use-cascading-deletion/#use-background-cascading-deletion) |
| 197 | +to learn more. |
| 198 | +--> |
| 199 | +### 后台级联删除 {#background-deletion} |
| 200 | + |
| 201 | +在后台级联删除过程中,Kubernetes 服务器立即删除属主对象,控制器在后台清理所有依赖对象。 |
| 202 | +默认情况下,Kubernetes 使用后台级联删除方案,除非你手动设置了要使用前台删除, |
| 203 | +或者选择遗弃依赖对象。 |
| 204 | + |
| 205 | +参阅[使用后台级联删除](/zh/docs/tasks/administer-cluster/use-cascading-deletion/#use-background-cascading-deletion) |
| 206 | +以了解进一步的细节。 |
| 207 | + |
| 208 | +<!-- |
| 209 | +### Orphaned dependents |
| 210 | +
|
| 211 | +When Kubernetes deletes an owner object, the dependents left behind are called |
| 212 | +*orphan* objects. By default, Kubernetes deletes dependent objects. To learn how |
| 213 | +to override this behaviour, see [Delete owner objects and orphan dependents](/docs/tasks/administer-cluster/use-cascading-deletion/#set-orphan-deletion-policy). |
| 214 | +--> |
| 215 | +### 被遗弃的依赖对象 {#orphaned-dependents} |
| 216 | + |
| 217 | +当 Kubernetes 删除某个属主对象时,被留下来的依赖对象被称作被遗弃的(Orphaned)对象。 |
| 218 | +默认情况下,Kubernetes 会删除依赖对象。要了解如何重载这种默认行为,可参阅 |
| 219 | +[删除属主对象和遗弃依赖对象](/zh/docs/tasks/administer-cluster/use-cascading-deletion/#set-orphan-deletion-policy)。 |
| 220 | + |
| 221 | +<!-- |
| 222 | +## Garbage collection of unused containers and images {#containers-images} |
| 223 | +
|
| 224 | +The {{<glossary_tooltip text="kubelet" term_id="kubelet">}} performs garbage |
| 225 | +collection on unused images every five minutes and on unused containers every |
| 226 | +minute. You should avoid using external garbage collection tools, as these can |
| 227 | +break the kubelet behavior and remove containers that should exist. |
| 228 | +--> |
| 229 | +## 未使用容器和镜像的垃圾收集 {#containers-images} |
| 230 | + |
| 231 | +{{<glossary_tooltip text="kubelet" term_id="kubelet">}} 会每五分钟对未使用的镜像执行一次垃圾收集, |
| 232 | +每分钟对未使用的容器执行一次垃圾收集。 |
| 233 | +你应该避免使用外部的垃圾收集工具,因为外部工具可能会破坏 kubelet |
| 234 | +的行为,移除应该保留的容器。 |
| 235 | + |
| 236 | +<!-- |
| 237 | +To configure options for unused container and image garbage collection, tune the |
| 238 | +kubelet using a [configuration file](/docs/tasks/administer-cluster/kubelet-config-file/) |
| 239 | +and change the parameters related to garbage collection using the |
| 240 | +[`KubeletConfiguration`](/docs/reference/config-api/kubelet-config.v1beta1/#kubelet-config-k8s-io-v1beta1-KubeletConfiguration) |
| 241 | +resource type. |
| 242 | +--> |
| 243 | +要配置对未使用容器和镜像的垃圾收集选项,可以使用一个 |
| 244 | +[配置文件](/zh/docs/tasks/administer-cluster/kubelet-config-file/),基于 |
| 245 | +[`KubeletConfiguration`](/zh/docs/reference/config-api/kubelet-config.v1beta1/#kubelet-config-k8s-io-v1beta1-KubeletConfiguration) |
| 246 | +资源类型来调整与垃圾搜集相关的 kubelet 行为。 |
| 247 | + |
| 248 | +<!-- |
| 249 | +### Container image lifecycle |
| 250 | +
|
| 251 | +Kubernetes manages the lifecycle of all images through its *image manager*, |
| 252 | +which is part of the kubelet, with the cooperation of |
| 253 | +{{< glossary_tooltip text="cadvisor" term_id="cadvisor" >}}. The kubelet |
| 254 | +considers the following disk usage limits when making garbage collection |
| 255 | +decisions: |
| 256 | +--> |
| 257 | +### 容器镜像生命期 {#container-image-lifecycle} |
| 258 | + |
| 259 | +Kubernetes 通过其镜像管理器(Image Manager)来管理所有镜像的生命周期, |
| 260 | +该管理器是 kubelet 的一部分,工作时与 |
| 261 | +{{< glossary_tooltip text="cadvisor" term_id="cadvisor" >}} 协同。 |
| 262 | +kubelet 在作出垃圾收集决定时会考虑如下磁盘用量约束: |
| 263 | + |
| 264 | +* `HighThresholdPercent` |
| 265 | +* `LowThresholdPercent` |
| 266 | + |
| 267 | +<!-- |
| 268 | +Disk usage above the configured `HighThresholdPercent` value triggers garbage |
| 269 | +collection, which deletes images in order based on the last time they were used, |
| 270 | +starting with the oldest first. The kubelet deletes images |
| 271 | +until disk usage reaches the `LowThresholdPercent` value. |
| 272 | +--> |
| 273 | +磁盘用量超出所配置的 `HighThresholdPercent` 值时会触发垃圾收集, |
| 274 | +垃圾收集器会基于镜像上次被使用的时间来按顺序删除它们,首先删除的是最老的镜像。 |
| 275 | +kubelet 会持续删除镜像,直到磁盘用量到达 `LowThresholdPercent` 值为止。 |
| 276 | + |
| 277 | +<!-- |
| 278 | +### Container garbage collection {#container-image-garbage-collection} |
| 279 | +
|
| 280 | +The kubelet garbage collects unused containers based on the following variables, |
| 281 | +which you can define: |
| 282 | +--> |
| 283 | +### 容器垃圾收集 {#container-image-garbage-collection} |
| 284 | + |
| 285 | +kubelet 会基于如下变量对所有未使用的容器执行垃圾收集操作,这些变量都是你可以定义的: |
| 286 | + |
| 287 | +<!-- |
| 288 | +* `MinAge`: the minimum age at which the kubelet can garbage collect a |
| 289 | + container. Disable by setting to `0`. |
| 290 | +* `MaxPerPodContainer`: the maximum number of dead containers each Pod pair |
| 291 | + can have. Disable by setting to less than `0`. |
| 292 | +* `MaxContainers`: the maximum number of dead containers the cluster can have. |
| 293 | + Disable by setting to less than `0`. |
| 294 | +--> |
| 295 | +* `MinAge`:kubelet 可以垃圾回收某个容器时该容器的最小年龄。设置为 `0` |
| 296 | + 表示禁止使用此规则。 |
| 297 | +* `MaxPerPodContainer`:每个 Pod 可以包含的已死亡的容器个数上限。设置为小于 `0` |
| 298 | + 的值表示禁止使用此规则。 |
| 299 | +* `MaxContainers`:集群中可以存在的已死亡的容器个数上限。设置为小于 `0` |
| 300 | + 的值意味着禁止应用此规则。 |
| 301 | + |
| 302 | +<!-- |
| 303 | +In addition to these variables, the kubelet garbage collects unidentified and |
| 304 | +deleted containers, typically starting with the oldest first. |
| 305 | +
|
| 306 | +`MaxPerPodContainer` and `MaxContainer` may potentially conflict with each other |
| 307 | +in situations where retaining the maximum number of containers per Pod |
| 308 | +(`MaxPerPodContainer`) would go outside the allowable total of global dead |
| 309 | +containers (`MaxContainers`). In this situation, the kubelet adjusts |
| 310 | +`MaxPerPodContainer` to address the conflict. A worst-case scenario would be to |
| 311 | +downgrade `MaxPerPodContainer` to `1` and evict the oldest containers. |
| 312 | +Additionally, containers owned by pods that have been deleted are removed once |
| 313 | +they are older than `MinAge`. |
| 314 | +--> |
| 315 | +除以上变量之外,kubelet 还会垃圾收集除无标识的以及已删除的容器,通常从最老的容器开始。 |
| 316 | + |
| 317 | +当保持每个 Pod 的最大数量的容器(`MaxPerPodContainer`)会使得全局的已死亡容器个数超出上限 |
| 318 | +(`MaxContainers`)时,`MaxPerPodContainer` 和 `MaxContainers` 之间可能会出现冲突。 |
| 319 | +在这种情况下,kubelet 会调整 `MaxPerPodContainer` 来解决这一冲突。 |
| 320 | +最坏的情形是将 `MaxPerPodContainer` 降格为 `1`,并驱逐最老的容器。 |
| 321 | +此外,当隶属于某已被删除的 Pod 的容器的年龄超过 `MinAge` 时,它们也会被删除。 |
| 322 | + |
| 323 | +{{<note>}} |
| 324 | +<!-- |
| 325 | +The kubelet only garbage collects the containers it manages. |
| 326 | +--> |
| 327 | +kubelet 仅会回收由它所管理的容器。 |
| 328 | +{{</note>}} |
| 329 | + |
| 330 | +<!-- |
| 331 | +## Configuring garbage collection {#configuring-gc} |
| 332 | +
|
| 333 | +You can tune garbage collection of resources by configuring options specific to |
| 334 | +the controllers managing those resources. The following pages show you how to |
| 335 | +configure garbage collection: |
| 336 | +
|
| 337 | + * [Configuring cascading deletion of Kubernetes objects](/docs/tasks/administer-cluster/use-cascading-deletion/) |
| 338 | + * [Configuring cleanup of finished Jobs](/docs/concepts/workloads/controllers/ttlafterfinished/) |
| 339 | +--> |
| 340 | +## 配置垃圾收集 {#configuring-gc} |
| 341 | + |
| 342 | +你可以通过配置特定于管理资源的控制器来调整资源的垃圾收集行为。 |
| 343 | +下面的页面为你展示如何配置垃圾收集: |
| 344 | + |
| 345 | +* [配置 Kubernetes 对象的级联删除](/zh/docs/tasks/administer-cluster/use-cascading-deletion/) |
| 346 | +* [配置已完成 Job 的清理](/zh/docs/concepts/workloads/controllers/ttlafterfinished/) |
| 347 | + |
| 348 | +<!-- * [Configuring unused container and image garbage collection](/docs/tasks/administer-cluster/reconfigure-kubelet/) --> |
| 349 | + |
| 350 | +## {{% heading "whatsnext" %}} |
| 351 | + |
| 352 | +<!-- |
| 353 | +* Learn more about [ownership of Kubernetes objects](/docs/concepts/overview/working-with-objects/owners-dependents/). |
| 354 | +* Learn more about Kubernetes [finalizers](/docs/concepts/overview/working-with-objects/finalizers/). |
| 355 | +* Learn about the [TTL controller](/docs/concepts/workloads/controllers/ttlafterfinished/) (beta) that cleans up finished Jobs. |
| 356 | +--> |
| 357 | +* 进一步了解 [Kubernetes 对象的属主关系](/zh/docs/concepts/overview/working-with-objects/owners-dependents/)。 |
| 358 | +* 进一步了解 Kubernetes [finalizers](/zh/docs/concepts/overview/working-with-objects/finalizers/)。 |
| 359 | +* 进一步了解 [TTL 控制器](/zh/docs/concepts/workloads/controllers/ttlafterfinished/) (beta), |
| 360 | + 该控制器负责清理已完成的 Job。 |
| 361 | + |
0 commit comments