你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Azure Kubernetes Fleet Manager ResourcePlacement 跨多个群集部署命名空间范围内的资源(预览版)

本文介绍如何使用 Azure Kubernetes 机队管理器 ResourcePlacement 跨机群中的群集部署命名空间范围内的资源。

重要

Azure Kubernetes 舰队管理器预览功能可以通过自助服务方式选择性启用。 预览版按“现状”和“视供应情况”提供,它们不包括在服务级别协议和有限保证范围内。 客户支持部门会尽力为 Azure Kubernetes 舰队管理器预览功能提供部分支持。 因此,这些功能并不适合用于生产。

先决条件

跨成员群集建立命名空间

在可用于 ResourcePlacement 部署命名空间范围的资源之前,目标命名空间必须存在于成员群集上。 此示例演示如何在中心群集上创建命名空间并将其传播到使用 ClusterResourcePlacement的成员群集。

注释

以下示例使用 placement.kubernetes-fleet.io/v1beta1 API 版本。 该 selectionScope: NamespaceOnly 字段是 v1beta1 中提供的预览功能,在稳定的 v1 API 中不可用。

  1. 在中心群集上创建命名空间:

    kubectl create namespace my-app
    
  2. 创建一个 ClusterResourcePlacement 对象以将命名空间传播到所有成员群集。 将以下 YAML 保存到名为 namespace-crp.yaml 的文件:

    apiVersion: placement.kubernetes-fleet.io/v1beta1
    kind: ClusterResourcePlacement
    metadata:
      name: my-app-namespace
    spec:
      resourceSelectors:
        - group: ""
          kind: Namespace
          name: my-app
          version: v1
          selectionScope: NamespaceOnly
      policy:
        placementType: PickAll
    
  3. ClusterResourcePlacement 应用于中心集群。

    kubectl apply -f namespace-crp.yaml
    
  4. 验证命名空间是否已成功传播:

    kubectl get clusterresourceplacement my-app-namespace
    

    输出应类似于以下示例:

    NAME                GEN   SCHEDULED   SCHEDULED-GEN   AVAILABLE   AVAILABLE-GEN   AGE
    my-app-namespace    1     True        1               True        1               15s
    

使用 ResourcePlacement 来放置命名空间范围的资源

ResourcePlacement 对象在中心群集的命名空间中创建,用于将特定的命名空间范围资源传播到成员群集。 此示例演示如何使用 ResourcePlacement 具有放置策略的对象 PickFixed 将 ConfigMap 传播到特定成员群集。

