使用 Azure Kubernetes Fleet Manager 資源配置,將叢集範圍的資源部署到多個叢集

適用於: ✔️ 使用中樞叢集的車隊管理器

Azure Kubernetes Fleet Manager 的資源配置功能允許您在叢集範圍內的多個叢集間,分發並同步叢集範圍的資源——包括命名空間及其子資源。 此功能簡化多叢集部署,確保基礎設施資源配置一致,減少手動負擔,並讓您能以單一真實來源大規模管理複雜工作負載。 本文將介紹如何使用 ClusterResourcePlacement 命名空間及其工作負載分配給成員叢集。

若要在命名空間內分配個別資源,請參閱 有關命名空間範圍資源配置的文件。

你可以透過 Azure 入口網站或 Azure CLI 完成本文中的步驟。

Note

你可以在 AKS 和支援 Azure Arc 的 Kubernetes 叢集中使用 Fleet Manager 的資源配置功能。

開始之前

將命名空間及其資源分配到成員叢集

使用包含 Deployment 和 Service 的簡單情境,將其部署到艦隊中的所有叢集。 在您的 Fleet Manager 樞紐叢集上建立命名空間,將工作負載加入其中,然後使用 ClusterResourcePlacement,透過 PickAll 放置原則,將該命名空間及其所有內容分發到所有成員叢集。

  1. 為您的訂用帳戶識別碼、資源群組和 Kubernetes 機群資源設定下列環境變數:

    export SUBSCRIPTION_ID=<subscription-id>
    export GROUP=<resource-group-name>
    export FLEET=<fleet-name>
    
  2. 請使用 [az account set][az-account-set] 命令設定預設 Azure 訂閱:

    az account set \
        --subscription ${SUBSCRIPTION_ID}
    
  3. 使用 [az fleet get-credentials][az-fleet-get-credentials] 指令取得 Kubernetes Fleet hub 叢集的 kubeconfig 檔案:

    az fleet get-credentials \
        --resource-group ${GROUP} \
        --name ${FLEET}
    

    你的輸出應該會像以下一樣。

    Merged "hub" as current context in /home/fleet/.kube/config
    

    Note

    如果您收到類型為InvalidHubOperation的錯誤,且訊息指出機群沒有中樞,請新增一個中樞叢集。 欲了解更多資訊,請參閱 升級集線器叢集類型。

  4. 在樞紐叢集建立命名空間。

    kubectl create namespace test-app
    
  5. 將以下 YAML 儲存為 test-workload.yaml。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
      namespace: test-app
    spec:
      selector:
        matchLabels:
          app: nginx
      replicas: 2
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: mcr.microsoft.com/azurelinux/base/nginx:1.28@sha256:3352a36cbcab4708883a3e77b64f64159e11e1aab358c010e1c4e465dbfb4f57
            ports:
            - containerPort: 80
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx-service
      namespace: test-app
    spec:
      selector:
        app: nginx
      ports:
      - protocol: TCP
        port: 80
        targetPort: 80
      type: LoadBalancer
    
  6. 使用 kubectl 將測試工作負載部署到 Fleet Manager 中樞叢集。

    kubectl apply -f test-workload.yaml
    
  7. 將以下 YAML 儲存為 crp-distribute-workload.yaml。

    apiVersion: placement.kubernetes-fleet.io/v1
    kind: ClusterResourcePlacement
    metadata:
      name: distribute-test-app
    spec:
      resourceSelectors:
        - group: ""
          kind: Namespace
          version: v1          
          name: test-app
      policy:
        placementType: PickAll
    
  8. 將配置清單套用到 Fleet Manager 的樞紐叢集。

    kubectl apply -f crp-distribute-workload.yaml
    

    Note

    套用資訊清單後,資源部署作業會立即開始。 採用隱性 RollingUpdate 策略。 想了解如何對部署有更多控制權,請參見 定義資源部署的部署策略。

  9. 查看資源配置的進度。

kubectl get clusterresourceplacement distribute-test-app

輸出看起來應類似下列範例:

NAME                   GEN   SCHEDULED   SCHEDULEDGEN   APPLIED   APPLIEDGEN   AGE
distribute-test-app    2     True        2              True      2            10s
  1. 查看實習詳情。
kubectl describe clusterresourceplacement distribute-test-app

輸出看起來應類似下列範例:

Name:         distribute-test-app
Namespace:    
Labels:       <none>
Annotations:  <none>
API Version:  placement.kubernetes-fleet.io/v1
Kind:         ClusterResourcePlacement
Metadata:
  Creation Timestamp:  2024-04-01T18:55:31Z
  Finalizers:
    kubernetes-fleet.io/crp-cleanup
    kubernetes-fleet.io/scheduler-cleanup
  Generation:        2
  Resource Version:  6949
  UID:               815b1d81-61ae-4fb1-a2b1-06794be3f986
