|
| 1 | +--- |
| 2 | +title: 属主与附属 |
| 3 | +content_type: concept |
| 4 | +weight: 60 |
| 5 | +--- |
| 6 | +<!-- |
| 7 | +title: Owners and Dependents |
| 8 | +content_type: concept |
| 9 | +weight: 60 |
| 10 | +--> |
| 11 | + |
| 12 | +<!-- overview --> |
| 13 | + |
| 14 | + |
| 15 | +<!-- |
| 16 | +In Kubernetes, some objects are *owners* of other objects. For example, a |
| 17 | +{{<glossary_tooltip text="ReplicaSet" term_id="replica-set">}} is the owner of a set of Pods. These owned objects are *dependents* |
| 18 | +of their owner. |
| 19 | +--> |
| 20 | + |
| 21 | +在 Kubernetes 中,一些对象是其他对象的*属主(Owner)*。 |
| 22 | +例如,{{<glossary_tooltip text="ReplicaSet" term_id="replica-set">}} 是一组 Pod 的属主。 |
| 23 | +具有属主的对象是属主的*附属(Dependent)* 。 |
| 24 | + |
| 25 | +<!-- |
| 26 | +Ownership is different from the [labels and selectors](/docs/concepts/overview/working-with-objects/labels/) |
| 27 | +mechanism that some resources also use. For example, consider a Service that |
| 28 | +creates `EndpointSlice` objects. The Service uses labels to allow the control plane to |
| 29 | +determine which `EndpointSlice` objects are used for that Service. In addition |
| 30 | +to the labels, each `EndpointSlice` that is managed on behalf of a Service has |
| 31 | +an owner reference. Owner references help different parts of Kubernetes avoid |
| 32 | +interfering with objects they don’t control. |
| 33 | +--> |
| 34 | +属主关系不同于一些资源使用的[标签和选择算符](/zh/docs/concepts/overview/working-with-objects/labels/)机制。 |
| 35 | +例如,有一个创建 `EndpointSlice` 对象的 Service, |
| 36 | +该 Service 使用标签来让控制平面确定,哪些 `EndpointSlice` 对象属于该 Service。 |
| 37 | +除开标签,每个代表 Service 所管理的 `EndpointSlice` 都有一个属主引用。 |
| 38 | +属主引用避免 Kubernetes 的不同部分干扰到不受它们控制的对象。 |
| 39 | + |
| 40 | +<!-- |
| 41 | +## Owner references in object specifications |
| 42 | +
|
| 43 | +Dependent objects have a `metadata.ownerReferences` field that references their |
| 44 | +owner object. A valid owner reference consists of the object name and a UID |
| 45 | +within the same namespace as the dependent object. Kubernetes sets the value of |
| 46 | +this field automatically for objects that are dependents of other objects like |
| 47 | +ReplicaSets, DaemonSets, Deployments, Jobs and CronJobs, and ReplicationControllers. |
| 48 | +You can also configure these relationships manually by changing the value of |
| 49 | +this field. However, you usually don't need to and can allow Kubernetes to |
| 50 | +automatically manage the relationships. |
| 51 | +--> |
| 52 | +## 对象规约中的属主引用 {#owner-references-in-object-specifications} |
| 53 | + |
| 54 | +附属对象有一个 `metadata.ownerReferences` 字段,用于引用其属主对象。 |
| 55 | +一个有效的属主引用,包含与附属对象同在一个命名空间下的对象名称和一个 UID。 |
| 56 | +Kubernetes 自动为一些对象的附属资源设置属主引用的值, |
| 57 | +这些对象包含 ReplicaSet、DaemonSet、Deployment、Job、CronJob、ReplicationController 等。 |
| 58 | +你也可以通过改变这个字段的值,来手动配置这些关系。 |
| 59 | +然而,你通常不需要这么做,你可以让 Kubernetes 自动管理附属关系。 |
| 60 | + |
| 61 | +<!-- |
| 62 | +Dependent objects also have an `ownerReferences.blockOwnerDeletion` field that |
| 63 | +takes a boolean value and controls whether specific dependents can block garbage |
| 64 | +collection from deleting their owner object. Kubernetes automatically sets this |
| 65 | +field to `true` if a {{<glossary_tooltip text="controller" term_id="controller">}} |
| 66 | +(for example, the Deployment controller) sets the value of the |
| 67 | +`metadata.ownerReferences` field. You can also set the value of the |
| 68 | +`blockOwnerDeletion` field manually to control which dependents block garbage |
| 69 | +collection. |
| 70 | +
|
| 71 | +A Kubernetes admission controller controls user access to change this field for |
| 72 | +dependent resources, based on the delete permissions of the owner. This control |
| 73 | +prevents unauthorized users from delaying owner object deletion. |
| 74 | +--> |
| 75 | +附属对象还有一个 `ownerReferences.blockOwnerDeletion` 字段,该字段使用布尔值, |
| 76 | +用于控制特定的附属对象是否可以阻止垃圾收集删除其属主对象。 |
| 77 | +如果{{<glossary_tooltip text="控制器" term_id="controller">}}(例如 Deployment 控制器) |
| 78 | +设置了 `metadata.ownerReferences` 字段的值,Kubernetes 会自动设置 |
| 79 | +`blockOwnerDeletion` 的值为 `true`。 |
| 80 | +你也可以手动设置 `blockOwnerDeletion` 字段的值,以控制哪些附属对象会阻止垃圾收集。 |
| 81 | + |
| 82 | +{{< note >}} |
| 83 | +<!-- |
| 84 | +Cross-namespace owner references are disallowed by design. |
| 85 | +Namespaced dependents can specify cluster-scoped or namespaced owners. |
| 86 | +A namespaced owner **must** exist in the same namespace as the dependent. |
| 87 | +If it does not, the owner reference is treated as absent, and the dependent |
| 88 | +is subject to deletion once all owners are verified absent. |
| 89 | +
|
| 90 | +Cluster-scoped dependents can only specify cluster-scoped owners. |
| 91 | +In v1.20+, if a cluster-scoped dependent specifies a namespaced kind as an owner, |
| 92 | +it is treated as having an unresolvable owner reference, and is not able to be garbage collected. |
| 93 | +
|
| 94 | +In v1.20+, if the garbage collector detects an invalid cross-namespace `ownerReference`, |
| 95 | +or a cluster-scoped dependent with an `ownerReference` referencing a namespaced kind, a warning Event |
| 96 | +with a reason of `OwnerRefInvalidNamespace` and an `involvedObject` of the invalid dependent is reported. |
| 97 | +You can check for that kind of Event by running |
| 98 | +`kubectl get events -A --field-selector=reason=OwnerRefInvalidNamespace`. |
| 99 | +--> |
| 100 | +根据设计,kubernetes 不允许跨名字空间指定属主。 |
| 101 | +名字空间范围的附属可以指定集群范围的或者名字空间范围的属主。 |
| 102 | +名字空间范围的属主**必须**和该附属处于相同的名字空间。 |
| 103 | +如果名字空间范围的属主和附属不在相同的名字空间,那么该属主引用就会被认为是缺失的, |
| 104 | +并且当附属的所有属主引用都被确认不再存在之后,该附属就会被删除。 |
| 105 | + |
| 106 | +集群范围的附属只能指定集群范围的属主。 |
| 107 | +在 v1.20+ 版本,如果一个集群范围的附属指定了一个名字空间范围类型的属主, |
| 108 | +那么该附属就会被认为是拥有一个不可解析的属主引用,并且它不能够被垃圾回收。 |
| 109 | + |
| 110 | +在 v1.20+ 版本,如果垃圾收集器检测到无效的跨名字空间的属主引用, |
| 111 | +或者一个集群范围的附属指定了一个名字空间范围类型的属主, |
| 112 | +那么它就会报告一个警告事件。该事件的原因是 `OwnerRefInvalidNamespace`, |
| 113 | +`involvedObject` 属性中包含无效的附属。 |
| 114 | +你可以运行 `kubectl get events -A --field-selector=reason=OwnerRefInvalidNamespace` |
| 115 | +来获取该类型的事件。 |
| 116 | +{{< /note >}} |
| 117 | + |
| 118 | +<!-- |
| 119 | +## Ownership and finalizers |
| 120 | +
|
| 121 | +When you tell Kubernetes to delete a resource, the API server allows the |
| 122 | +managing controller to process any [finalizer rules](/docs/concepts/overview/working-with-objects/finalizers/) |
| 123 | +for the resource. {{<glossary_tooltip text="Finalizers" term_id="finalizer">}} |
| 124 | +prevent accidental deletion of resources your cluster may still need to function |
| 125 | +correctly. For example, if you try to delete a `PersistentVolume` that is still |
| 126 | +in use by a Pod, the deletion does not happen immediately because the |
| 127 | +`PersistentVolume` has the `kubernetes.io/pv-protection` finalizer on it. |
| 128 | +Instead, the volume remains in the `Terminating` status until Kubernetes clears |
| 129 | +the finalizer, which only happens after the `PersistentVolume` is no longer |
| 130 | +bound to a Pod. |
| 131 | +--> |
| 132 | +## 属主关系与 Finalizer {#ownership-and-finalizers} |
| 133 | + |
| 134 | +当你告诉 Kubernetes 删除一个资源,API 服务器允许管理控制器处理该资源的任何 |
| 135 | +[Finalizer 规则](/zh/docs/concepts/overview/working-with-objects/finalizers/)。 |
| 136 | +{{<glossary_tooltip text="Finalizer" term_id="finalizer">}} |
| 137 | +防止意外删除你的集群所依赖的、用于正常运作的资源。 |
| 138 | +例如,如果你试图删除一个仍被 Pod 使用的 `PersistentVolume`,该资源不会被立即删除, |
| 139 | +因为 `PersistentVolume` 有 `kubernetes.io/pv-protection` Finalizer。 |
| 140 | +相反,它将进入 `Terminating` 状态,直到 Kubernetes 清除这个 Finalizer, |
| 141 | +而这种情况只会发生在 `PersistentVolume` 不再被挂载到 Pod 上时。 |
| 142 | + |
| 143 | +<!-- |
| 144 | +Kubernetes also adds finalizers to an owner resource when you use either |
| 145 | +[foreground or orphan cascading deletion](/docs/concepts/architecture/garbage-collection/#cascading-deletion). |
| 146 | +In foreground deletion, it adds the `foreground` finalizer so that the |
| 147 | +controller must delete dependent resources that also have |
| 148 | +`ownerReferences.blockOwnerDeletion=true` before it deletes the owner. If you |
| 149 | +specify an orphan deletion policy, Kubernetes adds the `orphan` finalizer so |
| 150 | +that the controller ignores dependent resources after it deletes the owner |
| 151 | +object. |
| 152 | +--> |
| 153 | +当你使用[前台或孤立级联删除](/zh/docs/concepts/architecture/garbage-collection/#cascading-deletion)时, |
| 154 | +Kubernetes 也会向属主资源添加 Finalizer。 |
| 155 | +在前台删除中,会添加 `foreground` Finalizer,这样控制器必须在删除了拥有 |
| 156 | +`ownerReferences.blockOwnerDeletion=true` 的附属资源后,才能删除属主对象。 |
| 157 | +如果你指定了孤立删除策略,Kubernetes 会添加 `orphan` Finalizer, |
| 158 | +这样控制器在删除属主对象后,会忽略附属资源。 |
| 159 | + |
| 160 | +## {{% heading "whatsnext" %}} |
| 161 | + |
| 162 | +<!-- |
| 163 | +* Learn more about [Kubernetes finalizers](/docs/concepts/overview/working-with-objects/finalizers/). |
| 164 | +* Learn about [garbage collection](/docs/concepts/architecture/garbage-collection). |
| 165 | +* Read the API reference for [object metadata](/docs/reference/kubernetes-api/common-definitions/object-meta/#System). |
| 166 | +--> |
| 167 | +* 了解更多关于 [Kubernetes Finalizer](/zh/docs/concepts/overview/working-with-objects/finalizers/)。 |
| 168 | +* 了解关于[垃圾收集](/zh/docs/concepts/architecture/garbage-collection)。 |
| 169 | +* 阅读[对象元数据](/docs/reference/kubernetes-api/common-definitions/object-meta/#System)的 API 参考文档。 |
0 commit comments