Kubernetes 1.31: PodAffinity 中的 MatchLabelKeys 晋升至 Beta
Kubernetes 1.29 在 podAffinity
和 podAntiAffinity
中引入了新字段 matchLabelKeys
和 mismatchLabelKeys
。
在 Kubernetes 1.31 中,此功能进入 Beta 阶段,并且相应的特性门控 (MatchLabelKeysInPodAffinity
) 默认启用。
matchLabelKeys
- 增强调度,实现灵活的滚动更新
在工作负载(例如 Deployment)进行滚动更新期间,集群中可能同时存在多个版本的 Pod。然而,调度器无法根据 podAffinity
或 podAntiAffinity
中指定的 labelSelector
来区分新旧版本。因此,它会不区分版本地将 Pods 共同定位或分散。
这可能导致次优的调度结果,例如:
- 新版本 Pods 与旧版本 Pods 共存(
podAffinity
),而旧版本 Pods 最终会在滚动更新后被移除。 - 旧版本 Pods 分布在所有可用的拓扑中,导致新版本 Pods 由于
podAntiAffinity
无法找到节点。
matchLabelKeys
是一组 Pod 标签键,用于解决此问题。调度器从新 Pod 的标签中查找这些键的值,并将它们与 labelSelector
结合,从而使 podAffinity 匹配标签中具有相同键值的 Pods。
通过在 matchLabelKeys
中使用标签 pod-template-hash,您可以确保只有相同版本的 Pods 会被评估用于 podAffinity
或 podAntiAffinity
。
apiVersion: apps/v1
kind: Deployment
metadata:
name: application-server
...
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- database
topologyKey: topology.kubernetes.io/zone
matchLabelKeys:
- pod-template-hash
上述 matchLabelKeys
在 Pods 中将被转换为:
kind: Pod
metadata:
name: application-server
labels:
pod-template-hash: xyz
...
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- database
- key: pod-template-hash # Added from matchLabelKeys; Only Pods from the same replicaset will match this affinity.
operator: In
values:
- xyz
topologyKey: topology.kubernetes.io/zone
matchLabelKeys:
- pod-template-hash
mismatchLabelKeys
- 服务隔离
mismatchLabelKeys
是一组 Pod 标签键,类似于 matchLabelKeys
,它从新 Pod 的标签中查找这些键的值,并将它们与 labelSelector
合并为 key notin (value)
,从而使 podAffinity
不匹配标签中具有相同键值的 Pods。
假设每个租户的所有 Pods 都通过控制器或像 Helm 这样的清单管理工具获得 tenant
标签。
尽管在编写每个工作负载的清单时 tenant
标签的值未知,但集群管理员希望通过独占的租户到域 1:1 放置来实现租户隔离。
mismatchLabelKeys
适用于此用例;通过使用一个可变 admission webhook 全局应用以下 affinity,集群管理员可以确保来自同一租户的 Pods 独占地落在同一个域上,这意味着来自其他租户的 Pods 不会落在同一个域上。
affinity:
podAffinity: # ensures the pods of this tenant land on the same node pool
requiredDuringSchedulingIgnoredDuringExecution:
- matchLabelKeys:
- tenant
topologyKey: node-pool
podAntiAffinity: # ensures only Pods from this tenant lands on the same node pool
requiredDuringSchedulingIgnoredDuringExecution:
- mismatchLabelKeys:
- tenant
labelSelector:
matchExpressions:
- key: tenant
operator: Exists
topologyKey: node-pool
上述 matchLabelKeys
和 mismatchLabelKeys
将被转换为:
kind: Pod
metadata:
name: application-server
labels:
tenant: service-a
spec:
affinity:
podAffinity: # ensures the pods of this tenant land on the same node pool
requiredDuringSchedulingIgnoredDuringExecution:
- matchLabelKeys:
- tenant
topologyKey: node-pool
labelSelector:
matchExpressions:
- key: tenant
operator: In
values:
- service-a
podAntiAffinity: # ensures only Pods from this tenant lands on the same node pool
requiredDuringSchedulingIgnoredDuringExecution:
- mismatchLabelKeys:
- tenant
labelSelector:
matchExpressions:
- key: tenant
operator: Exists
- key: tenant
operator: NotIn
values:
- service-a
topologyKey: node-pool
参与其中
这些特性由 Kubernetes SIG Scheduling 管理。
请加入我们并分享您的反馈。我们期待您的来信!