有关详细信息,请参阅 通过 Azure Kubernetes Fleet Manager ResourcePlacement 在命名空间范围内进行资源放置

  1. 在中心群集的命名空间中创建 ConfigMaps。 这些 ConfigMap 将传播到所选成员群集:

    kubectl create configmap app-config \
      --from-literal=environment=production \
      --from-literal=log-level=info \
      -n my-app
    
    kubectl create configmap feature-flags \
      --from-literal=new-ui=enabled \
      --from-literal=api-v2=disabled \
      -n my-app
    
  2. 创建一个 ResourcePlacement 对象以传播 ConfigMaps。 将以下 YAML 保存到名为 app-configs-rp.yaml 的文件:

    apiVersion: placement.kubernetes-fleet.io/v1beta1
    kind: ResourcePlacement
    metadata:
      name: app-configs
      namespace: my-app
    spec:
      resourceSelectors:
        - group: ""
          kind: ConfigMap
          version: v1
          name: app-config
        - group: ""
          kind: ConfigMap
          version: v1
          name: feature-flags
      policy:
        placementType: PickFixed
        clusterNames:
          - membercluster1
          - membercluster2
    

    注释

    membercluster1membercluster2替换为您成员群集的实际名称。 可以使用kubectl get memberclusters列出可用的成员群集。

  3. 将应用 ResourcePlacement 到中心群集:

    kubectl apply -f app-configs-rp.yaml
    
  4. 检查资源传播的进度:

    kubectl get resourceplacement app-configs -n my-app
    

    输出应类似于以下示例:

    NAME          GEN   SCHEDULED   SCHEDULED-GEN   AVAILABLE   AVAILABLE-GEN   AGE
    app-configs   1     True        1               True        1               20s
    
  5. 查看放置对象的详细信息:

    kubectl describe resourceplacement app-configs -n my-app
    

    输出应类似于以下示例:

    Name:         app-configs
    Namespace:    my-app
    Labels:       <none>
    Annotations:  <none>
    API Version:  placement.kubernetes-fleet.io/v1beta1
    Kind:         ResourcePlacement
    Metadata:
      Creation Timestamp:  2025-11-13T22:08:12Z
      Finalizers:
        kubernetes-fleet.io/crp-cleanup
        kubernetes-fleet.io/scheduler-cleanup
      Generation:        1
      Resource Version:  12345
      UID:               cec941f1-e48a-4045-b5dd-188bfc1a830f
    Spec:
      Policy:
        Cluster Names:
          membercluster1
          membercluster2
        Placement Type:  PickFixed
      Resource Selectors:
        Group:                 
        Kind:                  ConfigMap
        Name:                  app-config
        Version:               v1
        Group:                 
        Kind:                  ConfigMap
        Name:                  feature-flags
        Version:               v1
      Revision History Limit:  10
      Strategy:
        Type:  RollingUpdate
    Status:
      Conditions:
        Last Transition Time:   2025-11-13T22:08:12Z
        Message:                found all cluster needed as specified by the scheduling policy, found 2 cluster(s)
        Observed Generation:    1
        Reason:                 SchedulingPolicyFulfilled
        Status:                 True
        Type:                   ResourcePlacementScheduled
        Last Transition Time:   2025-11-13T22:08:12Z
        Message:                All 2 cluster(s) start rolling out the latest resource
        Observed Generation:    1
        Reason:                 RolloutStarted
        Status:                 True
        Type:                   ResourcePlacementRolloutStarted
        Last Transition Time:   2025-11-13T22:08:13Z
        Message:                No override rules are configured for the selected resources
        Observed Generation:    1
        Reason:                 NoOverrideSpecified
        Status:                 True
        Type:                   ResourcePlacementOverridden
        Last Transition Time:   2025-11-13T22:08:13Z
        Message:                Works(s) are succcesfully created or updated in 2 target cluster(s)' namespaces
        Observed Generation:    1
        Reason:                 WorkSynchronized
        Status:                 True
        Type:                   ResourcePlacementWorkSynchronized
        Last Transition Time:   2025-11-13T22:08:13Z
        Message:                The selected resources are successfully applied to 2 cluster(s)
        Observed Generation:    1
        Reason:                 ApplySucceeded
        Status:                 True
        Type:                   ResourcePlacementApplied
        Last Transition Time:   2025-11-13T22:08:13Z
        Message:                The selected resources in 2 cluster(s) are available now
        Observed Generation:    1
        Reason:                 ResourceAvailable
        Status:                 True
        Type:                   ResourcePlacementAvailable
      Observed Resource Index:  0
      Placement Statuses:
        Cluster Name:  membercluster1
        Conditions:
          Last Transition Time:   2025-11-13T22:08:12Z
          Message:                Successfully scheduled resources for placement in "membercluster1": picked by scheduling policy
          Observed Generation:    1
          Reason:                 Scheduled
          Status:                 True
          Type:                   Scheduled
          Last Transition Time:   2025-11-13T22:08:12Z
          Message:                Detected the new changes on the resources and started the rollout process
          Observed Generation:    1
          Reason:                 RolloutStarted
          Status:                 True
          Type:                   RolloutStarted
          Last Transition Time:   2025-11-13T22:08:13Z
          Message:                No override rules are configured for the selected resources
          Observed Generation:    1
          Reason:                 NoOverrideSpecified
          Status:                 True
          Type:                   Overridden
          Last Transition Time:   2025-11-13T22:08:13Z
          Message:                All of the works are synchronized to the latest
          Observed Generation:    1
          Reason:                 AllWorkSynced
          Status:                 True
          Type:                   WorkSynchronized
          Last Transition Time:   2025-11-13T22:08:13Z
          Message:                All corresponding work objects are applied
          Observed Generation:    1
          Reason:                 AllWorkHaveBeenApplied
          Status:                 True
          Type:                   Applied
          Last Transition Time:   2025-11-13T22:08:13Z
          Message:                All corresponding work objects are available
          Observed Generation:    1
          Reason:                 AllWorkAreAvailable
          Status:                 True
          Type:                   Available
        Observed Resource Index:  0
        Cluster Name:             membercluster2
        Conditions:
          Last Transition Time:   2025-11-13T22:08:12Z
          Message:                Successfully scheduled resources for placement in "membercluster2": picked by scheduling policy
          Observed Generation:    1
          Reason:                 Scheduled
          Status:                 True
          Type:                   Scheduled
          Last Transition Time:   2025-11-13T22:08:12Z
          Message:                Detected the new changes on the resources and started the rollout process
          Observed Generation:    1
          Reason:                 RolloutStarted
          Status:                 True
          Type:                   RolloutStarted
          Last Transition Time:   2025-11-13T22:08:13Z
          Message:                No override rules are configured for the selected resources
          Observed Generation:    1
          Reason:                 NoOverrideSpecified
          Status:                 True
          Type:                   Overridden
          Last Transition Time:   2025-11-13T22:08:13Z
          Message:                All of the works are synchronized to the latest
          Observed Generation:    1
          Reason:                 AllWorkSynced
          Status:                 True
          Type:                   WorkSynchronized
          Last Transition Time:   2025-11-13T22:08:13Z
          Message:                All corresponding work objects are applied
          Observed Generation:    1
          Reason:                 AllWorkHaveBeenApplied
          Status:                 True
          Type:                   Applied
          Last Transition Time:   2025-11-13T22:08:13Z
          Message:                All corresponding work objects are available
          Observed Generation:    1
          Reason:                 AllWorkAreAvailable
          Status:                 True
          Type:                   Available
        Observed Resource Index:  0
      Selected Resources:
        Kind:       ConfigMap
        Name:       app-config
        Namespace:  my-app
        Version:    v1
        Kind:       ConfigMap
        Name:       feature-flags
        Namespace:  my-app
        Version:    v1
    Events:
      Type    Reason                     Age   From                            Message
      ----    ------                     ----  ----                            -------
      Normal  PlacementRolloutStarted       37s   placement-controller  Started rolling out the latest resources
      Normal  PlacementOverriddenSucceeded  36s   placement-controller  Placement has been successfully overridden
      Normal  PlacementWorkSynchronized     36s   placement-controller  Work(s) have been created or updated successfully for the selected cluster(s)
      Normal  PlacementApplied              36s   placement-controller  Resources have been applied to the selected cluster(s)
      Normal  PlacementAvailable            36s   placement-controller  Resources are available on the selected cluster(s)
      Normal  PlacementRolloutCompleted     36s   placement-controller  Placement has finished the rollout process and reached the desired status
    

验证成员群集上的资源

可以验证 ConfigMap 是否已成功传播到成员群集。

  1. 获取其中一个成员群集的凭据:

    az aks get-credentials --resource-group <resource-group> --name membercluster1
    
  2. 验证命名空间是否存在:

    kubectl get namespace my-app
    
  3. 验证命名空间中是否存在 ConfigMap:

    kubectl get configmap -n my-app
    

    输出应同时显示两个 ConfigMap:

    NAME             DATA   AGE
    app-config       2      2m
    feature-flags    2      2m
    
  4. 查看 ConfigMap 的内容:

    kubectl describe configmap app-config -n my-app
    

清理资源

如果不再想要使用这些 ResourcePlacement 对象,可以使用以下命令将其删除 kubectl delete

kubectl delete resourceplacement app-configs -n my-app

若要删除命名空间 ClusterResourcePlacement

kubectl delete clusterresourceplacement my-app-namespace

若要从中心群集中删除命名空间及其中的所有资源,请执行以下作:

kubectl delete namespace my-app

若要了解有关命名空间范围资源放置的详细信息,请参阅以下资源: