配合使用虚拟机规模集和应用程序运行状况扩展Using Application Health extension with virtual machine scale sets

监视应用程序的运行状况是管理和升级部署的重要信号。Monitoring your application health is an important signal for managing and upgrading your deployment. Azure 虚拟机规模集支持滚动升级(包括自动 OS-image 升级),其依赖对各实例的运行状况监视来升级部署。Azure virtual machine scale sets provide support for rolling upgrades including automatic OS-image upgrades, which rely on health monitoring of the individual instances to upgrade your deployment.

本文介绍如何使用应用程序运行状况扩展监控部署在虚拟机规模集上的应用程序的运行状况。This article describes how you can use the Application Health extension to monitor the health of your applications deployed on virtual machine scale sets.

先决条件Prerequisites

本文假定你熟悉以下内容:This article assumes that you are familiar with:

何时使用应用程序运行状况扩展When to use the Application Health extension

应用程序运行状况扩展部署在虚拟机规模集实例中,并从规模集实例中报告 VM 的运行状况。The Application Health extension is deployed inside a virtual machine scale set instance and reports on VM health from inside the scale set instance. 可以配置扩展,以探测应用程序终结点并更新该实例上应用程序的状态。You can configure the extension to probe on an application endpoint and update the status of the application on that instance. Azure 会检查此实例状态,以确定实例是否符合升级操作的条件。This instance status is checked by Azure to determine whether an instance is eligible for upgrade operations.

因为扩展从 VM 中报告运行状况,所以扩展可在无法利用外部探测(例如使用自定义 Azure 负载均衡器探测的应用程序运行状况探测)的情况下使用。As the extension reports health from within a VM, the extension can be used in situations where external probes such as Application Health Probes (that utilize custom Azure Load Balancer probes) can’t be used.

扩展架构Extension schema

以下 JSON 显示应用程序运行状况扩展的架构。The following JSON shows the schema for the Application Health extension. 扩展至少需要“tcp”、“http”或“https”请求,且各自具有相关的端口或请求路径。The extension requires at a minimum either a "tcp", "http" or "https" request with an associated port or request path respectively.

{
  "type": "extensions",
  "name": "HealthExtension",
  "apiVersion": "2018-10-01",
  "location": "<location>",  
  "properties": {
    "publisher": "Microsoft.ManagedServices",
    "type": "< ApplicationHealthLinux or ApplicationHealthWindows>",
    "autoUpgradeMinorVersion": true,
    "typeHandlerVersion": "1.0",
    "settings": {
      "protocol": "<protocol>",
      "port": "<port>",
      "requestPath": "</requestPath>"
    }
  }
}  

属性值Property values

名称Name 值/示例Value / Example 数据类型Data Type
apiVersionapiVersion 2018-10-01 datedate
publisherpublisher Microsoft.ManagedServices stringstring
typetype ApplicationHealthLinux (Linux)、ApplicationHealthWindows (Windows)ApplicationHealthLinux (Linux), ApplicationHealthWindows (Windows) stringstring
typeHandlerVersiontypeHandlerVersion 1.0 intint

设置Settings

名称Name 值/示例Value / Example 数据类型Data Type
协议protocol httphttpstcphttp or https or tcp stringstring
portport 协议为 httphttps 时为可选,协议为 tcp 时为必需Optional when protocol is http or https, mandatory when protocol is tcp intint
requestPathrequestPath 协议为 httphttps 时为必需,协议为 tcp 时为不允许Mandatory when protocol is http or https, not allowed when protocol is tcp stringstring

部署应用程序运行状况扩展Deploy the Application Health extension

可以使用多种方法将应用程序运行状况扩展部署到规模集,如下面的示例所示。There are multiple ways of deploying the Application Health extension to your scale sets as detailed in the examples below.

REST APIREST API

下面的示例将应用程序运行状况扩展(名为 myHealthExtension)添加到 extensionProfile(位于基于 Windows 的规模集的规模集模型中)。The following example adds the Application Health extension (with name myHealthExtension) to the extensionProfile in the scale set model of a Windows-based scale set.

PUT on `/subscriptions/subscription_id/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSet/extensions/myHealthExtension?api-version=2018-10-01`
{
  "name": "myHealthExtension",
  "properties": {
    "publisher": " Microsoft.ManagedServices",
    "type": "< ApplicationHealthWindows>",
    "autoUpgradeMinorVersion": true,
    "typeHandlerVersion": "1.0",
    "settings": {
      "protocol": "<protocol>",
      "port": "<port>",
      "requestPath": "</requestPath>"
    }
  }
}

使用 PATCH 编辑已部署的扩展。Use PATCH to edit an already deployed extension.

Azure PowerShellAzure PowerShell

使用 Add-AzVmssExtension cmdlet 将应用程序运行状况扩展添加到规模集模型定义中。Use the Add-AzVmssExtension cmdlet to add the Application Health extension to the scale set model definition.

下面的示例将应用程序运行状况扩展添加到基于 Windows 的规模集的规模集模型的 extensionProfile 中。The following example adds the Application Health extension to the extensionProfile in the scale set model of a Windows-based scale set. 该示例使用新的 Az PowerShell 模块。The example uses the new Az PowerShell module.

# Define the scale set variables
$vmScaleSetName = "myVMScaleSet"
$vmScaleSetResourceGroup = "myVMScaleSetResourceGroup"

# Define the Application Health extension properties
$publicConfig = @{"protocol" = "http"; "port" = 80; "requestPath" = "/healthEndpoint"};
$extensionName = "myHealthExtension"
$extensionType = "ApplicationHealthWindows"
$publisher = "Microsoft.ManagedServices"

# Get the scale set object
$vmScaleSet = Get-AzVmss `
  -ResourceGroupName $vmScaleSetResourceGroup `
  -VMScaleSetName $vmScaleSetName

# Add the Application Health extension to the scale set model
Add-AzVmssExtension -VirtualMachineScaleSet $vmScaleSet `
  -Name $extensionName `
  -Publisher $publisher `
  -Setting $publicConfig `
  -Type $extensionType `
  -TypeHandlerVersion "1.0" `
  -AutoUpgradeMinorVersion $True

# Update the scale set
Update-AzVmss -ResourceGroupName $vmScaleSetResourceGroup `
  -Name $vmScaleSetName `
  -VirtualMachineScaleSet $vmScaleSet

Azure CLI 2.0Azure CLI 2.0

使用 az vmss 扩展集将应用程序运行状况扩展添加到规模集模型定义中。Use az vmss extension set to add the Application Health extension to the scale set model definition.

下面的示例将应用程序运行状况扩展添加到基于 Linux 的规模集的规模集模型中。The following example adds the Application Health extension to the scale set model of a Linux-based scale set.

az vmss extension set \
  --name ApplicationHealthLinux \
  --publisher Microsoft.ManagedServices \
  --version 1.0 \
  --resource-group <myVMScaleSetResourceGroup> \
  --vmss-name <myVMScaleSet> \
  --settings ./extension.json

extension.json 文件内容。The extension.json file content.

{
  "protocol": "<protocol>",
  "port": "<port>",
  "requestPath": "</requestPath>"
}

故障排除Troubleshoot

扩展执行输出将记录到在以下目录中发现的文件:Extension execution output is logged to files found in the following directories:

C:\WindowsAzure\Logs\Plugins\Microsoft.ManagedServices.ApplicationHealthWindows\<version>\
/var/lib/waagent/Microsoft.ManagedServices.ApplicationHealthLinux-<extension_version>/status
/var/log/azure/applicationhealth-extension

日志还会定期捕获应用程序运行状况状态。The logs also periodically capture the application health status.

后续步骤Next steps

了解如何在虚拟机规模集上部署应用程序Learn how to deploy your application on virtual machine scale sets.