在 Kubernetes 中,pod Affinity 和 pod Affinity 权重是否相互比较?还是独立? podAffinity 和 nodeAffinity 呢?

In Kubernetes, are podAffinity and podAntiAffinity weights compared to each other? Or idependently? What about podAffinity and nodeAffinity?

在 Kubernetes 中,podAffinitypodAntiAffinity 权重相互比较吗?还是独立? podAffinitynodeAffinity 呢? podAntiAffinity 会超过下面示例中的 podAffinity 吗?如果 nodeAffinity 也被添加到组合中会怎么样。

 affinity:
  podAffinity:
    preferredDuringSchedulingIgnoredDuringExecution:
    - weight: 50
      podAffinityTerm:
        labelSelector:
          matchExpressions:
          - key: app.kubernetes.io/name
            operator: In
            values:
            - test-1
        topologyKey: kubernetes.io/hostname

  podAntiAffinity:
    preferredDuringSchedulingIgnoredDuringExecution:
    - weight: 100
      podAffinityTerm:
        labelSelector:
          matchExpressions:
          - key: app.kubernetes.io/name
            operator: In
            values:
            - test-2          
        topologyKey: kubernetes.io/hostname

如果您不覆盖默认的个体权重 1,pods 和节点的所有亲和力设置将以相同的方式加权。

在执行期间,节点上亲和条件的出现次数乘以条件的权重。

pod a has an affinity to b
pod a has an anti-affinity to c

new pod of type "a" to be scheduled
---
node1 runs 3 "b" pods and 1 "c" pod --> affinity score: 2 (3 "b" pods - 1 "c" pod)
node2 runs 1 "b" pod --> affinity score 1: 1 (1 "b" pod)

根据亲和力计算,pod 将被安排在节点 1 上,尽管它已经 运行 pod “c” 因为 3 “b” pods 超过 1 “c” pod。

在您的示例中,如果节点上存在 test-1test-2 的 1:1 比率,则 anti-affinity 将超过关联设置。在节点上 test-1test-2 的 2:1 比率下,(反)亲和力会相互抵消。

Source