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

如何监视和更新 Azure 中的 Linux 虚拟机How to monitor and update a Linux virtual machine in Azure

为确保 Azure 中的虚拟机 (VM) 正常运行,可以查看启动诊断、性能指标,并管理程序包更新。To ensure your virtual machines (VMs) in Azure are running correctly, you can review boot diagnostics, performance metrics and manage package updates. 本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 在 VM 上启用启动诊断Enable boot diagnostics on the VM
  • 查看启动诊断View boot diagnostics
  • 查看主机指标View host metrics
  • 在 VM 上启用诊断扩展Enable diagnostics extension on the VM
  • 查看 VM 指标View VM metrics
  • 基于诊断指标创建警报Create alerts based on diagnostic metrics
  • 管理程序包更新Manage package updates
  • 设置高级监视Set up advanced monitoring

启动 Azure Cloud ShellLaunch Azure Cloud Shell

Azure Cloud Shell 是免费的交互式 shell,可以使用它运行本文中的步骤。The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. 它预安装有常用 Azure 工具并将其配置与帐户一起使用。It has common Azure tools preinstalled and configured to use with your account. 请直接单击“复制”对代码进行复制,将其粘贴到 Cloud Shell 中,然后按 Enter 来运行它。Just click the Copy to copy the code, paste it into the Cloud Shell, and then press enter to run it. 可通过多种方式来启动 Cloud Shell:There are a few ways to launch the Cloud Shell:

单击代码块右上角的“试用”。Click Try It in the upper right corner of a code block. 本文中的 Cloud Shell
在浏览器中打开 Cloud Shell。Open Cloud Shell in your browser. https://shell.azure.com/bashhttps://shell.azure.com/bash
单击 Azure 门户右上角菜单上的“Cloud Shell”按钮。Click the Cloud Shell button on the menu in the upper right of the Azure portal. 门户中的 Cloud Shell

如果选择在本地安装并使用 CLI,本教程要求运行 Azure CLI 2.0.4 或更高版本。If you choose to install and use the CLI locally, this tutorial requires that you are running the Azure CLI version 2.0.4 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLI 2.0If you need to install or upgrade, see Install Azure CLI 2.0.

创建 VMCreate VM

若要查看诊断和指标的状态,需要创建一个 VM。To see diagnostics and metrics in action, you need a VM. 首先,使用 az group create 创建资源组。First, create a resource group with az group create. 以下示例在 eastus 位置创建名为 myResourceGroupMonitor 的资源组。The following example creates a resource group named myResourceGroupMonitor in the eastus location.

az group create --name myResourceGroupMonitor --location eastus

现使用 az vm create 创建 VM。Now create a VM with az vm create. 以下示例创建一个名为 myVM 的 VM:The following example creates a VM named myVM:

az vm create \
  --resource-group myResourceGroupMonitor \
  --name myVM \
  --image UbuntuLTS \
  --admin-username azureuser \
  --generate-ssh-keys

启用启动诊断Enable boot diagnostics

Linux VM 启动时,启动诊断扩展将捕获启动输出并将其存储在 Azure 存储中。As Linux VMs boot, the boot diagnostic extension captures boot output and stores it in Azure storage. 此数据可以用于排查 VM 启动问题。This data can be used to troubleshoot VM boot issues. 使用 Azure CLI 创建 Linux VM 时,不会自动启用启动诊断。Boot diagnostics are not automatically enabled when you create a Linux VM using the Azure CLI.

在启用启动诊断之前,需要创建一个存储帐户来存储启动日志。Before enabling boot diagnostics, a storage account needs to be created for storing boot logs. 存储帐户的名称必须全局唯一,介于 3 和 24 个字符之间,并且只能包含数字和小写字母。Storage accounts must have a globally unique name, be between 3 and 24 characters, and must contain only numbers and lowercase letters. 使用 az storage account create 命令创建存储帐户。Create a storage account with the az storage account create command. 本示例使用一个随机字符串来创建唯一的存储帐户名称。In this example, a random string is used to create a unique storage account name.

storageacct=mydiagdata$RANDOM

az storage account create \
  --resource-group myResourceGroupMonitor \
  --name $storageacct \
  --sku Standard_LRS \
  --location eastus

启用引导诊断时,需要 Blob 存储容器的 URI。When enabling boot diagnostics, the URI to the blob storage container is needed. 以下命令查询存储帐户以返回此 URI。The following command queries the storage account to return this URI. URI 值存储在名为 bloburi 的变量中,会在下一步骤中使用。The URI value is stored in a variable names bloburi, which is used in the next step.

