共用方式為


快速入門:將資源從 Azure Kubernetes 機群管理員 (Fleet) 中樞叢集傳播到成員叢集

在本快速入門中,您將了解如何將資源從 Azure Kubernetes 機群管理員 (Fleet) 中樞叢集傳播到成員叢集。

必要條件

如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶

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

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

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

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

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

    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

下一步

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