您可以指定哪些 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 不满足分布约束,如何处理该 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。