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

在已启用 Azure Arc 的 Kubernetes 上创建和管理自定义位置

“自定义位置”功能提供了一种方法来将已启用 Azure Arc 的 Kubernetes 群集配置为用于部署 Azure 产品/服务实例的目标位置。 可部署在自定义位置之上的 Azure 产品/服务的示例包括数据库(例如,Azure Arc 启用的 SQL 托管实例和已启用 Azure Arc 的 PostgreSQL 服务器)或应用程序实例(例如,应用服务、Functions、事件网格、逻辑应用和 API 管理)。

自定义位置与已启用 Azure Arc 的 Kubernetes 群集中的命名空间之间具有一对一映射关系。 将 Azure 资源与 Azure 基于角色的访问控制 (Azure RBAC) 结合使用的自定义位置可用于向应用程序开发人员或数据库管理员授予细化权限,使他们能够在多租户环境中在已启用 Arc 的 Kubernetes 群集顶层部署资源,例如数据库或应用程序实例。

在本文中,你将了解如何在已启用 Arc 的 Kubernetes 群集上启用自定义位置,以及如何创建自定义位置。

先决条件

  • 安装或升级到 Azure CLI 的最新版本。

  • 安装以下 Azure CLI 扩展的最新版本:

    • connectedk8s

    • k8s-extension

    • customlocation

      az extension add --name connectedk8s
      az extension add --name k8s-extension
      az extension add --name customlocation
      

      如果已安装 connectedk8sk8s-extensioncustomlocation 扩展,则可以通过使用以下命令将其更新到最新版本

      az extension update --name connectedk8s
      az extension update --name k8s-extension
      az extension update --name customlocation
      
  • 验证 Microsoft.ExtendedLocation 的提供程序注册是否已完成。

    1. 输入以下命令:

      az provider register --namespace Microsoft.ExtendedLocation
      
    2. 监视注册过程。 注册可能最多需要 10 分钟。

      az provider show -n Microsoft.ExtendedLocation -o table
      

      一经注册,RegistrationState 状态将具有 Registered 值。

  • 验证你是否具有现有的已启用 Azure Arc 的 Kubernetes 连接的群集,并将你的代理升级到最新版本。 确认你用来运行本文所述命令的计算机具有指向此群集的 kubeconfig 文件。

在自己的群集上启用自定义位置

提示

“自定义位置”功能依赖于群集连接功能。 必须在群集中启用这两项功能才能使自定义位置正常工作。

如果你以 Microsoft Entra 用户的身份登录到 Azure CLI,请使用以下命令:

az connectedk8s enable-features -n <clusterName> -g <resourceGroupName> --features cluster-connect custom-locations

如果使用服务主体登录到 Azure CLI 时运行上述命令,可能会看到以下警告:

Unable to fetch oid of 'custom-locations' app. Proceeding without enabling the feature. Insufficient privileges to complete the operation.

这是因为服务主体无权获取 Azure Arc 服务使用的应用程序相关信息。 若要避免此错误,请完成以下步骤:

  1. 使用用户帐户登录到 Azure CLI。 使用以下命令提取 Azure Arc 服务使用的 Microsoft Entra 应用程序的 objectIdid

    az ad sp show --id bc313c14-388c-4e7d-a58e-70017303ee3b --query id -o tsv
    
  2. 使用服务主体登录到 Azure CLI。 使用上述步骤中的 <objectId>id 值在群集上启用自定义位置:

    az connectedk8s enable-features -n <cluster-name> -g <resource-group-name> --custom-locations-oid <objectId/id> --features cluster-connect custom-locations
    

创建自定义位置

  1. 在群集上部署要安装的 Azure 服务实例的 Azure 服务群集扩展:

  2. 获取已启用 Azure Arc 的 Kubernetes 群集的 Azure 资源管理器标识符,此标识符在后面的步骤中称为 connectedClusterId

    az connectedk8s show -n <clusterName> -g <resourceGroupName>  --query id -o tsv
    
  3. 获取你部署到已启用 Azure Arc 的 Kubernetes 群集上的群集扩展的 Azure 资源管理器标识符,此标识符在后面的步骤中称为 extensionId

    az k8s-extension show --name <extensionInstanceName> --cluster-type connectedClusters -c <clusterName> -g <resourceGroupName>  --query id -o tsv
    
  4. 通过引用已启用 Azure Arc 的 Kubernetes 群集和扩展来创建自定义位置:

    az customlocation create -n <customLocationName> -g <resourceGroupName> --namespace <name of namespace> --host-resource-id <connectedClusterId> --cluster-extension-ids <extensionId> 
    
    • 所需参数:

      参数名称 说明
      --name, --n 自定义位置的名称。
      --resource-group, --g 自定义位置的资源组。
      --namespace 群集中绑定到所创建的自定义位置的命名空间。
      --host-resource-id 已启用 Azure Arc 的 Kubernetes 群集(连接的群集)的 Azure 资源管理器标识符。
      --cluster-extension-ids 在连接的群集上安装的群集扩展实例的 Azure 资源管理器标识符。 对于多个扩展,请提供群集扩展 ID 的空格分隔列表
    • 可选参数:

      参数名称 说明
      --location, --l Azure 资源管理器资源在 Azure 中的自定义位置。 如果未指定,则使用连接的群集的位置。
      --tags 空格分隔的标记列表,格式为 key[=value]。 使用 '' 清除现有标记。
      --kubeconfig 群集的管理员 kubeconfig

显示自定义位置的详细信息

若要显示自定义位置的详细信息,请使用以下命令:

az customlocation show -n <customLocationName> -g <resourceGroupName> 

列出自定义位置

若要列出资源组中的所有自定义位置,请使用以下命令:

az customlocation list -g <resourceGroupName> 

更新自定义位置

使用 update 命令为 --tags 添加新值,或者将新的 --cluster-extension-ids 关联到自定义位置,同时保留标记的现有值和已关联的群集扩展。

az customlocation update -n <customLocationName> -g <resourceGroupName> --namespace <name of namespace> --host-resource-id <connectedClusterId> --cluster-extension-ids <extensionIds> 

修补自定义位置

使用 patch 命令替换 --cluster-extension-ids--tags 的现有值。 以前的值不会保留。

az customlocation patch -n <customLocationName> -g <resourceGroupName> --namespace <name of namespace> --host-resource-id <connectedClusterId> --cluster-extension-ids <extensionIds> 

删除自定义位置

若要删除自定义位置,请使用以下命令:

az customlocation delete -n <customLocationName> -g <resourceGroupName> 

故障排除

如果自定义位置创建失败并出现错误 Unknown proxy error occurred,请修改网络策略以允许 azure-arc 命名空间中的 Pod 到 Pod 内部通信。 确保还要将 azure-arc 命名空间添加为你配置的策略的无代理排除列表的一部分。

后续步骤