📌  相关文章
📜  Microsoft Azure – 为 Azure 中的 VM 启用启动诊断(1)

📅  最后修改于: 2023-12-03 15:02:56.519000             🧑  作者: Mango

Microsoft Azure - 为 Azure 中的 VM 启用启动诊断

启动诊断是一种用于定位和解决 Azure 虚拟机(VM)启动期间问题的工具。 它会捕获 VM 启动期间发生的各种事件,例如 VM 启动、网络配置、磁盘 I/O 和错误警报,这些事件可用于分析和故障排除。

在本指南中,我们将介绍如何启用 Azure 中虚拟机的启动诊断。

步骤
  1. 登录 Azure 门户
  2. 选择左侧导航菜单中的 “虚拟机”
  3. 从虚拟机列表中选择 VM
  4. 在 VM 概述页面中,在左侧菜单中选择 “诊断设置”
  5. 在 “诊断设置” 选项卡中,选择 “启动诊断”,然后单击 “启用”
  6. 配置启动诊断设置,例如日志的保留期和存储位置
  7. 单击 “保存” 并等待审核

启动诊断现在已启用,并在 VM 启动期间对事件进行记录。 事件可以通过 Azure 门户, PowerShell 或 CLI 进行检索和分析。 以下是一些常见的用例:

  • 分析 VM 启动时间
  • 检查网络配置问题
  • 诊断 OS 引导问题
PowerShell 代码示例

以下 PowerShell 代码演示如何使用 Set-AzDiagnosticSetting cmdlet 为 VM 启用启动诊断。

# 安装 Az PowerShell 模块
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force

# 连接到 Azure
Connect-AzAccount

# 定义 VM 名称和资源组
$VMName = "myVM"
$ResourceGroup = "myResourceGroup"

# 定义日志存储信息
$storageAccount = Get-AzStorageAccount -ResourceGroupName $ResourceGroup -Name "mystorageaccount"
$storageContext = $storageAccount.Context
$logsFolderUri = "https://mystorageaccount.blob.core.windows.net/vhds/"

# 启用启动诊断
Set-AzDiagnosticSetting -ResourceId "/subscriptions/<subscription-id>/resourceGroups/$ResourceGroup/providers/Microsoft.Compute/virtualMachines/$VMName" `
    -StorageAccountId $storageAccount.Id `
    -StorageAccountName $storageAccount.StorageAccountName `
    -StorageAccountKey $storageAccount.PrimaryAccessKey `
    -LogsRetentionInDays 30 `
    -StorageUri $logsFolderUri `
    -Enabled $true `
    -Categories "Startup"
CLI 代码示例

以下 CLI 代码演示如何使用 az monitor diagnostic-settings create 命令为 VM 启用启动诊断。

# 获得存储帐户密钥并定义日志存储信息
MY_STORAGE_ACCOUNT=mystorageaccount
MY_RESOURCE_GROUP=myResourceGroup
LOG_FOLDER_URI=https://$MY_STORAGE_ACCOUNT.blob.core.windows.net/vhds/
STORAGE_ACCOUNT_ID=$(az storage account show --name $MY_STORAGE_ACCOUNT --resource-group $MY_RESOURCE_GROUP --query id --output tsv)
STORAGE_ACCOUNT_KEY=$(az storage account keys list --resource-group $MY_RESOURCE_GROUP --account-name $MY_STORAGE_ACCOUNT --query '[0].value' --output tsv)

# 启用启动诊断
az monitor diagnostic-settings create --resource /subscriptions/<subscription-id>/resourceGroups/$MY_RESOURCE_GROUP/providers/Microsoft.Compute/virtualMachines/myVM `
--name "StartupDiagnostic" `
--logs '[
       {
         "category": "Startup",
         "enabled": true,
         "retentionPolicy": {
           "days": 30,
           "enabled": true
         }
       }
   ]' `
--storage-account-id $STORAGE_ACCOUNT_ID `
--storage-account-name $MY_STORAGE_ACCOUNT `
--storage-account-key $STORAGE_ACCOUNT_KEY `
--storage-endpoint "https://$MY_STORAGE_ACCOUNT.blob.core.windows.net/" `
--storage-container logs `
--storage-sas "?sv=2021-05-08&ss=bfqt&srt=sco&sp=rwdlacupitfx&se=2022-07-09T01:25:41Z&st=2022-07-08T17:25:41Z&spr=https&sig=<signature>"

现在您已经在 Azure 中启用了虚拟机的启动诊断。 您可以使用 Azure 门户,PowerShell 或 CLI 进行检索和分析。