Skip to content

Commit d581ee0

Browse files
authored
Merge pull request #47870 from windsonsea/0816m
[zh] Sync 2024-08-16-matchlabelkeys-podaffinity.md
2 parents 572bc5e + 30551f9 commit d581ee0

File tree

1 file changed

+85
-37
lines changed

1 file changed

+85
-37
lines changed

content/zh-cn/blog/_posts/2024-08-16-matchlabelkeys-podaffinity.md

Lines changed: 85 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ date: 2024-08-16
55
slug: matchlabelkeys-podaffinity
66
author: >
77
Kensei Nakada (Tetrate)
8+
translator: >
9+
[Michael Yao](https://github.com/windsonsea) (DaoCloud)
810
---
911
<!--
1012
layout: blog
@@ -16,51 +18,51 @@ author: >
1618
-->
1719

1820
<!--
19-
Kubernetes 1.29 introduced new fields `MatchLabelKeys` and `MismatchLabelKeys` in PodAffinity and PodAntiAffinity.
21+
Kubernetes 1.29 introduced new fields `matchLabelKeys` and `mismatchLabelKeys` in `podAffinity` and `podAntiAffinity`.
2022
2123
In Kubernetes 1.31, this feature moves to beta and the corresponding feature gate (`MatchLabelKeysInPodAffinity`) gets enabled by default.
2224
-->
23-
Kubernetes 1.29 在 podAffinity 和 podAntiAffinity 中引入了新的字段 `matchLabelKeys``mismatchLabelKeys`
25+
Kubernetes 1.29 在 `podAffinity``podAntiAffinity` 中引入了新的字段 `matchLabelKeys``mismatchLabelKeys`
2426

2527
在 Kubernetes 1.31 中,此特性进阶至 Beta,并且相应的特性门控(`MatchLabelKeysInPodAffinity`)默认启用。
2628

2729
<!--
28-
## `MatchLabelKeys` - Enhanced scheduling for versatile rolling updates
30+
## `matchLabelKeys` - Enhanced scheduling for versatile rolling updates
2931
30-
During a workload's (e.g., Deployment) rolling update, a cluster may have Pods from multiple versions at the same time.
31-
However, the scheduler cannot distinguish between old and new versions based on the `LabelSelector` specified in PodAffinity or PodAntiAffinity. As a result, it will co-locate or disperse Pods regardless of their versions.
32+
During a workload's (e.g., Deployment) rolling update, a cluster may have Pods from multiple versions at the same time.
33+
However, the scheduler cannot distinguish between old and new versions based on the `labelSelector` specified in `podAffinity` or `podAntiAffinity`. As a result, it will co-locate or disperse Pods regardless of their versions.
3234
-->
3335
## `matchLabelKeys` - 为多样化滚动更新增强了调度
3436

3537
在工作负载(例如 Deployment)的滚动更新期间,集群中可能同时存在多个版本的 Pod。
36-
然而,调度器无法基于 podAffinity 或 podAntiAffinity 中指定的 `labelSelector` 区分新旧版本。
38+
然而,调度器无法基于 `podAffinity``podAntiAffinity` 中指定的 `labelSelector` 区分新旧版本。
3739
结果,调度器将并置或分散调度 Pod,不会考虑这些 Pod 的版本。
3840

3941
<!--
4042
This can lead to sub-optimal scheduling outcome, for example:
41-
- New version Pods are co-located with old version Pods (PodAffinity), which will eventually be removed after rolling updates.
42-
- Old version Pods are distributed across all available topologies, preventing new version Pods from finding nodes due to PodAntiAffinity.
43+
- New version Pods are co-located with old version Pods (`podAffinity`), which will eventually be removed after rolling updates.
44+
- Old version Pods are distributed across all available topologies, preventing new version Pods from finding nodes due to `podAntiAffinity`.
4345
-->
4446
这可能导致次优的调度结果,例如:
4547

46-
- 新版本的 Pod 与旧版本的 Pod(podAffinity)并置在一起,这些旧版本的 Pod 最终将在滚动更新后被移除。
47-
- 旧版本的 Pod 被分布在所有可用的拓扑中,导致新版本的 Pod 由于 podAntiAffinity 无法找到节点。
48+
- 新版本的 Pod 与旧版本的 Pod(`podAffinity`)并置在一起,这些旧版本的 Pod 最终将在滚动更新后被移除。
49+
- 旧版本的 Pod 被分布在所有可用的拓扑中,导致新版本的 Pod 由于 `podAntiAffinity` 无法找到节点。
4850

4951
<!--
50-
`MatchLabelKeys` is a set of Pod label keys and addresses this problem.
51-
The scheduler looks up the values of these keys from the new Pod's labels and combines them with `LabelSelector`
52-
so that PodAffinity matches Pods that have the same key-value in labels.
52+
`matchLabelKeys` is a set of Pod label keys and addresses this problem.
53+
The scheduler looks up the values of these keys from the new Pod's labels and combines them with `labelSelector`
54+
so that podAffinity matches Pods that have the same key-value in labels.
5355
54-
By using label [pod-template-hash](/docs/concepts/workloads/controllers/deployment/#pod-template-hash-label) in `MatchLabelKeys`,
55-
you can ensure that only Pods of the same version are evaluated for PodAffinity or PodAntiAffinity.
56+
By using label [pod-template-hash](/docs/concepts/workloads/controllers/deployment/#pod-template-hash-label) in `matchLabelKeys`,
57+
you can ensure that only Pods of the same version are evaluated for `podAffinity` or `podAntiAffinity`.
5658
-->
5759
`matchLabelKeys` 是一组 Pod 标签键,可以解决上述问题。
5860
调度器从新 Pod 的标签中查找这些键的值,并将其与 `labelSelector` 结合,
59-
以便 podAffinity 匹配到具有相同标签键值的 Pod。
61+
以便 `podAffinity` 匹配到具有相同标签键值的 Pod。
6062

6163
通过在 `matchLabelKeys` 中使用标签
6264
[pod-template-hash](/zh-cn/docs/concepts/workloads/controllers/deployment/#pod-template-hash-label)
63-
你可以确保对 podAffinity 或 podAntiAffinity 进行评估时仅考虑相同版本的 Pod。
65+
你可以确保对 `podAffinity``podAntiAffinity` 进行评估时仅考虑相同版本的 Pod。
6466

6567
```yaml
6668
apiVersion: apps/v1
@@ -78,12 +80,12 @@ metadata:
7880
values:
7981
- database
8082
topologyKey: topology.kubernetes.io/zone
81-
matchLabelKeys:
83+
matchLabelKeys:
8284
- pod-template-hash
8385
```
8486
8587
<!--
86-
The above matchLabelKeys will be translated in Pods like:
88+
The above `matchLabelKeys` will be translated in Pods like:
8789
-->
8890
上述 Pod 中的 `matchLabelKeys` 将被转换为:
8991

@@ -116,29 +118,29 @@ metadata:
116118
```
117119

118120
<!--
119-
## `MismatchLabelKeys` - Service isolation
121+
## `mismatchLabelKeys` - Service isolation
120122

121-
`MismatchLabelKeys` is a set of Pod label keys, like `MatchLabelKeys`,
122-
which looks up the values of these keys from the new Pod's labels, and merge them with `LabelSelector` as `key notin (value)`
123-
so that PodAffinity does _not_ match Pods that have the same key-value in labels.
123+
`mismatchLabelKeys` is a set of Pod label keys, like `matchLabelKeys`,
124+
which looks up the values of these keys from the new Pod's labels, and merge them with `labelSelector` as `key notin (value)`
125+
so that `podAffinity` does _not_ match Pods that have the same key-value in labels.
124126

125127
Suppose all Pods for each tenant get `tenant` label via a controller or a manifest management tool like Helm.
126128
-->
127129
## `mismatchLabelKeys` - 服务隔离
128130

129131
`mismatchLabelKeys` 是一组 Pod 标签键,类似于 `matchLabelKeys`,
130132
它在新 Pod 的标签中查找这些键的值,并将其与 `labelSelector` 合并为 `key notin (value)`,
131-
以便 podAffinity **不**会匹配到具有相同标签键值的 Pod。
133+
以便 `podAffinity` **不**会匹配到具有相同标签键值的 Pod。
132134

133135
假设每个租户的所有 Pod 通过控制器或像 Helm 这样的清单管理工具得到 `tenant` 标签。
134136

135137
<!--
136-
Although the value of `tenant` label is unknown when composing each workload's manifest,
138+
Although the value of `tenant` label is unknown when composing each workload's manifest,
137139
the cluster admin wants to achieve exclusive 1:1 tenant to domain placement for a tenant isolation.
138140

139-
`MismatchLabelKeys` works for this usecase;
140-
By applying the following affinity globally using a mutating webhook,
141-
the cluster admin can ensure that the Pods from the same tenant will land on the same domain exclusively,
141+
`mismatchLabelKeys` works for this usecase;
142+
By applying the following affinity globally using a mutating webhook,
143+
the cluster admin can ensure that the Pods from the same tenant will land on the same domain exclusively,
142144
meaning Pods from other tenants won't land on the same domain.
143145
-->
144146
尽管在组合每个工作负载的清单时,`tenant` 标签的值是未知的,
@@ -150,8 +152,23 @@ meaning Pods from other tenants won't land on the same domain.
150152
这意味着来自其他租户的 Pod 不会落到同一域上。
151153

152154
<!--
153-
# ensures the pods of this tenant land on the same node pool
154-
# ensures only Pods from this tenant lands on the same node pool
155+
```yaml
156+
affinity:
157+
podAffinity: # ensures the pods of this tenant land on the same node pool
158+
requiredDuringSchedulingIgnoredDuringExecution:
159+
- matchLabelKeys:
160+
- tenant
161+
topologyKey: node-pool
162+
podAntiAffinity: # ensures only Pods from this tenant lands on the same node pool
163+
requiredDuringSchedulingIgnoredDuringExecution:
164+
- mismatchLabelKeys:
165+
- tenant
166+
labelSelector:
167+
matchExpressions:
168+
- key: tenant
169+
operator: Exists
170+
topologyKey: node-pool
171+
```
155172
-->
156173
```yaml
157174
affinity:
@@ -172,13 +189,44 @@ affinity:
172189
```
173190

174191
<!--
175-
The above matchLabelKeys and mismatchLabelKeys will be translated to like:
192+
The above `matchLabelKeys` and `mismatchLabelKeys` will be translated to like:
176193
-->
177194
上述的 `matchLabelKeys` 和 `mismatchLabelKeys` 将被转换为:
178195

179196
<!--
180-
# ensures the pods of this tenant land on the same node pool
181-
# ensures only Pods from this tenant lands on the same node pool
197+
```yaml
198+
kind: Pod
199+
metadata:
200+
name: application-server
201+
labels:
202+
tenant: service-a
203+
spec:
204+
affinity:
205+
podAffinity: # ensures the pods of this tenant land on the same node pool
206+
requiredDuringSchedulingIgnoredDuringExecution:
207+
- matchLabelKeys:
208+
- tenant
209+
topologyKey: node-pool
210+
labelSelector:
211+
matchExpressions:
212+
- key: tenant
213+
operator: In
214+
values:
215+
- service-a
216+
podAntiAffinity: # ensures only Pods from this tenant lands on the same node pool
217+
requiredDuringSchedulingIgnoredDuringExecution:
218+
- mismatchLabelKeys:
219+
- tenant
220+
labelSelector:
221+
matchExpressions:
222+
- key: tenant
223+
operator: Exists
224+
- key: tenant
225+
operator: NotIn
226+
values:
227+
- service-a
228+
topologyKey: node-pool
229+
```
182230
-->
183231
```yaml
184232
kind: Pod
@@ -215,7 +263,7 @@ spec:
215263
```
216264

217265
<!--
218-
## Getting involved
266+
## Getting involved
219267

220268
These features are managed by Kubernetes [SIG Scheduling](https://github.com/kubernetes/community/tree/master/sig-scheduling).
221269

@@ -229,10 +277,10 @@ Please join us and share your feedback. We look forward to hearing from you!
229277
请加入我们并分享你的反馈。我们期待听到你的声音!
230278

231279
<!--
232-
## How can I learn more?
280+
## How can I learn more?
233281

234-
- [The official document of PodAffinity](/docs/concepts/scheduling-eviction/assign-pod-node/#inter-pod-affinity-and-anti-affinity)
235-
- [KEP-3633: Introduce MatchLabelKeys and MismatchLabelKeys to PodAffinity and PodAntiAffinity](https://github.com/kubernetes/enhancements/blob/master/keps/sig-scheduling/3633-matchlabelkeys-to-podaffinity/README.md#story-2)
282+
- [The official document of podAffinity](/docs/concepts/scheduling-eviction/assign-pod-node/#inter-pod-affinity-and-anti-affinity)
283+
- [KEP-3633: Introduce matchLabelKeys and mismatchLabelKeys to podAffinity and podAntiAffinity](https://github.com/kubernetes/enhancements/blob/master/keps/sig-scheduling/3633-matchlabelkeys-to-podaffinity/README.md#story-2)
236284
-->
237285
## 了解更多
238286

0 commit comments

Comments
 (0)