一、拓扑域约束

Kubernetes 的 topologySpreadConstraints(拓扑域约束) 是一种高级的调度策略,用于确保工作负载的副本在集群中的不同拓扑域(如节点、可用区、区域等)之间均匀分布。

拓扑域约束yaml示例文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: example
  template:
    metadata:
      labels:
        app: example
    spec:
      topologySpreadConstraints:
      - maxSkew: 1
        whenUnsatisfiable: DoNotSchedule
        topologyKey: kubernetes.io/hostname
        labelSelector:
          matchLabels:
            app: example
      containers:
      - name: example
        image: registry.cn-hangzhou.aliyuncs.com/zq-demo/nginx:1.14.2

相关参数说明:

  • topologySpreadConstraints:拓扑域约束配置,可以是多个副本均匀分布在不同的域中,配置多个时,需要全部满足
  • maxSkew:指定允许的最大偏差。例如,如果 maxSkew 设置为 1,那么在任何拓扑域中,副本的数量最多只能相差 1
  • whenUnsatisfiable:指定当无法满足拓扑约束时的行为
  • DoNotSchedule:不允许调度新的 Pod,直到满足约束
  • ScheduleAnyway:即使不满足约束,也允许调度新的 Pod
  • topologyKey:指定拓扑域的键
  • labelSelector:指定要应用拓扑约束的 Pod 的标签选择器,通常配置为当前 Pod 的标签