【博客589】K8s Topology Spread Constraints
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
K8s Topology Spread Constraints
场景
你可以使用 拓扑分布约束Topology Spread Constraints 来控制 Pod 在集群内故障域之间的分布 例如区域Region、可用区Zone、节点和其他用户自定义拓扑域。 这样做有助于实现高可用并提升资源利用率。
用法
适用于
deploymentstatefulsetdaemonset
格式
---
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
# 配置一个拓扑分布约束
topologySpreadConstraints:
- maxSkew: <integer>
minDomains: <integer> # 可选自从 v1.25 开始成为 Beta
topologyKey: <string>
whenUnsatisfiable: <string>
labelSelector: <object>
matchLabelKeys: <list> # 可选自从 v1.25 开始成为 Alpha
nodeAffinityPolicy: [Honor|Ignore] # 可选自从 v1.26 开始成为 Beta
nodeTaintsPolicy: [Honor|Ignore] # 可选自从 v1.26 开始成为 Beta
### 其他 Pod 字段置于此处
参数解析
示例
1、结合NodeSelector/NodeAffinity一起使用
设有一个集群其节点分别用"env = prod”“env = staging"和"env = qa"标记现在您想将Pod均匀地跨区域放置到"qa"环境中
2、高阶多拓扑分布约束
我们希望同时将Pod调度到具有2个需求的集群中Pod跨区域均匀放置和Pod跨节点均匀放置
spec:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector:
- matchLabels:
app: nginx
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
- matchLabels:
app: nginx
3、将 Pod 最大程度上均匀的打散调度到各个节点上
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
labelSelector:
- matchLabels:
app: nginx
containers:
- name: nginx
image: nginx
配置解析
topologyKey: 与 podAntiAffinity 中配置类似。
labelSelector: 与 podAntiAffinity 中配置类似只是这里可以支持选中多组 pod 的 label。
maxSkew: 必须是大于零的整数表示能容忍不同拓扑域中 Pod 数量差异的最大值。这里的 1 意味着只允许相差 1 个 Pod。
whenUnsatisfiable: 指示不满足条件时如何处理。DoNotSchedule 不调度 (保持 Pending)类似强反亲和ScheduleAnyway 表示要调度类似弱反亲和
以上配置连起来解释
将所有 nginx 的 Pod 严格均匀打散调度到不同节点上不同节点上 nginx 的副本数量最多只能相差 1 个如果有节点因其它因素无法调度更多的 Pod (比如资源不足)那么就让剩余的 nginx 副本 Pending。
4、如果要在所有节点中严格打散通常不太可取可以加下 nodeAffinity只在部分资源充足的节点严格打散
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: io
operator: In
values:
- high
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
labelSelector:
- matchLabels:
app: nginx