您可以指定哪些 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 必须满足这两个约束才能被调度。