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

使用 Windows 虚拟机规模集的 Azure 资源管理器模板将来宾 OS 指标发送到 Azure Monitor 指标存储

注意

建议使用 Azure Az PowerShell 模块与 Azure 交互。 请参阅安装 Azure PowerShell 以开始使用。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

使用 Azure Monitor Windows Azure 诊断 (WAD) 扩展,可以从作为虚拟机、云服务或 Azure Service Fabric 群集的一部分运行的来宾操作系统(来宾 OS)收集指标和日志。 该扩展可将遥测数据发送到之前链接的文章中列出的许多不同位置。

本文介绍将 Windows 虚拟机规模集的来宾 OS 性能指标发送到 Azure Monitor 数据存储的过程。 自 Microsoft Windows Azure 诊断版本 1.11 版起,可将指标直接写入已收集标准平台指标的 Azure Monitor 指标存储。 将它们存储在此位置可以访问可用于平台指标的相同操作。 操作包括近实时警报、图表绘制、路由、从 REST API 访问,等等。 在过去,Microsoft Windows Azure 诊断扩展将数据写入 Azure 存储而不是 Azure Monitor 数据存储。

如果你不熟悉资源管理器模板,请了解模板部署及其结构和语法。

先决条件

将 Azure Monitor 设置为数据接收器

Azure 诊断扩展使用名为“数据接收器”的功能将指标和日志路由到不同位置。 以下步骤说明如何通过资源管理器模板和 PowerShell 来使用新的 Azure Monitor 数据接收器部署 VM。

创作资源管理器模板

对于本示例,可以使用公开发布的示例模板

  • Azuredeploy.json 是用于部署虚拟机规模集的预配置资源管理器模板。

  • Azuredeploy.parameters.json 是一个参数文件,用于存储要为 VM 设置的用户名和密码等信息。 部署期间,资源管理器模板使用此文件中设置的参数。

下载这两个文件并在本地保存。

修改 azuredeploy.parameters.json

打开 azuredeploy.parameters.json 文件:

  • 提供要部署的 vmSKU。 我们建议使用 Standard_D2_v3。
  • 指定要对虚拟机规模集使用的 windowsOSVersion。 我们建议使用 2016-Datacenter。
  • 使用 vmssName 属性命名要部署的虚拟机规模集资源。 例如 VMSS-WAD-TEST
  • 使用 instanceCount 属性指定要在虚拟机规模集上运行的 VM 数量。
  • 输入虚拟机规模集的 adminUsernameadminPassword 值。 这些参数用于对规模集中的 VM 进行远程访问。 为了避免 VM 被劫持,请勿使用此模板中的参数。 机器人在 Internet 上扫描公共 GitHub 存储库中的用户名和密码。 它们可能会使用这些默认值测试 VM。

修改 azuredeploy.json

打开 azuredeploy.json 文件。

添加一个变量,用于保存资源管理器模板中的存储帐户信息。 在诊断配置文件中指定的任何日志或性能计数器将写入到 Azure Monitor 指标存储和此处指定的存储帐户:

"variables": {
  //add this line
  "storageAccountName": "[concat('storage', uniqueString(resourceGroup().id))]",
  ...
}

在 resources 节中找到虚拟机规模集定义,并将 identity 节添加到配置中。 这可以确保 Azure 为它分配系统标识。 此步骤还确保规模集中的 VM 可将有关自身的来宾指标发送到 Azure Monitor:

{ 
  "type": "Microsoft.Compute/virtualMachineScaleSets", 
  "name": "[variables('namingInfix')]", 
  "location": "[resourceGroup().location]", 
  "apiVersion": "2017-03-30", 
  //add these lines below
  "identity": { 
       "type": "systemAssigned" 
   }, 
   //end of lines to add
   ...
}

在虚拟机规模集资源中,找到 virtualMachineProfile 节。 添加名为 extensionsProfile 的新配置文件来管理扩展。

extensionProfile 中,将 VMSS-WAD-extension 节中所示的新扩展添加到模板。 此节是 Azure 资源扩展的托管标识,确保 Azure Monitor 接受所发出的指标。 name 字段可以包含任何名称。