Spec:
  Policy:
    Placement Type:  PickAll
  Resource Selectors:
    Group:                 
    Kind:                  Namespace
    Name:                  test-app
    Version:               v1
  Revision History Limit:  10
  Strategy:
    Type:  RollingUpdate
Status:
  Conditions:
    Last Transition Time:   2024-04-01T18:55:31Z
    Message:                found all the clusters needed as specified by the scheduling policy
    Observed Generation:    2
    Reason:                 SchedulingPolicyFulfilled
    Status:                 True
    Type:                   ClusterResourcePlacementScheduled
    Last Transition Time:   2024-04-01T18:55:36Z
    Message:                All 3 cluster(s) are synchronized to the latest resources on the hub cluster
    Observed Generation:    2
    Reason:                 SynchronizeSucceeded
    Status:                 True
    Type:                   ClusterResourcePlacementSynchronized
    Last Transition Time:   2024-04-01T18:55:36Z
    Message:                Successfully applied resources to 3 member clusters
    Observed Generation:    2
    Reason:                 ApplySucceeded
    Status:                 True
    Type:                   ClusterResourcePlacementApplied
  Observed Resource Index:  0
  Placement Statuses:
    Cluster Name:  membercluster1
    Conditions:
      Last Transition Time:  2024-04-01T18:55:31Z
      Message:               Successfully scheduled resources for placement in membercluster1 (affinity score: 0, topology spread score: 0): picked by scheduling policy
      Observed Generation:   2
      Reason:                ScheduleSucceeded
      Status:                True
      Type:                  ResourceScheduled
      Last Transition Time:  2024-04-01T18:55:36Z
      Message:               Successfully Synchronized work(s) for placement
      Observed Generation:   2
      Reason:                WorkSynchronizeSucceeded
      Status:                True
      Type:                  WorkSynchronized
      Last Transition Time:  2024-04-01T18:55:36Z
      Message:               Successfully applied resources
      Observed Generation:   2
      Reason:                ApplySucceeded
      Status:                True
      Type:                  ResourceApplied
    Cluster Name:            membercluster2
    Conditions:
      Last Transition Time:  2024-04-01T18:55:31Z
      Message:               Successfully scheduled resources for placement in membercluster2 (affinity score: 0, topology spread score: 0): picked by scheduling policy
      Observed Generation:   2
      Reason:                ScheduleSucceeded
      Status:                True
      Type:                  ResourceScheduled
      Last Transition Time:  2024-04-01T18:55:36Z
      Message:               Successfully Synchronized work(s) for placement
      Observed Generation:   2
      Reason:                WorkSynchronizeSucceeded
      Status:                True
      Type:                  WorkSynchronized
      Last Transition Time:  2024-04-01T18:55:36Z
      Message:               Successfully applied resources
      Observed Generation:   2
      Reason:                ApplySucceeded
      Status:                True
      Type:                  ResourceApplied
    Cluster Name:            membercluster3
    Conditions:
      Last Transition Time:  2024-04-01T18:55:31Z
      Message:               Successfully scheduled resources for placement in membercluster3 (affinity score: 0, topology spread score: 0): picked by scheduling policy
      Observed Generation:   2
      Reason:                ScheduleSucceeded
      Status:                True
      Type:                  ResourceScheduled
      Last Transition Time:  2024-04-01T18:55:36Z
      Message:               Successfully Synchronized work(s) for placement
      Observed Generation:   2
      Reason:                WorkSynchronizeSucceeded
      Status:                True
      Type:                  WorkSynchronized
      Last Transition Time:  2024-04-01T18:55:36Z
      Message:               Successfully applied resources
      Observed Generation:   2
      Reason:                ApplySucceeded
      Status:                True
      Type:                  ResourceApplied
  Selected Resources:
    Kind:     Namespace
    Name:     my-namespace
    Version:  v1
