Kubernetes 1.31:PodAffinity 中的 MatchLabelKeys 功能升級至 Beta 版

Kubernetes 1.29 在 podAffinitypodAntiAffinity 中引入了新的欄位 matchLabelKeysmismatchLabelKeys

在 Kubernetes 1.31 中,此功能進入 Beta 階段,並且預設啟用對應的功能閘道 (MatchLabelKeysInPodAffinity)。

matchLabelKeys - 增強排程功能,以實現多功能的滾動更新

在工作負載(例如 Deployment)的滾動更新期間,叢集可能同時存在多個版本的 Pod。然而,排程器無法根據 podAffinitypodAntiAffinity 中指定的 labelSelector 來區分舊版本和新版本。因此,無論 Pod 的版本為何,它都會將 Pod 共置或分散。

這可能會導致次佳的排程結果,例如

  • 新版本 Pod 與舊版本 Pod 共置 (podAffinity),這些舊版本 Pod 最終會在滾動更新後移除。
  • 舊版本 Pod 分散在所有可用的拓撲中,由於 podAntiAffinity,阻止新版本 Pod 找到節點。

matchLabelKeys 是一組 Pod 標籤鍵,用於解決此問題。排程器從新 Pod 的標籤中查找這些鍵的值,並將它們與 labelSelector 結合,以便 podAffinity 匹配標籤中具有相同鍵值的 Pod。

透過在 matchLabelKeys 中使用標籤 pod-template-hash,您可以確保僅評估相同版本的 Pod 以進行 podAffinitypodAntiAffinity

apiVersion: apps/v1
kind: Deployment
metadata:
  name: application-server
...
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - database
        topologyKey: topology.kubernetes.io/zone
        matchLabelKeys:
        - pod-template-hash

上述 matchLabelKeys 將在 Pod 中轉換為類似

kind: Pod
metadata:
  name: application-server
  labels:
    pod-template-hash: xyz
...
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - database
          - key: pod-template-hash # Added from matchLabelKeys; Only Pods from the same replicaset will match this affinity.
            operator: In
            values:
            - xyz
        topologyKey: topology.kubernetes.io/zone
        matchLabelKeys:
        - pod-template-hash

mismatchLabelKeys - 服務隔離

mismatchLabelKeys 是一組 Pod 標籤鍵,類似於 matchLabelKeys,它從新 Pod 的標籤中查找這些鍵的值,並將它們與 labelSelector 合併為 key notin (value),以便 podAffinity *不* 匹配標籤中具有相同鍵值的 Pod。

假設每個租戶的所有 Pod 都透過控制器或資訊清單管理工具(如 Helm)取得 tenant 標籤。

儘管在撰寫每個工作負載的資訊清單時,tenant 標籤的值是未知的,但叢集管理員希望實現獨佔的 1:1 租戶到網域放置,以實現租戶隔離。

mismatchLabelKeys 適用於此用例;透過使用變異性 webhook 全域應用以下親和性,叢集管理員可以確保來自同一租戶的 Pod 將獨佔地落在同一網域上,這表示來自其他租戶的 Pod 不會落在同一網域上。

affinity:
  podAffinity:      # ensures the pods of this tenant land on the same node pool
    requiredDuringSchedulingIgnoredDuringExecution:
    - matchLabelKeys:
        - tenant
      topologyKey: node-pool
  podAntiAffinity:  # ensures only Pods from this tenant lands on the same node pool
    requiredDuringSchedulingIgnoredDuringExecution:
    - mismatchLabelKeys:
        - tenant
      labelSelector:
        matchExpressions:
        - key: tenant
          operator: Exists
      topologyKey: node-pool

上述 matchLabelKeysmismatchLabelKeys 將轉換為類似

kind: Pod
metadata:
  name: application-server
  labels:
    tenant: service-a
spec: 
  affinity:
    podAffinity:      # ensures the pods of this tenant land on the same node pool
      requiredDuringSchedulingIgnoredDuringExecution:
      - matchLabelKeys:
          - tenant
        topologyKey: node-pool
        labelSelector:
          matchExpressions:
          - key: tenant
            operator: In
            values:
            - service-a 
    podAntiAffinity:  # ensures only Pods from this tenant lands on the same node pool
      requiredDuringSchedulingIgnoredDuringExecution:
      - mismatchLabelKeys:
          - tenant
        labelSelector:
          matchExpressions:
          - key: tenant
            operator: Exists
          - key: tenant
            operator: NotIn
            values:
            - service-a
        topologyKey: node-pool

參與其中

這些功能由 Kubernetes SIG Scheduling 管理。

請加入我們並分享您的意見回饋。我們期待收到您的來信!

我該如何瞭解更多?