bloburi=$(az storage account show --resource-group myResourceGroupMonitor --name $storageacct --query 'primaryEndpoints.blob' -o tsv)

现在,请使用 az vm boot-diagnostics enable 启用启动诊断。Now enable boot diagnostics with az vm boot-diagnostics enable. --storage 值是在上一步骤中收集的 Blob URI。The --storage value is the blob URI collected in the previous step.

az vm boot-diagnostics enable \
  --resource-group myResourceGroupMonitor \
  --name myVM \
  --storage $bloburi

查看启动诊断View boot diagnostics

启用引导诊断后,每当停止再启动 VM 时,会将有关启动过程的信息写入日志文件。When boot diagnostics are enabled, each time you stop and start the VM, information about the boot process is written to a log file. 本示例首先使用 az vm deallocate 命令解除分配 VM,如下所示:For this example, first deallocate the VM with the az vm deallocate command as follows:

az vm deallocate --resource-group myResourceGroupMonitor --name myVM

现在,请使用 az vm start 命令启动 VM,如下所示:Now start the VM with the az vm start command as follows:

az vm start --resource-group myResourceGroupMonitor --name myVM

可以使用 az vm boot-diagnostics get-boot-log 命令获取 myVM 的启动诊断数据,如下所示:You can get the boot diagnostic data for myVM with the az vm boot-diagnostics get-boot-log command as follows:

az vm boot-diagnostics get-boot-log --resource-group myResourceGroupMonitor --name myVM

查看主机指标View host metrics

Linux VM 在 Azure 中有一个与它交互的专用主机。A Linux VM has a dedicated host in Azure that it interacts with. 系统会自动收集该主机的指标,可以在 Azure 门户中查看这些指标,如下所示:Metrics are automatically collected for the host and can be viewed in 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. 要查看主机 VM 的执行方式,请在 VM 边栏选项卡上单击“指标”,并选择“可用指标”下面的任一“[主机]”指标。To see how the host VM is performing, click Metrics on the VM blade, then select any of the [Host] metrics under Available metrics.

    查看主机指标

安装诊断扩展Install diagnostics extension

重要

本文档介绍了已弃用的 Linux 诊断扩展 2.3 版。This document describes version 2.3 of the Linux Diagnostic Extension, which has been deprecated. 2018 年 6 月 30 日后将不再支持 2.3 版。Version 2.3 will be supported until June 30, 2018.

可改为启用 Linux 诊断扩展 3.0 版。Version 3.0 of the Linux Diagnostic Extension can be enabled instead. 有关详细信息,请参阅文档For more information, see the documentation.

可以使用基本的主机指标,但若要查看更详细的指标和 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 门户中,单击“资源组”,选择“myResourceGroup”,并在资源列表中选择“myVM”。In the Azure portal, click Resource Groups, select myResourceGroup, 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. 在“存储帐户”部分中,浏览到在上一部分创建的 mydiagdata[1234] 帐户并将其选中。In the Storage account section, browse to and select the mydiagdata[1234] account created in the previous section.
  4. 单击“保存”按钮 。Click the Save 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 门户中,单击“资源组”,选择“myResourceGroup”,并在资源列表中选择“myVM”。In the Azure portal, click Resource Groups, select myResourceGroup, 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 门户中,单击“资源组”,选择“myResourceGroup”,并在资源列表中选择“myVM”。In the Azure portal, click Resource Groups, select myResourceGroup, 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.

管理程序包更新Manage package updates

通过使用更新管理,你可以管理 Azure Linux 虚拟机的程序包更新和修补程序。By using Update management, you can manage package updates and patches for your Azure Linux VMs. 可以直接从虚拟机快速评估可用更新的状态,计划安装所需更新,并查看部署结果,以验证更新是否已成功应用于虚拟机。Directly from your VM, you can quickly assess the status of available updates, schedule installation of required updates, and review deployment results to verify updates were applied successfully to the VM.

有关定价信息,请参阅更新管理自动化定价For pricing information, see Automation pricing for Update management

启用更新管理(预览)Enable Update management (Preview)

为虚拟机启用更新管理Enable Update management for your VM

  1. 在屏幕的左侧,选择“虚拟机”On the left-hand side of the screen, select Virtual machines.
  2. 从列表中选择一个虚拟机。From the list, select a VM.
  3. 在虚拟机屏幕的“操作”部分中,单击“更新管理”。On the VM screen, in the Operations section, click Update management. “启用更新管理”屏幕随即打开。The Enable Update management screen opens.

执行验证以确定是否为该虚拟机启用了更新管理。Validation is performed to determine if Update management is enabled for this VM. 验证包括检查 Log Analytics 工作区和链接的自动化帐户,以及解决方案是否在工作区中。The validation includes checks for a Log Analytics workspace and linked Automation account, and if the solution is in the workspace.

Log Analytics 工作区用于收集由功能和服务(如更新管理)生成的数据。A Log Analytics workspace is used to collect data that is generated by features and services such as Update management. 工作区提供了一个位置来查看和分析来自多个数据源的数据。The workspace provides a single location to review and analyze data from multiple sources. 要在需要更新的 VM 上执行其他操作,可使用 Azure 自动化运行针对 VM 的脚本,例如下载和应用更新。To perform additional action on VMs that require updates, Azure Automation allows you to run scripts against VMs, such as to download and apply updates.

验证过程还会检查 VM 是否预配了 Microsoft Monitoring Agent (MMA) 和混合辅助角色。The validation process also checks to see if the VM is provisioned with the Microsoft Monitoring Agent (MMA) and hybrid worker. 此代理用于与虚拟机通信并获取关于更新状态的信息。This agent is used to communicate with the VM and obtain information about the update status.

如果未满足这些先决条件,则会显示横幅,可在其中选择启用该解决方案。If these prerequisites are not met, a banner appears that gives you the option to enable the solution.

更新管理载入配置横幅

单击横幅以启用该解决方案。Click the banner to enable the solution. 如果在验证后发现缺少下列任何先决条件,将自动添加这些条件:If any of the following prerequisites were found to be missing after the validation, they will be automatically added:

“启用更新管理”屏幕随即打开。The Enable Update Management screen opens. 配置设置,然后单击“启用”。Configure the settings, and click Enable.

启用“更新管理解决方案”

启用该解决方案最多需要 15 分钟,在此期间,不应关闭浏览器窗口。Enabling the solution can take up to 15 minutes, and during this time you should not close the browser window. 启用该解决方案后,虚拟机中缺少的更新信息会从程序包管理器流向 Log Analytics。After the solution is enabled, information about missing updates from the package manager on the VM flows to Log Analytics. 这些数据需花费 30 分钟到 6 小时的时间才能用于分析。It can take between 30 minutes and 6 hours for the data to be available for analysis.

查看更新评估View update assessment

启用“更新管理”解决方案后,将出现“更新管理”屏幕。After the Update management solution is enabled, the Update management screen appears. 可在“缺少更新”选项卡上查看缺少更新的列表。You can see a list of missing updates on the Missing updates tab.

查看更新状态

计划更新部署Schedule an update deployment

若要安装更新,请计划一个遵循发布计划和维护时段的部署。To install updates, schedule a deployment that follows your release schedule and maintenance window.

通过单击“更新管理”屏幕顶部的“计划更新部署”,计划一个用于虚拟机的新更新部署。Schedule a new Update Deployment for the VM by clicking Schedule update deployment at the top of the Update management screen. 在“新建更新部署”屏幕中,指定以下信息:In the New update deployment screen, specify the following information:

  • 名称 - 提供用于标识更新部署的唯一名称。Name - Provide a unique name to identify the update deployment.
  • 要排除的更新 - 选择此项,可输入要从更新中排除的程序包的名称。Updates to exclude - select this to enter names of packages to exclude from the update.
  • 计划设置 - 可以接受默认的日期和时间,即当前时间后 30 分钟,或指定不同的时间。Schedule settings - You can either accept the default date and time, which is 30 minutes after current time, or specify a different time. 还可以指定部署是发生一次还是设置定期计划。You can also specify whether the deployment occurs once or set up a recurring schedule. 单击“重复周期”下的“重复执行”选项可设置定期计划。Click the Recurring option under Recurrence to set up a recurring schedule.

    更新计划设置屏幕

  • 维护时段(分钟) - 指定要在其中进行更新部署的时间段。Maintenance window (minutes) - Specify the period of time you want the update deployment to occur within. 这有助于确保在定义的维护时段内执行更改。This helps ensure changes are performed within your defined maintenance windows.

