使用拓扑密钥的反亲和性规则部署失败:kubernetes.io/hostname - 必需值:不能为空

Bra*_*och 2 kubernetes

我有一个反关联规则,要求 kubernetes 将来自同一部署的 pod 调度到不同的节点上,我们已经成功使用了很长时间。

affinity:
  podAntiAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
    - topologyKey: kubernetes.io/hostname
      labelSelector:
        matchExpressions:
        - key: application
          operator: In
          values:
          - {{ $appName }}
        - key: proc  
          operator: In
          values:
          - {{ $procName }}
Run Code Online (Sandbox Code Playgroud)

我正在尝试将我的 pod 关联规则更新为强偏好而不是硬性要求,这样如果部署需要的副本数多于可用节点的数量,我们就不需要扩展我们的集群。

affinity:
  podAntiAffinity:
    preferredDuringSchedulingIgnoredDuringExecution:
    - topologyKey: kubernetes.io/hostname
      weight: 100
      labelSelector:
        matchExpressions:
        - key: application
          operator: In
          values:
          - {{ $appName }}
        - key: proc 
          operator: In
          values:
          - {{ $procName }}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试应用新规则时,topologyKey 出现意外错误:

Error: Deployment.apps "core--web" is invalid:
[spec.template.spec.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution[0].podAffinityTerm.topologyKey: Required value: can not be empty,
spec.template.spec.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution[0].podAffinityTerm.topologyKey: Invalid value: "": name part must be non-empty,
spec.template.spec.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution[0].podAffinityTerm.topologyKey: Invalid value: "": name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName',  or 'my.name',  or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')]
Run Code Online (Sandbox Code Playgroud)

调度程序似乎为拓扑键获取了一个空字符串值,即使我的所有节点都有一个与正则表达式匹配的指定键的标签:

$ kubectl describe nodes | grep kubernetes.io/hostname
kubernetes.io/hostname=ip-10-x-x-x.ec2.internal
kubernetes.io/hostname=ip-10-x-x-x.ec2.internal
kubernetes.io/hostname=ip-10-x-x-x.ec2.internal
kubernetes.io/hostname=ip-10-x-x-x.ec2.internal
Run Code Online (Sandbox Code Playgroud)

我没想到从必需到首选的简单更改中会出现这样的问题。我搞砸了什么导致了topologyKey错误?

Bra*_*och 10

required 和 preferred 的语法略有不同,请注意podAffinityTerm错误消息路径中的引用:

spec.template.spec.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution[0].podAffinityTerm.topologyKey
Run Code Online (Sandbox Code Playgroud)

首选调度的正确语法是:

affinity:
  podAntiAffinity:
    preferredDuringSchedulingIgnoredDuringExecution:
    - weight: 100
      podAffinityTerm:
        topologyKey: kubernetes.io/hostname
        labelSelector:
          matchExpressions:
          - key: application
            operator: In
            values:
            - {{ $appName }}
          - key: proc
            operator: In
            values:
            - {{ $procName }}
Run Code Online (Sandbox Code Playgroud)

请注意,这weight是一个顶级键,其同级键podAffinityTerm包含topologyKeylabelSelector