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

本文說明如何為叢集上執行的 Config Sync 系統 Pod 設定節點位置。設定節點位置有許多好處,例如基於安全考量,將 Config Sync 系統 Pod 隔離至特定節點,或將 Config Sync 排定在較便宜的節點上,以節省成本。

關於 MutatingAdmissionPolicy

MutatingAdmissionPolicy 是 Kubernetes 1.36 以上版本提供的功能,可讓您自訂行為,在有人變更 Kubernetes API 時觸發。

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

設定 nodeSelectors

以下範例說明如何使用 MutatingAdmissionPolicy,為每個 Config Sync 系統命名空間中執行的每個 Pod 自動新增 nodeSelector,並加上 configmanagement.gke.io/system: "true" 標籤。Config Sync 系統命名空間包括 config-management-system、config-management-monitoring 和 resource-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 的節點。