管理 Azure VM 应用程序

本文介绍如何在Azure计算库中查看、更新和删除已发布的 VM 应用程序。 然后,它介绍如何在Azure虚拟机(VM)或Virtual Machine Scale Sets上查看、监视、更新和删除已部署的 VM 应用程序资源。

本部分介绍如何在 Azure 计算库中查看、更新和删除发布的 VM 应用程序

查看已发布的 VM 应用程序

若要在 Azure 门户中查看已发布的 VM 应用程序的属性,请执行以下作:

  1. 登录到 Azure 门户
  2. 搜索 Azure 计算画廊
  3. 选择包含虚拟机应用程序的图库。
  4. 单击要查看的 VM 应用程序名称
  5. 概述/属性 面板显示有关 VM 应用的信息。
  6. 概述/版本 ”边栏选项卡显示所有已发布版本及其基本属性,如目标区域、预配状态和复制状态。
  7. 选择 特定版本 以查看其所有详细信息。

显示 VM 应用程序属性和 Azure 门户中所有版本的屏幕截图。

显示 Azure 门户中 VM 应用程序版本属性的屏幕截图。

使用Azure Resource Graph查看已发布的 VM 应用程序

Azure资源图查询可用于查看所有已发布的 VM 应用程序及其所有计算库的属性。 它提供了一种以编程方式查看应用程序清单及其属性的高效解决方案。 使用此方法与仪表板和自定义报表集成。

查看所有计算图库的列表

resources
| where type == "microsoft.compute/galleries"
| where subscriptionId == "85236c53-92ad-4e66-97a4-8253a5246b99"
| extend provisioningState = properties["provisioningState"]
| extend permissions = properties["sharingProfile"]["permissions"]
| extend communityGalleryInfo = properties["sharingProfile"]["communityGalleryInfo"]
| project subscriptionId, resourceGroup, location, name, provisioningState, permissions, communityGalleryInfo

查看所有图库中所有已发布的 VM 应用程序的列表

resources
| where subscriptionId == "85236c53-92ad-4e66-97a4-8253a5246b99"
| where type == "microsoft.compute/galleries/applications"
| extend OSType = properties["supportedOSType"]
| extend description = properties["description"]
| extend endOfLifeDate = properties["endOfLifeDate"]
| parse id with  "/subscriptions/" SubId "/resourceGroups/" rgName "/providers/Microsoft.Compute/galleries/" gallaryName "/applications/" appName  
| project subscriptionId, resourceGroup, location, gallaryName, name, OSType, description

查看所有应用程序及图库中已发布的 VM 应用程序版本列表

resources
| where type == "microsoft.compute/galleries/applications/versions"
| project subscriptionId, resourceGroup, id, location, properties
| parse id with  "/subscriptions/" SubId "/resourceGroups/" rgName "/providers/Microsoft.Compute/galleries/" gallaryName "/applications/" appName "/versions/" versionNumber 
| extend provisioningState = properties["provisioningState"]
| extend publishingProfile = properties["publishingProfile"]
| extend storageAccountType = publishingProfile["storageAccountType"]
| extend scriptBehaviorAfterReboot = publishingProfile["settings"]["scriptBehaviorAfterReboot"]
| extend packageFileName = publishingProfile["settings"]["packageFileName"]
| extend configFileName = publishingProfile["settings"]["configFileName"]
| extend mediaLink = publishingProfile["source"]["mediaLink"]
| extend defaultConfigurationLink = publishingProfile["source"]["defaultConfigurationLink"]
| extend excludeFromLatest = publishingProfile["excludeFromLatest"]
| extend targetRegions = publishingProfile["targetRegions"]
| extend replicaCount = publishingProfile["replicaCount"]
| extend publishedDate = publishingProfile["publishedDate"]
| extend installScript = publishingProfile["manageActions"]["install"]
| extend removeScript = publishingProfile["manageActions"]["remove"]
| extend safetyProfile = properties["safetyProfile"]
| extend allowDeletionOfReplicatedLocations = safetyProfile["allowDeletionOfReplicatedLocations"]
| project-away safetyProfile, publishingProfile, SubId, rgName, id, properties

更新已发布的 VM 应用程序

注释

不能更新已发布的 VM 应用程序或 VM 应用程序版本的所有属性。 有关可更新属性的完整列表,请参阅 VM 应用程序资源和 VM 应用程序版本资源架构

更新 VM 应用程序资源:

PATCH
https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}/applications/{galleryApplicationName}?api-version=2024-03-03

Body
{
    "properties": {
        "description": "Updated description",
        "endOfLifeDate": "2026-12-31T00:00:00Z",
        "eula": "Updated EULA text",
        "privacyStatementUri": "https://contoso.com/privacy",
        "releaseNoteUri": "https://contoso.com/release-notes"
    }
}

更新 VM 应用程序版本资源:

PATCH
https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}/applications/{galleryApplicationName}/versions/{galleryApplicationVersionName}?api-version=2024-03-03

Body
{
    "properties": {
        "publishingProfile": {
            "targetRegions": [
                {
                    "name": "chinanorth2",
                    "regionalReplicaCount": 2,
                    "storageAccountType": "Standard_LRS"
                },
                {
                    "name": "chinanorth2",
                    "regionalReplicaCount": 1,
                    "storageAccountType": "Standard_LRS"
                }
            ],
            "excludeFromLatest": false
        }
    }
}

若要删除 VM 应用程序资源,首先需要删除其所有版本。 删除应用程序版本会导致从 Azure 计算库及其所有副本中删除应用程序版本资源。 用于创建应用程序版本的存储帐户中的应用程序 Blob 不受影响。

警告

  • 删除应用程序版本会导致使用该版本的 VM 上的后续 PUT 操作失败。 若要防止此失败,请使用 latest 关键字作为版本号中的 applicationProfile 版本号,而不是对版本号进行硬编码。

  • 删除部署在任何 VM 或虚拟机规模集上的 VM 应用程序会导致这些资源的后续 PUT 操作失败(例如,更新、缩放或重置镜像)。 在删除之前,请确保所有 VM/Virtual Machine Scale Sets 实例停止使用应用程序,方法是将其从其 applicationProfile 中删除。

  • 若要防止意外删除,请在发布版本时设置为safetyProfile/allowDeletionOfReplicatedLocationsfalse VM 应用程序资源上应用 Azure 资源管理器锁(CanNotDelete 或 ReadOnly)。

  1. 登录到 Azure 门户
  2. 搜索 Azure Compute Gallery 并打开目标库。
  3. 选择要删除的 VM 应用程序。
  4. 选择要删除的一个或多个版本。
  5. 若要删除 VM 应用程序,请先删除所有版本。 然后点击“删除”(在界面顶部)。
  6. 监控通知以检查完成情况。 如果删除被阻止,请删除任何锁,并确保没有虚拟机(VM)或虚拟机规模集引用该应用程序。

显示在 Azure 门户中删除 VM 应用程序及其版本的屏幕截图。

在 Azure VM 和 Virtual Machine Scale Sets 上管理已部署的 VM 应用程序

本部分介绍如何查看已部署的应用程序详细信息,以及如何跨基础结构监视已部署的应用程序。 它还介绍了如何在 Azure VM 和 Virtual Machine Scale Sets 上更新和删除 部署的 VM 应用程序

查看已部署的 VM 应用程序及其状态

Azure使用 VMAppExtension 在 VM 上部署、监视和管理 VM 应用程序。 因此,部署的 VM 应用程序的预配状态在 VMAppExtension 的状态中进行了说明。

若要显示 VM 应用程序状态,请转到设置下的“ 扩展 + 应用程序 ”选项卡,并检查 VMAppExtension 的状态:

显示 VM 应用程序状态的屏幕截图。

若要显示Virtual Machine Scale Sets的 VM 应用程序状态,请转到Azure门户Virtual Machine Scale Sets页。 在“实例”部分中,选择其中一个实例。 然后转到“设置”下的“ 扩展 + 应用程序 ”选项卡,并检查 VMAppExtension 的状态:

屏幕截图显示“Virtual Machine Scale Sets”应用程序状态。

使用“运行”命令查看应用程序安装的日志

Azure VM 应用程序在 Azure VM 或 Virtual Machine Scale Sets 上下载并安装应用程序时,它会通过管道将所有 stdout 结果传递给应用程序存储库中的 stdout 文件。 客户可以使用installScript启用应用程序安装的详细日志记录并编写自定义日志。 然后,客户可以手动检查 stdoutstderr 文件或使用 Runcommand 获取文件内容。

在托管运行命令中使用以下 PowerShell 脚本。 更新应用程序中的appName变量和appVersion变量

$appName = "vm-application-name"        # VM Application definition name
$appVersion = "1.0.0"                   # VM Application version name

$VMAppManagerVersion = "1.0.16"         # Version of the VMApplicationManagerWindows extension on the VM
$StdoutFilePath = "C:\Packages\Plugins\Microsoft.CPlat.Core.VMApplicationManagerWindows\$VMAppManagerVersion\Downloads\$appName\$appVersion\stdout"
$StderrFilePath = "C:\Packages\Plugins\Microsoft.CPlat.Core.VMApplicationManagerWindows\$VMAppManagerVersion\Downloads\$appName\$appVersion\stderr"

Write-Host "`n=== Contents of stdout ==="
Get-Content -Path $StdoutFilePath

Write-Host "`n=== Contents of stderr ==="
Get-Content -Path $StderrFilePath

使用脚本执行 run 命令并获取应用程序安装日志。

  1. 打开 Azure 门户并导航到 VM。
  2. 操作 下,选择 运行命令
  3. 从列表中选择 RunPowerShellScript
  4. 在编辑器中输入 PowerShell 脚本。
  5. 选择 “运行” 以执行脚本。
  6. “输出 ”部分中查看输出。 如果脚本失败,请查看 “错误” 部分了解详细信息。

有关详细信息,请参阅使用“运行命令”在 Windows VM 中运行 PowerShell 脚本

使用Azure Resource Graph查看所有已部署的 VM 应用程序

Azure资源图查询可用于查看所有已部署的VM应用程序及其在所有虚拟机与虚拟机规模集上的属性。 它提供了一种编程方式,用于大规模查看应用程序清单、状态和部署的版本。 使用此方法与仪表板和自定义报表集成。

resources
| where type == "microsoft.compute/virtualmachines" or type == "microsoft.compute/virtualmachinescalesets"
| where properties has "applicationProfile"
| extend resourceType = iff(type == "microsoft.compute/virtualmachines", "VM", "VMSS")
| extend applications = iff(resourceType == "VM", parse_json(properties["applicationProfile"]["galleryApplications"]), parse_json(properties["virtualMachineProfile"]["applicationProfile"]["galleryApplications"]))
| mv-expand applications
| extend enableAutomaticUpgrade = applications["enableAutomaticUpgrade"]
| extend packageReferenceId = applications["packageReferenceId"]
| extend treatFailureAsDeploymentFailure = applications["treatFailureAsDeploymentFailure"]
| parse packageReferenceId with "/subscriptions/" publisherSubcriptionId "/resourceGroups/" publisherResourceGroup "/providers/Microsoft.Compute/galleries/" galleryName "/applications/" appName "/versions/" version
| project tenantId, subscriptionId, resourceGroup, resourceName = name, type, location, appName, version, enableAutomaticUpgrade, treatFailureAsDeploymentFailure, galleryName, publisherSubcriptionId, publisherResourceGroup, properties

使用 Azure Policy 审核所需的 VM 应用程序

Azure Policy通过审核所需的 VM 应用程序是否在你的 VM 和虚拟机规模集上部署,来帮助实施治理。 可以创建和分配自定义策略来检查符合性并确保基础结构上存在特定应用程序。

有关如何使用 Azure Policy 审核 VM 应用程序部署的分步说明,请参阅使用 Azure Policy 审核所需的 VM 应用程序。

更新部署的 VM 应用程序

若要更新已部署的 VM 应用程序,请修改 applicationProfile 以引用较新版本或更改部署设置,例如 treatFailureAsDeploymentFailureorder

更新单个 VM 上的 VM 应用程序版本或设置:

PATCH
https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}?api-version=2024-03-03

Body
{
    "properties": {
        "applicationProfile": {
            "galleryApplications": [
                {
                    "packageReferenceId": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}/applications/{applicationName}/versions/{newVersion}",
                    "treatFailureAsDeploymentFailure": true,
                    "enableAutomaticUpgrade": true
                }
            ]
        }
    }
}

在虚拟机规模集(模型)上更新 VM 应用程序:

PATCH
https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachineScaleSets/{vmssName}?api-version=2024-03-03

Body
{
    "properties": {
        "virtualMachineProfile": {
            "applicationProfile": {
                "galleryApplications": [
                    {
                        "packageReferenceId": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}/applications/{applicationName}/versions/{newVersion | latest}",
                        "treatFailureAsDeploymentFailure": true,
                        "order": 2
                    }
                ]
            }
        }
    }
}

将更改应用于现有的虚拟机规模集实例(当 upgradePolicy.mode 为手动时需要):

POST
https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachineScaleSets/{vmssName}/updateInstances?api-version=2024-03-03

Body
{
    "instanceIds": ["*"]
}

小窍门

latest 用作版本标识符,在 packageReferenceId 中自动部署最新发布的版本,而无需手动更新部署。

从Azure VM 或Virtual Machine Scale Sets中删除 VM 应用程序

  1. 打开 Azure 门户,转到目标虚拟机(VM)或虚拟机规模集。
  2. 在“设置”中,选择“ 扩展 + 应用程序”,然后选择“ VM 应用程序 ”选项卡。
  3. 单击 VM 应用程序上的“卸载”按钮并保存。
  4. 在“通知”中跟踪进度,或检查 VMAppExtension 状态的实例视图。

显示如何从虚拟机中卸载虚拟机应用程序的屏幕截图。

后续步骤