Events:
  Type    Reason                     Age   From                                   Message
  ----    ------                     ----  ----                                   -------
  Normal  PlacementScheduleSuccess   108s  cluster-resource-placement-controller  Successfully scheduled the placement
  Normal  PlacementSyncSuccess       103s  cluster-resource-placement-controller  Successfully synchronized the placement
  Normal  PlacementRolloutCompleted  103s  cluster-resource-placement-controller  Resources have been applied to the selected clusters
  1. 在 Azure 入口網站中,前往您的艦隊管理器。

  2. 在服務選單中,在 艦隊資源下,選擇 命名空間>+建立。

    Note

    如果你看不到 艦隊資源,代表你的艦隊管理員沒有集聯區叢集。 如需進一步了解如何新增一個叢集,請參閱 升級集線器叢集類型。

  3. 在選單中選擇 命名空間,輸入 名稱,然後選擇 建立。

    Azure 入口網站的截圖顯示一個名為 test-app 的命名空間正在 Fleet Manager 樞紐叢集上建立。

    幾分鐘後,頁面重新整理,命名空間會出現在 Fleet Manager 樞紐叢集的命名空間列表中。 現在它已準備好託管你想要分送到各成員叢集的任何資源。

  4. 在命名空間清單頂端,選取 + 建立>套用 YAML,並使用以下範例。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
      namespace: test-app
    spec:
      selector:
        matchLabels:
          app: nginx
      replicas: 2
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: mcr.microsoft.com/azurelinux/base/nginx:1.28@sha256:3352a36cbcab4708883a3e77b64f64159e11e1aab358c010e1c4e465dbfb4f57
            ports:
            - containerPort: 80
    
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx-service
      namespace: test-app
    spec:
      selector:
        app: nginx
      ports:
      - protocol: TCP
        port: 80
        targetPort: 80
      type: LoadBalancer
    

    複製並貼上下列範例,然後依照下圖逐一套用。

    Azure 入口網站的螢幕截圖,顯示已填入可套用至機群管理員中樞叢集之部署的「使用 YAML 套用」對話方塊。

    命名空間及其工作負載現在已準備好分發到成員叢集。 部署和服務並未在機群管理員中樞叢集上排程。

  5. 在服務選單中,在 艦隊資源下,選擇資源 配置>+創建。

  6. 在 [基本] 索引標籤上,設定下列選項:

    • 在 實習詳情中,輸入實習名稱。

    • 在 資源細節中,將 範圍 保留為 叢集範圍, 並輸入群組版本類型(GVK)及要分發的資源名稱。 在這個範例中,請使用以下數值。

      group: ""
      kind: Namespace
      version: v1          
      name: test-app
      
    • 在 成員叢集選擇 中,於 放置類型 選擇 所有叢集。

    • 對於 Rollout,請保留已選取的「循序更新」策略。

    Azure 入口網站的截圖,顯示已完成的 Basics 標籤,用於一個名為 test-app 的命名空間的叢集範圍資源配置。

  7. 選擇 「下一頁 」以查看產生的 ClusterResourcePlacement 清單。 如果需要,你可以修改資訊清單,使用 驗證(試跑) 選項進行驗證,或選擇 檢閱 + 建立 以進入最後確認。

    Azure 入口網站的螢幕截圖,其中顯示檢閱 YAML 索引標籤,且有個針對名為 test-app 之命名空間的叢集範圍資源放置的已通過試執行確認。

  8. 選擇 建立 即可立即開始分發命名空間。

    Azure 入口網站的螢幕截圖,其中顯示針對名為 test-app 之命名空間叢集範圍資源放置的檢閱與建立索引標籤。

    Note

    當你使用所選 RollingUpdate 策略套用資源放置時,資源展開會立即開始。 想了解如何對部署進行額外控制,請參見 「定義資源配置的部署策略」。

  9. 頁面會重新整理, 資源擺放 清單會顯示新建立的位置。 它顯示所選叢集數量,以及車隊排程器是否能執行該配置政策。

    Azure 入口網站的螢幕擷取畫面,顯示資源放置清單,其中只有一個名為 distribute-test-app 的資源放置。

  10. 請在列表中選取投放名稱,以查看該投放的推出狀態。 選擇個別叢集以查看該叢集的資源部署。

    Azure 入口網站的截圖,顯示名為 distribute-test-app 的資源配置的詳細部署資訊。

在所選的 Kubernetes 叢集中,你會看到命名空間、部署,以及已部署且運作中的服務。

刪除該版位以移除資源

當你刪除 Fleet Manager 集線器叢集上的資源配置時,就是從選取的成員叢集移除分散資源。

若要移除分散式資源而不刪除放置,請使用PickFixed搭配空的clusterNames陣列。 欲了解更多資訊,請參閱 PickFixed 放置類型設定。

kubectl delete clusterresourceplacement distribute-test-app
  1. 在服務功能表上的 [艦隊資源] 底下,選取 [資源位置]。

  2. 在資源版位清單中,選取該版位,然後選取 刪除。

  3. 在 刪除時,請確認你選擇了正確的位置。 當您準備好時,請選取 [確認刪除]>[刪除]。

    Azure 入口網站的截圖顯示一個名為 distribute-test-app 的資源放置正在被刪除。

  4. 頁面會重新整理,且該位置不再出現在列表中。 資源會從成員叢集中移除。

欲了解更多資源配置資訊,請參閱以下資源: