共用方式為


將資源從 Azure Kubernetes Fleet Manager 中樞叢集傳播至成員叢集

本文說明如何將資源從 Azure Kubernetes Fleet Manager (Kubernetes Fleet) 中樞叢集傳播到成員叢集。

必要條件

  • 如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶
  • 請閱讀資源傳播的概念概觀,以瞭解本文中使用的概念和術語。
  • 您需要具有中樞叢集和成員叢集的 Kubernetes Fleet 資源。 如果您沒有資源,請參閱 使用 Azure CLI 建立 Azure Kubernetes Fleet Manager 資源和加入成員叢集。
  • 成員叢集必須在中樞叢集中適當標示,以符合所需的選取準則。 範例標籤包括區域、環境、小組、可用性區域、節點可用性,或您想要的任何其他專案。
  • 您需要中樞叢集的 Kubernetes API 存取權。 如果您沒有存取權,請參閱 存取 Azure Kubernetes Fleet Manager 中樞叢集的 Kubernetes API。

使用 ClusterResourcePlacement API 將資源傳播至成員叢集

ClusterResourcePlacement API 物件會在中樞叢集中建立,並用於將資源傳播至成員叢集。 它會指定要傳播的資源,以及當您選取成員叢集時要使用的放置原則。 此範例示範如何使用 API 物件搭配PickAll放置原則,將命名空間傳播至成員叢集ClusterResourcePlacement

如需詳細資訊,請參閱 Kubernetes 資源放置從中樞叢集到成員叢集開放原始碼 Kubernetes Fleet 檔

  1. 使用 kubectl create namespace 命令,建立要放在成員叢集上的命名空間。 下列範例會建立名為 my-namespace 的命名空間:

    kubectl create namespace my-namespace
    
  2. 在中樞叢集中建立 ClusterResourcePlacement API 物件,以將命名空間傳播至成員叢集,並使用 kubectl apply -f 命令加以部署。 在下列範例中,建立名為 crp 的物件,ClusterResourcePlacement並使用my-namespace命名空間搭配PickAll放置原則,將命名空間傳播至所有成員叢集:

    kubectl apply -f - <<EOF
    apiVersion: placement.kubernetes-fleet.io/v1
    kind: ClusterResourcePlacement
    metadata:
      name: crp
    spec:
      resourceSelectors:
        - group: ""
          kind: Namespace
          version: v1          
          name: my-namespace
      policy:
        placementType: PickAll
    EOF
    
  3. 使用 kubectl get clusterresourceplacement 命令檢查資源傳播的進度。 下列範例會檢查名為 crpClusterResourcePlacement 物件的狀態:

    kubectl get clusterresourceplacement crp
    

    您的輸出訊息看起來應類似下列範例:

    NAME   GEN   SCHEDULED   SCHEDULEDGEN   APPLIED   APPLIEDGEN   AGE
    crp    2     True        2              True      2            10s
    
  4. 使用 kubectl describe crp 命令檢視對象的詳細crp數據。 下列範例描述名為 crpClusterResourcePlacement 物件:

    kubectl describe clusterresourceplacement crp
    

    您的輸出訊息看起來應類似下列範例:

    Name:         crp
    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:                  my-namespace
        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
    

清除資源

如果您不想再使用 ClusterResourcePlacement 物件,您可以使用 命令加以刪除 kubectl delete 。 下列範例會刪除名為 crpClusterResourcePlacement 物件:

kubectl delete clusterresourceplacement crp

若要深入了解資源傳播,請參閱下列資源: