您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

向启用了 Azure Arc 的服务器应用库存标记Apply inventory tagging to Azure Arc enabled servers

本文提供有关如何使用启用了 Azure Arc 的服务器来跨混合多云和本地环境提供服务器清单管理功能的指南。This article provides guidance on how to use Azure Arc enabled servers to provide server inventory management capabilities across hybrid multicloud and on-premises environments.

使用启用了 azure Arc 的服务器可以管理在公司网络或其他云提供商的 Azure 外部托管的 Windows 和 Linux 计算机。Azure Arc enabled servers enable you to manage your Windows and Linux machines hosted outside of Azure on your corporate network or other cloud providers. 这类似于管理 Azure 中的本地虚拟机的方式。This is similar to how you manage native virtual machines in Azure. 当混合计算机连接到 Azure 时,它将成为一台联网计算机,被视为 Azure 中的资源。When a hybrid machine is connected to Azure, it becomes a connected machine and is treated as a resource in Azure. 每台连接的计算机都有一个资源 ID,作为订阅内的资源组的一部分进行管理,并从标准 Azure 构造(如 Azure 策略和应用标记)中受益。Each connected machine has a resource ID, is managed as part of a resource group inside a subscription, and benefits from standard Azure constructs such as Azure Policy and applying tags. 使用 Azure 作为管理引擎轻松组织和管理服务器清单的能力极大地降低了管理复杂性,并为混合环境和多云环境提供了一致的策略。The ability to easily organize and manage server inventory using Azure as a management engine greatly reduces administrative complexity and provides a consistent strategy for hybrid and multicloud environments.

下面的过程使用 资源 Graph 资源管理器Azure CLI 来演示如何从 Azure 中的单个窗格在多个云之间标记和查询服务器清单。The following procedures use Resource Graph Explorer and Azure CLI to demonstrate how to tag and query server inventory across multiple clouds from a single pane of glass in Azure.

重要

本文中的过程假定你已部署了 Vm 或运行本地或其他云的服务器,并且你已将其连接到 Azure Arc。如果你尚未这样做,以下信息可以帮助你自动执行此过程。The procedures in this article assumes you've already deployed VMs, or servers that are running on-premises or on other clouds, and you have connected them to Azure Arc. If you haven't, the following information can help you automate this.

先决条件Prerequisites

  1. 克隆 Azure Arc Jumpstart 存储库。Clone the Azure Arc Jumpstart repository.

    git clone https://github.com/microsoft/azure_arc
    
  2. 安装或更新到版本2.7 或更高版本 Azure CLIInstall or update Azure CLI to version 2.7 or later. 使用以下命令来检查当前安装的版本。Use the following command to check the current installed version.

    az --version
    

验证你的 Azure Arc 连接服务器已准备好进行标记Verify that your Azure Arc connected servers are ready for tagging

使用资源 Graph 资源管理器来查询和查看 Azure 中的资源。Use Resource Graph Explorer to query and view resources in Azure.

  1. 在 Azure 门户中的顶部搜索栏中输入 资源 Graph 资源管理器 ,并将其选中。Enter Resource Graph Explorer in the top search bar in the Azure portal and select it.

    Azure 门户中的资源关系图资源管理器的屏幕截图。

  2. 在查询窗口中,输入以下查询,然后选择 " 运行查询":In the query window, enter the following query, and then select Run Query:

    Resources
    | where type =~ 'Microsoft.HybridCompute/machines'
    
  3. 如果已正确创建启用了 Azure Arc 的服务器,它们将列在资源 Graph 资源管理器的结果窗格中。If you have correctly created Azure Arc enabled servers, they are listed in the results pane of Resource Graph Explorer. 你还可以从 Azure 门户查看启用了 Azure Arc 的服务。You can also view the Azure Arc enabled serves from the Azure portal.

    资源 Graph 资源管理器查询的屏幕截图。

    Azure 门户中启用了 Azure Arc 的服务器的详细信息屏幕截图。

创建基本 Azure 标记分类Create a basic Azure tag taxonomy

打开 Azure CLI,并运行以下命令,以创建一个基本分类结构,使你可以轻松地查询和报告托管服务器资源的位置 (是在 Azure、AWS、GCP 还是本地) 。Open Azure CLI and run the following commands to create a basic taxonomy structure that lets you easily query and report on where your server resources are hosted (whether in Azure, AWS, GCP, or on-premises). 有关生成标记分类的更多指导,请查看 资源命名和标记决策指南For more guidance on building out a tag taxonomy please review the resource naming and tagging decision guide.

az tag create --name "Hosting Platform"
az tag add-value --name "Hosting Platform" --value "Azure"
az tag add-value --name "Hosting Platform" --value "AWS"
az tag add-value --name "Hosting Platform" --value "GCP"
az tag add-value --name "Hosting Platform" --value "On-premises"

"Az tag create" 命令输出的屏幕截图。

标记 Azure Arc 资源Tag your Azure Arc resources

创建基本分类结构后,将标记应用于启用了 Azure Arc 的服务器资源。After you've created a basic taxonomy structure, apply tags to the Azure Arc enabled server resources. 下面的过程演示如何在 AWS 和 GCP 中标记资源。The following procedure demonstrates tagging resources in both AWS and GCP. 如果你只有其中一个提供程序中的资源,则可以跳转到 AWS 或 GCP 的相应部分。If you only have resources in one of these providers, you can skip to the appropriate section for AWS or GCP.

标记 Azure Arc 连接的 AWS Ubuntu EC2 实例Tag the Azure Arc connected AWS Ubuntu EC2 instance

在 CLI 中运行以下命令,将标记应用 Hosting Platform : AWS 到已启用 AWS Azure Arc 的服务器。In CLI, run the following commands to apply the Hosting Platform : AWS tag to your AWS Azure Arc enabled servers.

备注

如果使用 Azure 教程中所述的方法以外的方法连接 AWS EC2 实例,则需要调整和的值, awsResourceGroup awsMachineName 以匹配特定于环境的值。If you connected your AWS EC2 instances using a method other than the one described in the Azure tutorial, then you need to adjust the values for awsResourceGroup and awsMachineName to match values specific to your environment.

export awsResourceGroup="arc-aws-demo"
export awsMachineName="arc-aws-demo"
export awsMachineResourceId="$(az resource show --resource-group $awsResourceGroup --name $awsMachineName --resource-type "Microsoft.HybridCompute/machines" --query id)"
export awsMachineResourceId="$(echo $awsMachineResourceId | tr -d "\"" | tr -d '\r')"
az resource tag --ids $awsMachineResourceId --tags "Hosting Platform"="AWS"

"Az resource tag" 命令的一个输出的屏幕截图。

标记 Azure Arc 连接的 GCP Ubuntu 服务器Tag Azure Arc connected GCP Ubuntu server

在 CLI 中运行以下命令,将标记应用 Hosting Platform : GCP 到已启用 GCP Azure Arc 的服务器。In CLI, run the following commands to apply the Hosting Platform : GCP tag to your GCP Azure Arc enabled servers.

备注

如果使用相关 Azure Arc Terraform 教程中所述的方法连接了 GCP 实例,则需要调整和的值, gcpResourceGroup gcpMachineName 以匹配特定于环境的值。If you connected your GCP instances using a method other than the one described in the related Azure Arc Terraform tutorial, then you will need to adjust the values for gcpResourceGroup and gcpMachineName to match values specific to your environment.

export gcpResourceGroup="arc-gcp-demo"
export gcpMachineName="arc-gcp-demo"
export gcpMachineResourceId="$(az resource show --resource-group $gcpResourceGroup --name $gcpMachineName --resource-type "Microsoft.HybridCompute/machines" --query id)"
export gcpMachineResourceId="$(echo $gcpMachineResourceId | tr -d "\"" | tr -d '\r')"
az resource tag --resource-group $gcpResourceGroup --ids $gcpMachineResourceId --tags "Hosting Platform"="GCP"

"Az resource tag" 命令的另一个输出的屏幕截图。

使用资源 Graph 资源管理器按标记查询资源Query resources by tag using Resource Graph Explorer

将标记应用到多个云中托管的资源之后,使用资源 Graph 资源管理器对其进行查询,并深入了解你的多云。After you apply tags to the resources that are hosted in multiple clouds, use Resource Graph Explorer to query them and get insight into your multicloud landscape.

  1. 在查询窗口中输入以下查询:In the query window, enter the following query:

    Resources
    | where type =~ 'Microsoft.HybridCompute/machines'
    | where isnotempty(tags['Hosting Platform'])
    | project name, location, resourceGroup, tags
    

    资源 Graph 资源管理器查询的详细信息的屏幕截图。

  2. 单击 " 运行查询 ",然后选择 " 带格式的结果 " 切换。Click Run Query and then select the Formatted Results toggle. 如果正确完成,应会看到所有启用了 Azure Arc 的服务器及其分配的 Hosting Platform 标记值。If done correctly, you should see all Azure Arc enabled servers and their assigned Hosting Platform tag values.

    资源 Graph 资源管理器查询的结果的屏幕截图。

    还可以从 Azure 门户查看预计服务器上的标记。We can also view the tags on the projected servers from Azure portal.

    启用了 Azure Arc 的服务器上的一组标记的屏幕截图。

    启用了 Azure Arc 的服务器上的另一组标记的屏幕截图。

清理环境Clean up your environment

完成以下步骤来清理环境。Complete the following steps to clean up your environment.

  1. 按照每个指南中的拆卸说明,从每个环境中删除虚拟机。Remove the virtual machines from each environment by following the teardown instructions from each guide.

  2. 执行 Azure CLI 中的以下脚本,删除作为本指南的一部分创建的标记。Remove tags created as part of this guide by executing the following script in Azure CLI.

    az tag remove-value --name "Hosting Platform" --value "Azure"
    az tag remove-value --name "Hosting Platform" --value "AWS"
    az tag remove-value --name "Hosting Platform" --value "GCP"
    az tag remove-value --name "Hosting Platform" --value "On-premises"
    az tag create --name "Hosting Platform"