自訂 Config Sync 系統 Pod 的節點放置位置

本文說明如何為叢集上執行的 Config Sync 系統 Pod 設定節點放置位置。設定節點放置位置很有用,例如為了安全起見,將 Config Sync 系統 Pod 隔離到特定節點,或是為了節省成本,在較便宜的節點上排程 Config Sync。

關於 MutatingAdmissionPolicy

MutatingAdmissionPolicy 是 Kubernetes 功能,適用於 Kubernetes 1.36 以上版本,可讓您在有人對 Kubernetes API 進行變更時,自訂相關行為。

在 Config Sync 的環境中,您可以使用 MutatingAdmissionPolicy,將節點放置參數新增至在 Config Sync 系統命名空間中執行的 Pod。這些參數包括 Pod 規格欄位,例如 nodeSelector 和親和性。本文提供如何搭配使用 MutatingAdmissionPolicy 與 Config Sync 的範例,您可以在 Config Sync 開放原始碼存放區中找到更多範例。

設定 nodeSelectors

以下範例說明如何使用 MutatingAdmissionPolicy,為每個在 Config Sync 系統命名空間中執行的 Pod 自動新增 nodeSelector,並加上 configmanagement.gke.io/system: "true" 標籤。Config Sync 系統命名空間包括 config-management-systemconfig-management-monitoringresource-group-system

apiVersion: admissionregistration.k8s.io/v1beta1
kind: MutatingAdmissionPolicy
metadata:
  name: "my-pod-node-selector"
spec:
  matchConstraints:
    resourceRules:
    - apiGroups:   [""]
      apiVersions: ["v1"]
      operations:  ["CREATE", "UPDATE"]
      resources:   ["pods"]
  failurePolicy: Fail
  reinvocationPolicy: IfNeeded
  mutations:
  # Basic example of adding nodeSelector: https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/
  # Similar mutations can be applied for affinities, tolerations, and so on
  - patchType: "JSONPatch"
    jsonPatch:
      expression: >
        [
          JSONPatch{
            op: "add", path: "/spec/nodeSelector",
            value: {
              "LABEL_KEY": "LABEL_VALUE",
            }
          }
        ]
---
apiVersion: admissionregistration.k8s.io/v1beta1
kind: MutatingAdmissionPolicyBinding
metadata:
  name: "configsync-node-selector"
spec:
  policyName: "my-pod-node-selector"
  matchResources:
    namespaceSelector:
      matchLabels:
        # This namespace selector only selects Config Sync system namespaces
        configmanagement.gke.io/system: "true"

更改下列內容:

  • LABEL_KEY:節點選取器使用的標籤鍵。
  • LABEL_VALUE:節點選取器使用的標籤值。

將這項設定套用至叢集後,所有 Config Sync 系統 Pod 都會排定至標籤為 LABEL_KEY=LABEL_VALUE 的節點。