完成配置计划后,单击“创建”按钮,然后返回到状态仪表板。After you have completed configuring the schedule, click Create button and you return to the status dashboard. 请注意,“已计划”表显示你创建的部署计划。Notice that the Scheduled table shows the deployment schedule you created.

警告

如果维护时段的时间足够,虚拟机将在安装更新后自动重新启动。The VM will be restarted automatically after updates are installed if there is enough time in the maintenance window.

更新管理使用虚拟机上的现有程序包管理器来安装程序包。Update management uses the existing package manager on your VM to install packages.

查看更新部署结果View results of an update deployment

在计划的部署开始后,可以在“更新管理”屏幕的“更新部署”选项卡上查看该部署的状态。After the scheduled deployment is started, you can see the status for that deployment on the Update deployments tab on the Update management screen. 如果部署当前正在运行,则状态显示为“正在运行”。If it is currently running, it's status shows as In progress. 完成后,如果成功,其状态会更改为“已成功”。After it completes, if successful, it changes to Succeeded. 如果部署中有一个或多个更新失败,则状态为“已失败”。If there is a failure with one or more updates in the deployment, the status is Failed. 单击完成的更新部署以查看该更新部署的仪表板。Click the completed update deployment to see the dashboard for that update deployment.

特定部署的更新部署状态仪表板

在“更新结果”中,磁贴总结了 VM 上更新和部署结果的总数。In Update results tile is a summary of the total number of updates and deployment results on the VM. 右侧的表格详细列出了每个更新的细目以及安装结果,结果可能是以下值之一:In the table to the right is a detailed breakdown of each update and the installation results, which could be one of the following values:

  • 未尝试 - 由于定义的维护时段时长不足,因而未安装更新。Not attempted - the update was not installed because there was insufficient time available based on the maintenance window duration defined.
  • 已成功 - 更新已成功下载并安装在虚拟机上Succeeded - the update was successfully downloaded and installed on the VM
  • 已失败 - 无法下载或在虚拟机上安装更新。Failed - the update failed to download or install on the VM.

单击“所有日志”,查看部署创建的所有日志条目。Click All logs to see all log entries that the deployment created.

单击“输出”磁贴,查看负责管理目标虚拟机更新部署的 runbook 的作业流。Click the Output tile to see job stream of the runbook responsible for managing the update deployment on the target VM.

单击“错误”,查看有关部署中的任何错误的详细信息。Click Errors to see detailed information about any errors from the deployment.

高级监视Advanced monitoring

可以使用 Operations Management Suite 以更高级的方法来监视 VM。You can do more advanced monitoring of your VM by using Operations Management Suite. 可以注册 Operations Management Suite 免费试用版(如果尚未注册)。If you haven't already done so, you can sign up for a free trial of Operations Management Suite.

如果有权访问 OMS 门户,可以在“设置”边栏选项卡中找到工作区密钥和工作区标识符。When you have access to the OMS portal, you can find the workspace key and workspace identifier on the Settings blade. 请将 替换为 OMS 工作区中的值,并可以使用 az vm extension set 将 OMS 扩展添加到 VM:Replace and with the values for from your OMS workspace and then you can use az vm extension set to add the OMS extension to the VM:

az vm extension set \
  --resource-group myResourceGroupMonitor \
  --vm-name myVM \
  --name OmsAgentForLinux \
  --publisher Microsoft.EnterpriseCloud.Monitoring \
  --version 1.3 \
  --protected-settings '{"workspaceKey": "<workspace-key>"}' \
  --settings '{"workspaceId": "<workspace-id>"}'

在 OMS 门户的“日志搜索”边栏选项卡上,应会看到“myVM”,如下图中所示:On the Log Search blade of the OMS portal, you should see myVM such as what is shown in the following picture:

OMS 边栏选项卡

后续步骤Next steps

在本教程中,将配置、审核和管理虚拟机更新。In this tutorial, you configured, reviewed, and managed updates for a VM. 你已了解如何:You learned how to:

  • 在 VM 上启用启动诊断Enable boot diagnostics on the VM
  • 查看启动诊断View boot diagnostics
  • 查看主机指标View host metrics
  • 在 VM 上启用诊断扩展Enable diagnostics extension on the VM
  • 查看 VM 指标View VM metrics
  • 基于诊断指标创建警报Create alerts based on diagnostic metrics
  • 管理程序包更新Manage package updates
  • 设置高级监视Set up advanced monitoring

请转到下一教程来了解 Azure 安全中心。Advance to the next tutorial to learn about Azure Security Center.