教程:监视和更新 Azure 中的 Windows 虚拟机Tutorial: Monitor and update a Windows virtual machine in Azure

Azure 监视使用代理从 Azure VM 收集启动和性能数据,将此数据存储在 Azure 存储中,并使其可供通过门户、Azure PowerShell 模块和 Azure CLI 进行访问。Azure monitoring uses agents to collect boot and performance data from Azure VMs, store this data in Azure storage, and make it accessible through portal, the Azure PowerShell module, and the Azure CLI. 使用更新管理可以管理 Azure Windows VM 的更新和修补程序。Update management allows you to manage updates and patches for your Azure Windows VMs.

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 在 VM 上启用启动诊断Enable boot diagnostics on a VM
  • 查看启动诊断View boot diagnostics
  • 查看 VM 主机指标View VM host metrics
  • 安装诊断扩展Install the diagnostics extension
  • 查看 VM 指标View VM metrics
  • 创建警报Create an alert

启动 Azure PowerShellLaunch Azure PowerShell

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块This article has been updated to use the new Azure PowerShell Az module. AzureRM 将继续获取关键的 bug 修复更新,但新功能将只出现在 Az 模块中。AzureRM will continue to get critical bugfix updates, but new features will be in the Az module only.

  • 若要在本地安装 Az 模块,请参阅安装 Azure PowerShellTo install the Az module locally, see Install Azure PowerShell.
  • 如果在本地安装 Az 模块,可通过运行 Enable-AzureRmAlias 来启用 AzureRM 兼容性。If you install the Az module locally, you can enable the AzureRM compatibility by running Enable-AzureRmAlias.

创建虚拟机Create virtual machine

若要在本教程中配置 Azure 监视和更新管理,需要 Azure 中的 Windows VM。To configure Azure monitoring and update management in this tutorial, you need a Windows VM in Azure. 首先,使用 Get-Credential 设置 VM 的管理员用户名和密码:First, set an administrator username and password for the VM with Get-Credential:

Connect-AzAccount -Environment AzureChinaCloud
$cred = Get-Credential

现在,使用 New-AzVM 创建 VM。Now create the VM with New-AzVM. 以下示例在“ChinaEast”位置创建一个名为 myVM 的 VM。The following example creates a VM named myVM in the ChinaEast location. 如果资源组 myResourceGroupMonitorMonitor 和支持的网络资源不存在,则会创建它们:If they do not already exist, the resource group myResourceGroupMonitorMonitor and supporting network resources are created:

New-AzVm `
    -ResourceGroupName "myResourceGroupMonitor" `
    -Name "myVM" `
    -Location "China East" `
    -Credential $cred

创建资源和 VM 需要几分钟的时间。It takes a few minutes for the resources and VM to be created.

查看启动诊断View boot diagnostics

当 Windows 虚拟机启动时,启动诊断代理将捕获屏幕输出,可以使用该输出进行故障排除。As Windows virtual machines boot up, the boot diagnostic agent captures screen output that can be used for troubleshooting purpose. 此功能是默认启用的。This capability is enabled by default. 捕获的屏幕截图存储在一个 Azure 存储帐户中,该帐户也是默认创建的。The captured screenshots are stored in an Azure storage account, which is also created by default.

可以使用 Get-AzureRmVMBootDiagnosticsData 命令获取启动诊断数据。You can get the boot diagnostic data with the Get-AzureRmVMBootDiagnosticsData command. 在下面的示例中,启动诊断下载到了 *c:* 驱动器的根目录中。In the following example, boot diagnostics are downloaded to the root of the *c:* drive.

Get-AzVMBootDiagnosticsData -ResourceGroupName "myResourceGroupMonitor" -Name "myVM" -Windows -LocalPath "c:\"

查看主机指标View host metrics

Windows VM 在 Azure 中有一个与它交互的专用主机 VM。A Windows VM has a dedicated Host VM in Azure that it interacts with. 系统会自动收集该主机的指标,可以在 Azure 门户中查看这些指标。Metrics are automatically collected for the Host and can be viewed in the Azure portal.

  1. 在 Azure 门户中单击“资源组”,选择“myResourceGroupMonitor”,并在资源列表中选择“myVM”。In the Azure portal, click Resource Groups, select myResourceGroupMonitor, and then select myVM in the resource list.

  2. 要查看主机 VM 的性能情况,请在 VM 边栏选项卡上单击“指标”,并选择“可用指标”下的任一主机指标。Click Metrics on the VM blade, and then select any of the Host metrics under Available metrics to see how the Host VM is performing.

    查看主机指标

