您可以指定哪些 Pod 需要分组,它们分布在哪些拓扑域中,以及可接受的倾斜度。
根据区域分布匹配指定标签的 Pod 的示例
apiVersion: v1
kind: Pod
metadata:
name: my-pod
labels:
region: us-east
spec:
securityContext:
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
topologySpreadConstraints:
- maxSkew: 1 (1)
topologyKey: topology.kubernetes.io/zone (2)
whenUnsatisfiable: DoNotSchedule (3)
labelSelector: (4)
matchLabels:
region: us-east (5)
matchLabelKeys:
- my-pod-label (6)
containers:
- image: "docker.io/ocpqe/hello-pod"
name: hello-pod
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop: [ALL]
1 |
任意两个拓扑域之间 Pod 数量的最大差异。默认为1 ,不能指定0 值。 |
2 |
节点标签的键。具有此键和相同值的节点被认为位于同一拓扑中。 |
3 |
如果 Pod 不满足扩散约束,如何处理。默认为DoNotSchedule ,这意味着告诉调度程序不要调度该 Pod。设置为ScheduleAnyway 仍然调度 Pod,但调度程序会优先考虑遵守倾斜度,以免使集群更加不平衡。 |
4 |
匹配此标签选择器的 Pod 在扩散以满足约束时会被计算并识别为一个组。请务必指定标签选择器,否则无法匹配任何 Pod。 |
5 |
如果您希望将来正确计算此 Pod,请确保此Pod 规范也设置其标签以匹配此标签选择器。 |
6 |
用于选择要计算其扩散的 Pod 的 Pod 标签键列表。 |
演示单个 Pod 拓扑扩散约束的示例
kind: Pod
apiVersion: v1
metadata:
name: my-pod
labels:
region: us-east
spec:
securityContext:
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
region: us-east
containers:
- image: "docker.io/ocpqe/hello-pod"
name: hello-pod
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop: [ALL]
前面的示例定义了一个带有单个 Pod 拓扑扩散约束的Pod
规范。它匹配标记为region: us-east
的 Pod,在区域之间分布,指定倾斜度为1
,如果不满足这些要求,则不调度 Pod。
演示多个 Pod 拓扑扩散约束的示例
kind: Pod
apiVersion: v1
metadata:
name: my-pod-2
labels:
region: us-east
spec:
securityContext:
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
topologySpreadConstraints:
- maxSkew: 1
topologyKey: node
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
region: us-east
- maxSkew: 1
topologyKey: rack
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
region: us-east
containers:
- image: "docker.io/ocpqe/hello-pod"
name: hello-pod
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop: [ALL]
前面的示例定义了一个带有两个 Pod 拓扑扩散约束的Pod
规范。两者都匹配标记为region: us-east
的 Pod,指定倾斜度为1
,如果不满足这些要求,则不调度 Pod。
第一个约束基于用户定义的标签node
分布 Pod,第二个约束基于用户定义的标签rack
分布 Pod。必须满足这两个约束才能调度 Pod。