|
| 1 | +--- |
| 2 | +title: Finalizers |
| 3 | +content_type: concept |
| 4 | +weight: 60 |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- overview --> |
| 8 | + |
| 9 | +{{<glossary_definition term_id="finalizer" length="long">}} |
| 10 | + |
| 11 | +<!-- |
| 12 | +You can use finalizers to control {{<glossary_tooltip text="garbage collection" term_id="garbage-collection">}} |
| 13 | +of resources by alerting {{<glossary_tooltip text="controllers" term_id="controller">}} to perform specific cleanup tasks before |
| 14 | +deleting the target resource. |
| 15 | +--> |
| 16 | +你可以通过使用 Finalizers 提醒{{<glossary_tooltip text="控制器" term_id="controller">}} |
| 17 | +在删除目标资源前执行特定的清理任务, |
| 18 | +来控制资源的{{<glossary_tooltip text="垃圾收集" term_id="garbage-collection">}}。 |
| 19 | + |
| 20 | +<!-- |
| 21 | +Finalizers don't usually specify the code to execute. Instead, they are |
| 22 | +typically lists of keys on a specific resource similar to annotations. |
| 23 | +Kubernetes specifies some finalizers automatically, but you can also specify |
| 24 | +your own. |
| 25 | +--> |
| 26 | +Finalizers 通常不指定要执行的代码。 |
| 27 | +相反,它们通常是特定资源上的键的列表,类似于注解。 |
| 28 | +Kubernetes 自动指定了一些 Finalizers,但你也可以指定你自己的。 |
| 29 | + |
| 30 | +<!-- |
| 31 | +## How finalizers work |
| 32 | +
|
| 33 | +When you create a resource using a manifest file, you can specify finalizers in |
| 34 | +the `metadata.finalizers` field. When you attempt to delete the resource, the |
| 35 | +controller that manages it notices the values in the `finalizers` field and does |
| 36 | +the following: |
| 37 | +
|
| 38 | + * Modifies the object to add a `metadata.deletionTimestamp` field with the |
| 39 | + time you started the deletion. |
| 40 | + * Marks the object as read-only until its `metadata.finalizers` field is empty. |
| 41 | +--> |
| 42 | +## Finalizers 如何工作 {#how-finalizers-work} |
| 43 | + |
| 44 | +当你使用清单文件创建资源时,你可以在 `metadata.finalizers` 字段指定 Finalizers。 |
| 45 | +当你试图删除该资源时,管理该资源的控制器会注意到 `finalizers` 字段中的值, |
| 46 | +并进行以下操作: |
| 47 | + |
| 48 | + * 修改对象,将你开始执行删除的时间添加到 `metadata.deletionTimestamp` 字段。 |
| 49 | + * 将该对象标记为只读,直到其 `metadata.finalizers` 字段为空。 |
| 50 | + |
| 51 | +<!-- |
| 52 | +The controller then attempts to satisfy the requirements of the finalizers |
| 53 | +specified for that resource. Each time a finalizer condition is satisfied, the |
| 54 | +controller removes that key from the resource's `finalizers` field. When the |
| 55 | +field is empty, garbage collection continues. You can also use finalizers to |
| 56 | +prevent deletion of unmanaged resources. |
| 57 | +--> |
| 58 | +然后,控制器试图满足资源的 Finalizers 的条件。 |
| 59 | +每当一个 Finalizer 的条件被满足时,控制器就会从资源的 `finalizers` 字段中删除该键。 |
| 60 | +当该字段为空时,垃圾收集继续进行。 |
| 61 | +你也可以使用 Finalizers 来阻止删除未被管理的资源。 |
| 62 | + |
| 63 | +<!-- |
| 64 | +A common example of a finalizer is `kubernetes.io/pv-protection`, which prevents |
| 65 | +accidental deletion of `PersistentVolume` objects. When a `PersistentVolume` |
| 66 | +object is in use by a Pod, Kubernetes adds the `pv-protection` finalizer. If you |
| 67 | +try to delete the `PersistentVolume`, it enters a `Terminating` status, but the |
| 68 | +controller can't delete it because the finalizer exists. When the Pod stops |
| 69 | +using the `PersistentVolume`, Kubernetes clears the `pv-protection` finalizer, |
| 70 | +and the controller deletes the volume. |
| 71 | +--> |
| 72 | +一个常见的 Finalizer 的例子是 `kubernetes.io/pv-protection`, |
| 73 | +它用来防止意外删除 `PersistentVolume` 对象。 |
| 74 | +当一个 `PersistentVolume` 对象被 Pod 使用时, |
| 75 | +Kubernetes 会添加 `pv-protection` Finalizer。 |
| 76 | +如果你试图删除 `PersistentVolume`,它将进入 `Terminating` 状态, |
| 77 | +但是控制器因为该 Finalizer 存在而无法删除该资源。 |
| 78 | +当 Pod 停止使用 `PersistentVolume` 时, |
| 79 | +Kubernetes 清除 `pv-protection` Finalizer,控制器就会删除该卷。 |
| 80 | + |
| 81 | +<!-- |
| 82 | +## Owner references, labels, and finalizers {#owners-labels-finalizers} |
| 83 | +
|
| 84 | +Like {{<glossary_tooltip text="labels" term_id="label">}}, [owner references](/concepts/overview/working-with-objects/owners-dependents/) |
| 85 | +describe the relationships between objects in Kubernetes, but are used for a |
| 86 | +different purpose. When a |
| 87 | +{{<glossary_tooltip text="controller" term_id="controller">}} manages objects |
| 88 | +like Pods, it uses labels to track changes to groups of related objects. For |
| 89 | +example, when a {{<glossary_tooltip text="Job" term_id="job">}} creates one or |
| 90 | +more Pods, the Job controller applies labels to those pods and tracks changes to |
| 91 | +any Pods in the cluster with the same label. |
| 92 | +--> |
| 93 | +## 属主引用、标签和 Finalizers {#owners-labels-finalizers} |
| 94 | + |
| 95 | +与{{<glossary_tooltip text="标签" term_id="label">}}类似, |
| 96 | +[属主引用](/zh/concepts/overview/working-with-objects/owners-dependents/) |
| 97 | +描述了 Kubernetes 中对象之间的关系,但它们作用不同。 |
| 98 | +当一个{{<glossary_tooltip text="控制器" term_id="controller">}} |
| 99 | +管理类似于 Pod 的对象时,它使用标签来跟踪相关对象组的变化。 |
| 100 | +例如,当 {{<glossary_tooltip text="Job" term_id="job">}} 创建一个或多个 Pod 时, |
| 101 | +Job 控制器会给这些 Pod 应用上标签,并跟踪集群中的具有相同标签的 Pod 的变化。 |
| 102 | + |
| 103 | +<!-- |
| 104 | +The Job controller also adds *owner references* to those Pods, pointing at the |
| 105 | +Job that created the Pods. If you delete the Job while these Pods are running, |
| 106 | +Kubernetes uses the owner references (not labels) to determine which Pods in the |
| 107 | +cluster need cleanup. |
| 108 | +
|
| 109 | +Kubernetes also processes finalizers when it identifies owner references on a |
| 110 | +resource targeted for deletion. |
| 111 | +
|
| 112 | +In some situations, finalizers can block the deletion of dependent objects, |
| 113 | +which can cause the targeted owner object to remain in a read-only state for |
| 114 | +longer than expected without being fully deleted. In these situations, you |
| 115 | +should check finalizers and owner references on the target owner and dependent |
| 116 | +objects to troubleshoot the cause. |
| 117 | +--> |
| 118 | +Job 控制器还为这些 Pod 添加了*属主引用*,指向创建 Pod 的 Job。 |
| 119 | +如果你在这些 Pod 运行的时候删除了 Job, |
| 120 | +Kubernetes 会使用属主引用(而不是标签)来确定集群中哪些 Pod 需要清理。 |
| 121 | + |
| 122 | +当 Kubernetes 识别到要删除的资源上的属主引用时,它也会处理 Finalizers。 |
| 123 | + |
| 124 | +在某些情况下,Finalizers 会阻止依赖对象的删除, |
| 125 | +这可能导致目标属主对象,保持在只读状态的时间比预期的长,且没有被完全删除。 |
| 126 | +在这些情况下,你应该检查目标属主和附属对象上的 Finalizers 和属主引用,来排查原因。 |
| 127 | + |
| 128 | +{{<note>}} |
| 129 | +<!-- |
| 130 | +In cases where objects are stuck in a deleting state, try to avoid manually |
| 131 | +removing finalizers to allow deletion to continue. Finalizers are usually added |
| 132 | +to resources for a reason, so forcefully removing them can lead to issues in |
| 133 | +your cluster. |
| 134 | +--> |
| 135 | +在对象卡在删除状态的情况下,尽量避免手动移除 Finalizers,以允许继续删除操作。 |
| 136 | +Finalizers 通常因为特殊原因被添加到资源上,所以强行删除它们会导致集群出现问题。 |
| 137 | +{{</note>}} |
| 138 | + |
| 139 | + |
| 140 | +## {{% heading "whatsnext" %}} |
| 141 | + |
| 142 | +<!-- |
| 143 | +* Read [Using Finalizers to Control Deletion](/blog/2021/05/14/using-finalizers-to-control-deletion/) |
| 144 | + on the Kubernetes blog. |
| 145 | +--> |
| 146 | +* 阅读 Kubernetes 博客的[使用 Finalizers 控制删除](/blog/2021/05/14/using-finalizers-to-control-deletion/)。 |
0 commit comments