安装诊断扩展Install diagnostics extension

可以使用基本的主机指标,但若要查看更详细的指标和 VM 特定的指标,需在 VM 上安装 Azure 诊断扩展。The basic host metrics are available, but to see more granular and VM-specific metrics, you need to install the Azure diagnostics extension on the VM. 使用 Azure 诊断扩展可从 VM 检索其他监视数据和诊断数据。The Azure diagnostics extension allows additional monitoring and diagnostics data to be retrieved from the VM. 可以查看这些性能指标,并根据 VM 的性能情况创建警报。You can view these performance metrics and create alerts based on how the VM performs. 诊断扩展是通过 Azure 门户安装的,如下所述:The diagnostic extension is installed through the Azure portal as follows:

  1. 在 Azure 门户中单击“资源组”,选择“myResourceGroupMonitor”,并在资源列表中选择“myVM”。In the Azure portal, click Resource Groups, select myResourceGroupMonitor, and then select myVM in the resource list.

  2. 单击“诊断设置”。Click Diagnosis settings. 列表中将显示已在上一部分中启用的“启动诊断”。The list shows that Boot diagnostics are already enabled from the previous section. 单击“基本指标”对应的复选框。Click the check box for Basic metrics.

  3. 单击“启用来宾级监视”按钮。Click the Enable guest-level monitoring button.

    查看诊断指标

查看 VM 指标View VM metrics

可以像查看主机 VM 指标一样查看 VM 指标:You can view the VM metrics in the same way that you viewed the host VM metrics:

  1. 在 Azure 门户中单击“资源组”,选择“myResourceGroupMonitor”,并在资源列表中选择“myVM”。In the Azure portal, click Resource Groups, select myResourceGroupMonitor, and then select myVM in the resource list.

  2. 要查看 VM 的性能情况,请在 VM 边栏选项卡上单击“指标”,并选择“可用指标”下的任一诊断指标。To see how the VM is performing, click Metrics on the VM blade, and then select any of the diagnostics metrics under Available metrics.

    查看 VM 指标

创建警报Create alerts

可以根据特定的性能指标创建警报。You can create alerts based on specific performance metrics. 例如,当平均 CPU 使用率超过特定的阈值或者可用磁盘空间低于特定的空间量时,警报可用于发出通知。Alerts can be used to notify you when average CPU usage exceeds a certain threshold or available free disk space drops below a certain amount, for example. 警报显示在 Azure 门户中,也可以通过电子邮件发送。Alerts are displayed in the Azure portal or can be sent via email. 还可以触发 Azure 自动化 Runbook 或 Azure 逻辑应用来响应生成的警报。You can also trigger Azure Automation runbooks or Azure Logic Apps in response to alerts being generated.

以下示例针对平均 CPU 使用率创建警报。The following example creates an alert for average CPU usage.

  1. 在 Azure 门户中单击“资源组”,选择“myResourceGroupMonitor”,并在资源列表中选择“myVM”。In the Azure portal, click Resource Groups, select myResourceGroupMonitor, and then select myVM in the resource list.
  2. 在 VM 边栏选项卡上单击“警报规则”,然后单击警报边栏选项卡顶部的“添加指标警报”。Click Alert rules on the VM blade, then click Add metric alert across the top of the alerts blade.
  3. 为警报提供名称,例如 myAlertRuleProvide a Name for your alert, such as myAlertRule
  4. 若要在 CPU 百分比持续 5 分钟超过 1.0 时触发警报,请选中其他所有默认值。To trigger an alert when CPU percentage exceeds 1.0 for five minutes, leave all the other defaults selected.
  5. (可选)选中“电子邮件所有者、参与者和读者”对应的框,以便向他们发送电子邮件通知。Optionally, check the box for Email owners, contributors, and readers to send email notification. 默认操作是在门户中显示通知。The default action is to present a notification in the portal.
  6. 单击“确定”按钮。Click the OK button.

后续步骤Next steps

在本教程中,你已使用 Azure 安全中心配置并查看了 VM。In this tutorial, you configured and reviewed VMs with Azure Security Center. 你已了解如何:You learned how to:

  • 创建虚拟网络Create a virtual network
  • 创建资源组和 VMCreate a resource group and VM
  • 在 VM 上启用启动诊断Enable boot diagnostics on the VM
  • 查看启动诊断View boot diagnostics
  • 查看主机指标View host metrics
  • 安装诊断扩展Install the diagnostics extension
  • 查看 VM 指标View VM metrics
  • 创建警报Create an alert