教程:监视和更新 Azure 中的 Linux 虚拟机Tutorial: 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

Note

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

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

创建 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. 以下示例在“chinaeast”位置创建名为“myResourceGroupMonitor”的资源组。The following example creates a resource group named myResourceGroupMonitor in the chinaeast location.

az group create --name myResourceGroupMonitor --location chinaeast

现在,请使用 az vm create 创建 VM。Now create a VM with az vm create. 以下示例将创建名为 myVM 的 VM,并生成 SSH 密钥(如果它们尚不存在于 ~/.ssh/ 中):The following example creates a VM named myVM and generates SSH keys if they do not already exist in ~/.ssh/:

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 chinaeast

启用引导诊断时,需要 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, select Resource Groups, choose myResourceGroupMonitor, and then select myVM in the resource list.

  2. 若要查看主机 VM 的性能情况,请在 VM 窗口中选择“指标”,并选择“可用指标”下面的任一“[主机]”指标。To see how the host VM is performing, select Metrics on the VM window, then choose any of the [Host] metrics under Available metrics.

    查看主机指标

安装诊断扩展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, choose Resource Groups, select myResourceGroupMonitor, and then select myVM in the resource list.

  2. 选择“诊断设置”。Select Diagnosis settings. 在“选取存储帐户”下拉菜单中,如果尚未选择,请选择在上一部分中创建的“mydiagdata[1234]”帐户。In the Pick a storage account drop-down menu, if not already selected, choose the mydiagdata[1234] account created in the previous section.

  3. 选择“启用来宾级监视”按钮。Select the Enable guest-level monitoring button.

    查看诊断指标

创建警报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, select Resource Groups, select myResourceGroupMonitor, and then select myVM in the resource list.
  2. 选择“警报(经典)”,然后在警报窗口顶部选择“添加指标警报(经典)”。Select Alerts (classic), then choose to Add metric alert (classic) across the top of the alerts window.
  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. 选择“确定”按钮。Select the OK button.

后续步骤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
  • 基于诊断指标创建警报Create alerts based on diagnostic metrics