MSI 扩展中的以下代码还会将诊断扩展和配置作为扩展资源添加到虚拟机规模集资源。 根据需要任意添加或删除性能计数器。

  "extensionProfile": {
    "extensions": [
      // BEGINNING of added code
      // Managed identities for Azure resources
      {
        "name": "VMSS-WAD-extension",
        "properties": {
          "publisher": "Microsoft.ManagedIdentity",
          "type": "ManagedIdentityExtensionForWindows",
          "typeHandlerVersion": "1.0",
          "autoUpgradeMinorVersion": true,
          "settings": {
            "port": 50342
          },
          "protectedSettings": {}
        }
      },
      // add diagnostic extension. (Remove this comment after pasting.)
      { 
        "name": "[concat('VMDiagnosticsVmExt','_vmNodeType0Name')]", 
        "properties": { 
          "type": "IaaSDiagnostics",
          "autoUpgradeMinorVersion": true,
          "protectedSettings": {
            "storageAccountName": "[variables('storageAccountName')]",
            "storageAccountKey": "[listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')),'2015-05-01-preview').key1]",
            "storageAccountEndPoint": "https://core.windows.net/"
          },
          "publisher": "Microsoft.Azure.Diagnostics", 
          "settings": { 
            "WadCfg": { 
              "DiagnosticMonitorConfiguration": { 
                "overallQuotaInMB": "50000", 
                "PerformanceCounters": { 
                  "scheduledTransferPeriod": "PT1M", 
                  "sinks": "AzMonSink", 
                  "PerformanceCounterConfiguration": [
                    { 
                      "counterSpecifier": "\\Memory\\% Committed Bytes In Use", 
                      "sampleRate": "PT15S" 
                    },
                    { 
                      "counterSpecifier": "\\Memory\\Available Bytes", 
                      "sampleRate": "PT15S" 
                    }, 
                    { 
                      "counterSpecifier": "\\Memory\\Committed Bytes", 
                      "sampleRate": "PT15S" 
                    }
                  ]
                },
                "EtwProviders": {
                  "EtwEventSourceProviderConfiguration": [
                    { 
                      "provider": "Microsoft-ServiceFabric-Actors", 
                      "scheduledTransferKeywordFilter": "1", 
                      "scheduledTransferPeriod": "PT5M", 
                      "DefaultEvents": { 
                        "eventDestination": "ServiceFabricReliableActorEventTable" 
                      } 
                    }, 
                    { 
                      "provider": "Microsoft-ServiceFabric-Services", 
                      "scheduledTransferPeriod": "PT5M", 
                      "DefaultEvents": { 
                        "eventDestination": "ServiceFabricReliableServiceEventTable" 
                      } 
                    } 
                  ], 
                  "EtwManifestProviderConfiguration": [
                    {
                       "provider": "cbd93bc2-71e5-4566-b3a7-595d8eeca6e8", 
                       "scheduledTransferLogLevelFilter": "Information", 
                       "scheduledTransferKeywordFilter": "4611686018427387904", 
                       "scheduledTransferPeriod": "PT5M", 
                       "DefaultEvents": { 
                         "eventDestination": "ServiceFabricSystemEventTable" 
                       } 
                    } 
                  ]
                }
              },
              "SinksConfig": { 
                 "Sink": [ 
                    { 
                    "name": "AzMonSink", 
                    "AzureMonitor": {} 
                    } 
                 ]
              }
            },
            "StorageAccount": "[variables('storageAccountName')]" 
          },
          "typeHandlerVersion": "1.11" 
        }
      }
    ]
  },
  // end of added code. Be sure that the number and type of brackets match properly when done. 
  {
  "type": "Microsoft.Insights/autoscaleSettings",
  ...
  }

为存储帐户添加 dependsOn,确保按正确的顺序创建资源:

"dependsOn": [
  "[concat('Microsoft.Network/loadBalancers/', variables('loadBalancerName'))]",
  "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]",
  //add this line below
  "[concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]"
]

如果模板中尚未创建存储帐户,请创建一个:

"resources": [
  // add this code
  {
    "type": "Microsoft.Storage/storageAccounts",
    "name": "[variables('storageAccountName')]",
    "apiVersion": "2015-05-01-preview",
    "location": "[resourceGroup().location]",
    "properties": {
      "accountType": "Standard_LRS"
    }
  },
  // end added code
  {
    "type": "Microsoft.Network/virtualNetworks",
    "name": "[variables('virtualNetworkName')]",
    ...
  }
]

保存并关闭这两个文件。

部署资源管理器模板

注意

必须运行 Azure 诊断扩展 1.5 版或更高版本,在资源管理器模板中将 autoUpgradeMinorVersion: 属性设置为 true。 Azure 会在启动 VM 后加载适当的扩展。 如果模板中没有这些设置,请进行更改并重新部署模板。

若要部署资源管理器模板,请使用 Azure PowerShell:

  1. 启动 PowerShell。

  2. 使用 Login-AzAccount 登录到 Azure 门户。

  3. 使用 Get-AzSubscription 获取订阅列表。

  4. 设置要在其中创建或更新虚拟机的订阅:

    Select-AzSubscription -SubscriptionName "<Name of the subscription>"
    
  5. 为所要部署的 VM 创建新资源组。 运行下面的命令:

     New-AzResourceGroup -Name "VMSSWADtestGrp" -Location "<Azure Region>"
    
  6. 运行以下命令以部署 VM:

    注意

    若要更新现有的规模集,请将 -Mode Incremental 添加到该命令的末尾。

    New-AzResourceGroupDeployment -Name "VMSSWADTest" -ResourceGroupName "VMSSWADtestGrp" -TemplateFile "<File path of your azuredeploy.JSON file>" -TemplateParameterFile "<File path of your azuredeploy.parameters.JSON file>"
    
  7. 部署成功后,Azure 门户中应会显示该虚拟机规模集。 该规模应该会向 Azure Monitor 发出指标。

    注意

    你可能会遇到与所选 vmSkuSize 相关的错误。 如果发生这种情况,请返回到 azuredeploy.json 文件并更新 vmSkuSize 参数的默认值。 我们建议尝试 Standard_DS1_v2

绘制指标图表

  1. 登录到 Azure 门户。

  2. 在左侧菜单中选择“监视”。

  3. 在“监视”页上选择“指标” 。

    A screenshot showing the metrics menu item on the Azure Monitor menu page.

  4. 将聚合时限更改为“过去 30 分钟”。

  5. 在资源下拉菜单中,选择创建的虚拟机规模集。

  6. 在命名空间下拉菜单中,选择虚拟机来宾

  7. 在指标下拉菜单中,选择“Memory%Committed Bytes in Use”。

    A screenshot showing the selection of namespace metric and aggregation for a metrics chart.

然后,还可以选择使用此指标中的维度,来为特定的 VM 绘制此指标的图表,或者绘制规模集中每个 VM 的图表。

后续步骤