- 转到 Azure 门户,然后搜索并选择“Azure Compute Gallery”。
- 从列表中选择需要使用的库。
- 在库页面上,选择页面顶部的“添加”,然后从下拉列表中选择“VM 应用程序定义”。 此时会打开“创建 VM 应用程序定义”页。
- 在“基本信息”选项卡中输入应用程序的名称,然后选择该应用程序是用于运行 Linux 还是 Windows 的 VM。
- 如果要为 VM 应用程序定义指定以下任一可选设置,请选择“ 发布选项 ”选项卡:
- VM 应用程序定义的说明。
- 生命周期终结日期
- 指向最终用户许可协议(EULA)的链接
- 隐私声明的 URI
- 发行说明的 URI
- 完成操作后,选择“查看 + 创建”。
- 验证完成后,选择“创建”以部署定义。
- 部署完成后,选择“转到资源”。
- 在应用程序页面上,选择“创建 VM 应用程序版本”。 此时会打开“创建 VM 应用程序版本”页。
- 输入版本号,例如 1.0.0。
- 选择上传应用程序包的区域。
- 在“源应用程序包”下,选择“浏览”。 选择存储帐户,然后选择包所在的容器。 从列表中选择包,完成后再选择选择。 或者,如果需要,可以将 SAS URI 粘贴到此字段中。
- 提供“安装脚本”。 还可以提供“卸载脚本”和“更新脚本”。 有关如何创建脚本的信息,请参阅概述。
- 如果已将默认配置文件上传到存储帐户,可以在“默认配置”中选择它。
- 如果在创建 VM 时不希望该版本显示为最新版本,请选择“从最新版本中排除”。
- 对于“生命周期结束日期”,选择一个将来的日期来跟踪该版本应该何时停用。 它不会自动删除或移除,仅用于你自己的跟踪。
- 若要将此版本复制到其他区域,请选择“ 复制 ”选项卡,添加更多区域,并更改每个区域的副本数。 创建版本的原始区域必须在列表中,并且不能删除。
- 完成更改后,在页面底部选择“审阅并创建”。
- 当验证结果显示为通过时,选择“创建”来部署你的 VM 应用程序版本。
使用 New-AzGalleryApplication
.. 创建 VM 应用程序定义。 在此示例中,我们将在 myGallery Azure 计算库和 myGallery 资源组中创建名为 myApp 的 Linux 应用。 根据需要替换变量的值。
$galleryName = "myGallery"
$rgName = "myResourceGroup"
$applicationName = "myApp"
$description = "Backend Linux application for finance."
New-AzGalleryApplication `
-ResourceGroupName $rgName `
-GalleryName $galleryName `
-Location "East US" `
-Name $applicationName `
-SupportedOSType Linux `
-Description $description
使用 New-AzGalleryApplicationVersion
创建您的 VM 应用程序版本。 允许用于版本的字符为数字和句点。 数字必须在 32 位整数范围内。 格式:MajorVersion.MinorVersion.Patch。
在此示例中,我们将创建版本号 1.0.0。 请根据需要替换变量的值。
$galleryName = "myGallery"
$rgName = "myResourceGroup"
$applicationName = "myApp"
$version = "1.0.0"
New-AzGalleryApplicationVersion `
-ResourceGroupName $rgName `
-GalleryName $galleryName `
-GalleryApplicationName $applicationName `
-Name $version `
-PackageFileLink "https://<storage account name>.blob.core.windows.net/<container name>/<filename>" `
-DefaultConfigFileLink "https://<storage account name>.blob.core.windows.net/<container name>/<filename>" `
-Location "East US" `
-Install "mv myApp .\myApp\myApp" `
-Remove "rm .\myApp\myApp" `
VM 应用程序需要 Azure CLI 版本 2.30.0 或更高版本。
使用 “az sig gallery-application create”创建 VM 应用程序定义。 在此示例中,我们将为基于 Linux 的 VM 创建名为 myApp 的 VM 应用程序定义。
az sig gallery-application create \
--application-name myApp \
--gallery-name myGallery \
--resource-group myResourceGroup \
--os-type Linux \
--___location "East US"
使用 “az sig gallery-application version create”创建 VM 应用程序版本。 允许用于版本的字符为数字和句点。 数字必须在 32 位整数范围内。 格式:MajorVersion.MinorVersion.Patch。
将参数的值替换为你自己的值。
az sig gallery-application version create \
--version-name 1.0.0 \
--application-name myApp \
--gallery-name myGallery \
--___location "East US" \
--resource-group myResourceGroup \
--package-file-link "https://<storage account name>.blob.core.windows.net/<container name>/<filename>" \
--install-command "mv myApp .\myApp\myApp" \
--remove-command "rm .\myApp\myApp" \
--update-command "mv myApp .\myApp\myApp" \
--default-configuration-file-link "https://<storage account name>.blob.core.windows.net/<container name>/<filename>"\
使用“创建画廊应用程序 API”创建 VM 应用程序定义
PUT
/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}/applications/{applicationName}?api-version=2024-03-03
{
"___location": "West US",
"name": "myApp",
"properties": {
"supportedOSType": "Windows | Linux",
"endOfLifeDate": "2020-01-01",
"description": "Description of the App",
"eula": "Link to End-User License Agreement (EULA)",
"privacyStatementUri": "Link to privacy statement for the application",
"releaseNoteUri": "Link to release notes for the application"
}
}
字段名称 |
说明 |
限制 |
姓名 |
画廊中的虚拟机 (VM) 应用程序的唯一名称。 |
最大长度为 117 个字符。 允许的字符为大写或小写字母、数字、连字符 (-)、句点 (.)、下划线 (_)。 名称不能以句点 (.) 结尾。 |
支持的操作系统类型 |
定义支持的 OS 类型。 |
“Windows”或“Linux” |
生命周期结束日期 |
应用程序的未来生命周期结束日期。 日期仅供参考,不会强制执行。 |
有效的未来日期 |
描述 |
可选。 应用程序的说明。 |
|
eula |
可选。 对 End-User 许可协议(EULA)的引用。 |
|
隐私声明URI |
可选。 对应用程序的隐私声明的引用。 |
|
releaseNoteUri |
可选。 请参考应用程序的发布说明。 |
|
使用 “创建库应用程序版本 API”创建 VM 应用程序版本。
PUT
/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}/applications/{applicationName}/versions/{versionName}?api-version=2024-03-03
{
"___location": "$___location",
"properties": {
"publishingProfile": {
"source": {
"mediaLink": "$mediaLink",
"defaultConfigurationLink": "$configLink"
},
"manageActions": {
"install": "echo installed",
"remove": "echo removed",
"update": "echo update"
},
"targetRegions": [
{
"name": "West US",
"regionalReplicaCount": 1
},
{
"name": "East US"
}
]
"endofLifeDate": "datetime",
"replicaCount": 1,
"excludeFromLatest": false,
"storageAccountType": "PremiumV2_LRS | Premium_LRS | Standard_LRS | Standard_ZRS"
"safetyProfile": {
"allowDeletionOfReplicatedLocations": false
}
"settings": {
"scriptBehaviorAfterReboot": "None | Rerun",
"configFileName": "$appConfigFileName",
"packageFileName": "$appPackageFileName"
}
}
}
字段名称 |
说明 |
限制 |
位置 |
VM 应用程序版本的源位置。 |
有效的 Azure 区域 |
mediaLink |
包含应用程序版本包的 URL。 |
现有的有效存储 URL |
defaultConfigurationLink |
可选。 包含默认配置的 URL,在部署时可替代。 |
现有的有效存储 URL |
安装 |
用于安装应用程序的命令。 |
适用于给定 OS 的有效命令 |
删除 |
用于删除应用程序的命令。 |
适用于给定 OS 的有效命令 |
更新 |
可选。 用于更新应用程序的命令。 如果未指定并且需要更新,则会删除旧版本并安装新的版本。 |
适用于给定 OS 的有效命令 |
目标区域/名称 (targetRegions/name) |
要复制到的区域的名称。 |
验证 Azure 区域 |
目标区域/区域副本数量 |
可选。 要创建的区域中的副本数。 默认值为 1。 |
介于 1 和 3 之间(含限值)的整数 |
replicaCount |
可选。 定义各个区域的副本数量。 如果未定义 regionalReplicaCount,则生效。 |
介于 1 和 3 之间(含限值)的整数 |
生命周期结束日期 |
应用程序版本的未来生命周期结束日期。 此属性仅供客户参考,不会强制实施。 |
有效的未来日期 |
存储账户类型 |
可选。 要在每个区域中用于存储应用程序包的存储帐户的类型。 默认值为“Standard_LRS”。 |
此属性不可更新。 |
允许删除复制位置 |
可选。 指示是否允许从复制区域删除此库映像版本。 |
|
设置/重启后的脚本行为 |
可选。 重启 VM 后安装、更新或删除画廊应用程序时要执行的动作。 |
|
settings/configFileName |
可选。 在 VM 上分配下载的包文件的名称。 如果未指定,则包文件的名称与画廊应用程序名称相同。 |
此限制为 4,096 个字符。 |
设置/packageFileName |
可选。 在 VM 上分配下载的配置文件的名称。 如果未指定,配置文件将命名为“图库应用程序名称 + '_config'”。 |
此限制为 4,096 个字符。 |
现在,可以使用门户创建一个 VM 并将 VM 应用程序部署到其中。 只需像往常一样创建 VM,然后在“高级”选项卡下,选择“选择要安装的 VM 应用程序” 。
从列表中选择 VM 应用程序,然后选择页面底部的 “保存 ”。
如果要安装多个 VM 应用程序,可以返回“高级”选项卡并设置每个 VM 应用程序的安装顺序。
还可以将 VM 应用程序部署到当前正在运行的 VM。 在门户中查看 VM 详细信息时,在左侧菜单中的“设置”下选择“扩展 + 应用程序”选项。
选择“VM 应用程序”,然后选择“添加应用程序”以添加 VM 应用程序。
从列表中选择 VM 应用程序,然后选择页面底部的 “保存 ”。
使用 “az vm application set” 将 VM 应用程序设置为现有 VM,并将参数的值替换为自己的值。
az vm application set \
--resource-group myResourceGroup \
--name myVM \
--app-version-ids /subscriptions/{subID}/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/galleries/myGallery/applications/myApp/versions/1.0.0 \
--treat-deployment-as-failure true
若要在 VM 上设置多个应用程序,请执行以下操作:
az vm application set \
--resource-group myResourceGroup \
--name myVM \
--app-version-ids /subscriptions/{subId}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/galleries/myGallery/applications/myApp/versions/1.0.0 /subscriptions/{subId}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/galleries/myGallery/applications/myApp2/versions/1.0.1 \
--treat-deployment-as-failure true true
若要将应用程序添加到虚拟机规模集,请使用 “az vmss application set”:
az vmss application set \
--resource-group myResourceGroup \
--name myVmss \
--app-version-ids /subscriptions/{subId}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/galleries/myGallery/applications/myApp/versions/1.0.0 \
--treat-deployment-as-failure true
将多个应用程序添加到虚拟机规模集:
az vmss application set \
--resource-group myResourceGroup \
--name myVmss
--app-version-ids /subscriptions/{subId}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/galleries/myGallery/applications/myApp/versions/1.0.0 /subscriptions/{subId}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/galleries/myGallery/applications/myApp2/versions/1.0.0 \
--treat-deployment-as-failure true
若要将应用程序添加到现有 VM,请获取应用程序版本并使用它来获取 VM 应用程序版本 ID。 使用该 ID 将应用程序添加到 VM 配置。
$galleryName = "myGallery"
$rgName = "myResourceGroup"
$applicationName = "myApp"
$version = "1.0.0"
$vmName = "myVM"
$vm = Get-AzVM -ResourceGroupName $rgname -Name $vmName
$appVersion = Get-AzGalleryApplicationVersion `
-GalleryApplicationName $applicationName `
-GalleryName $galleryName `
-Name $version `
-ResourceGroupName $rgName
$packageId = $appVersion.Id
$app = New-AzVmGalleryApplication -PackageReferenceId $packageId
Add-AzVmGalleryApplication -VM $vm -GalleryApplication $app -TreatFailureAsDeploymentFailure true
Update-AzVM -ResourceGroupName $rgName -VM $vm
将应用程序添加到虚拟机规模集群:
$vmss = Get-AzVmss -ResourceGroupName $rgname -Name $vmssName
$appVersion = Get-AzGalleryApplicationVersion `
-GalleryApplicationName $applicationName `
-GalleryName $galleryName `
-Name $version `
-ResourceGroupName $rgName
$packageId = $appVersion.Id
$app = New-AzVmssGalleryApplication -PackageReferenceId $packageId
Add-AzVmssGalleryApplication -VirtualMachineScaleSetVM $vmss.VirtualMachineProfile -GalleryApplication $app
Update-AzVmss -ResourceGroupName $rgName -VirtualMachineScaleSet $vmss -VMScaleSetName $vmssName
若要将 VM 应用程序版本添加到某个 VM,请对该 VM 执行 PUT。
PUT
/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{VMName}?api-version=2024-03-03
{
"properties": {
"applicationProfile": {
"galleryApplications": [
{
"order": 1,
"packageReferenceId": "/subscriptions/{subscriptionId}/resourceGroups/<resource group>/providers/Microsoft.Compute/galleries/{gallery name}/applications/{application name}/versions/{version | latest}",
"configurationReference": "{path to configuration storage blob}",
"treatFailureAsDeploymentFailure": false
}
]
}
},
"name": "{vm name}",
"id": "/subscriptions/{subscriptionId}/resourceGroups/{resource group}/providers/Microsoft.Compute/virtualMachines/{vm name}",
"___location": "{vm ___location}"
}
若要将 VM 应用程序应用于统一规模集,请运行以下代码:
PUT
/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachineScaleSets/{VMSSName}?api-version=2024-03-03
{
"properties": {
"virtualMachineProfile": {
"applicationProfile": {
"galleryApplications": [
{
"order": 1,
"packageReferenceId": "/subscriptions/{subscriptionId}/resourceGroups/<resource group>/providers/Microsoft.Compute/galleries/{gallery name}/applications/{application name}/versions/{version | latest}",
"configurationReference": "{path to configuration storage blob}",
"treatFailureAsDeploymentFailure": false
}
]
}
}
},
"name": "{vm name}",
"id": "/subscriptions/{subscriptionId}/resourceGroups/{resource group}/providers/Microsoft.Compute/virtualMachines/{vm name}",
"___location": "{vm ___location}"
}
字段名称 |
说明 |
限制 |
顺序 |
可选。 应用程序的部署顺序。 |
验证整数 |
包裹参考编号 |
图片库应用程序版本的参考。 对版本使用“latest”关键字可以自动安装最新的可用版本。 |
有效的应用程序版本引用 |
configurationReference |
可选。 包含此部署的配置的存储 Blob 的完整 URL。 这会替代前面为 defaultConfiguration 提供的任何值。 |
有效的存储 Blob 引用 |
treatFailureAsDeploymentFailure |
可选。 启用后,应用部署失败会导致 VM 预配状态报告失败状态。 |
是或否 |
Order 字段可用于指定应用程序之间的依赖关系。 订单规则如下所示:
案例 |
安装时的含义 |
失败时的含义 |
未指定顺序 |
先安装有序应用程序,再安装无序应用程序。 无法保证未排序的应用程序之间的安装顺序。 |
其他应用程序(无论是有序还是无序)安装失败不会影响无序应用程序的安装。 |
重复顺序值 |
与具有相同顺序的其他应用程序相比,应用程序按任意顺序安装。 同一订单的所有应用程序都会在顺序较低的应用程序之后且顺序较高的应用程序之前安装。 |
如果前面有较低顺序的应用程序未能安装,则不会安装具有此顺序的应用程序。 如果具有此顺序的任何应用程序无法安装,则不会安装具有更高顺序的应用程序。 |
提高顺序 |
应用程序将安装在订单较低的应用程序之后,并且在订单较高的应用程序之前。 |
如果之前一个优先级较低的应用程序安装失败,那么此应用程序将不会安装。 如果此应用程序无法安装,则不会安装具有更高顺序的应用程序。 |
响应包括完整的 VM 模型。 下面是相关部分。
{
"name": "{vm name}",
"id": "{vm id}",
"type": "Microsoft.Compute/virtualMachines",
"___location": "{vm ___location}",
"properties": {
"applicationProfile": {
"galleryApplications": ""
},
"provisioningState": "Updating"
},
"resources": [
{
"name": "VMAppExtension",
"id": "{extension id}",
"type": "Microsoft.Compute/virtualMachines/extensions",
"___location": "centraluseuap",
"properties": "@{autoUpgradeMinorVersion=True; forceUpdateTag=7c4223fc-f4ea-4179-ada8-c8a85a1399f5; provisioningState=Creating; publisher=Microsoft.CPlat.Core; type=VMApplicationManagerLinux; typeHandlerVersion=1.0; settings=}"
}
]
}
若要显示 VM 应用程序状态,请转到“扩展 + 应用程序”选项卡/设置,然后查看 VMAppExtension 的状态:
若要显示虚拟机规模集的 VM 应用程序状态,请转到 Azure 门户的虚拟机规模集页面,然后进入实例部分,选择列出的一个规模集,再转到 VMAppExtension:
若要验证 VM 上的应用程序部署状态,请使用 “az vm get-instance-view”:
az vm get-instance-view -g myResourceGroup -n myVM --query "instanceView.extensions[?name == 'VMAppExtension']"
若要验证虚拟机规模集上的应用程序部署状态,请使用 “az vmss get-instance-view”:
az vmss get-instance-view --ids (az vmss list-instances -g myResourceGroup -n myVmss --query "[*].id" -o tsv) --query "[*].extensions[?name == 'VMAppExtension']"
注意
上一个虚拟机规模集部署状态命令不列出具有结果的实例 ID。 若要显示每个实例中扩展状态的实例 ID,需要更多脚本。 请参阅以下包含 PowerShell 语法的 CLI 示例:
$ids = az vmss list-instances -g myResourceGroup -n myVmss --query "[*].{id: id, instanceId: instanceId}" | ConvertFrom-Json
$ids | Foreach-Object {
$iid = $_.instanceId
Write-Output "instanceId: $iid"
az vmss get-instance-view --ids $_.id --query "extensions[?name == 'VMAppExtension']"
}
验证应用程序成功:
$rgName = "myResourceGroup"
$vmName = "myVM"
$result = Get-AzVM -ResourceGroupName $rgName -VMName $vmName -Status
$result.Extensions | Where-Object {$_.Name -eq "VMAppExtension"} | ConvertTo-Json
若要在虚拟机规模集上进行核实,您可以执行以下操作:
$rgName = "myResourceGroup"
$vmssName = "myVMss"
$result = Get-AzVmssVM -ResourceGroupName $rgName -VMScaleSetName $vmssName -InstanceView
$resultSummary = New-Object System.Collections.ArrayList
$result | ForEach-Object {
$res = @{ instanceId = $_.InstanceId; vmappStatus = $_.InstanceView.Extensions | Where-Object {$_.Name -eq "VMAppExtension"}}
$resultSummary.Add($res) | Out-Null
}
$resultSummary | ConvertTo-Json -Depth 5
如果未在 VM 上安装 VM 应用程序,该值为空。
若要获取 VM 实例视图的结果,请执行以下操作:
GET
/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{VMName}/instanceView?api-version=2024-03-03
结果类似以下形式:
{
...
"extensions" [
...
{
"name": "VMAppExtension",
"type": "Microsoft.CPlat.Core.VMApplicationManagerLinux",
"typeHandlerVersion": "1.0.9",
"statuses": [
{
"code": "ProvisioningState/succeeded",
"level": "Info",
"displayStatus": "Provisioning succeeded",
"message": "Enable succeeded: {\n \"CurrentState\": [\n {\n \"applicationName\": \"doNothingLinux\",\n \"version\": \"1.0.0\",\n \"result\": \"Install SUCCESS\"\n },\n {
\n \"applicationName\": \"badapplinux\",\n \"version\": \"1.0.0\",\n \"result\": \"Install FAILED Error executing command \u0027exit 1\u0027: command terminated with exit status=1\"\n }\n ],\n \"ActionsPerformed\": []\n}
"
}
]
}
...
]
}
VM 应用状态处于实例视图中 VM 应用扩展结果的状态消息中。
若要获取虚拟机规模集上的应用程序的状态,请执行以下操作:
GET
/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/ virtualMachineScaleSets/{VMSSName}/virtualMachines/{instanceId}/instanceView?api-version=2019-03-01
输出类似于前面的 VM 示例。
若要删除 VM 应用程序资源,首先需要删除其所有版本。 删除应用程序版本会导致从 Azure 计算库及其所有副本中删除应用程序版本资源。 用于创建应用程序版本的存储帐户中的应用程序 Blob 不受影响。 删除应用程序版本后,如果任何 VM 都使用该版本,则对这些 VM 执行重新映像作将失败。 使用“latest”关键字作为“applicationProfile”中的版本号,而不是硬编码版本号来解决此故障。
但是,如果删除了应用程序,则 VM 在重置映像作期间会失败,因为 Azure 没有可用的版本可供安装。 需要更新 VM 配置文件才能不使用 VM 应用程序。
删除 VM 应用程序版本:
Remove-AzGalleryApplicationVersion -ResourceGroupName $rgNmae -GalleryName $galleryName -GalleryApplicationName $galleryApplicationName -Name $name
删除 VM 应用程序的所有版本后,删除该应用程序。
Remove-AzGalleryApplication -ResourceGroupName $rgNmae -GalleryName $galleryName -Name $name
删除 VM 应用程序版本:
az sig gallery-application version delete --resource-group $rg-name --gallery-name $gallery-name --application-name $app-name --version-name $version-name
删除 VM 应用程序的所有版本后,删除该应用程序。
az sig gallery-application delete --resource-group $rg-name --gallery-name $gallery-name --application-name $app-name
删除 VM 应用程序版本:
DELETE
https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}/applications/{galleryApplicationName}/versions/{galleryApplicationVersionName}?api-version=2024-03-03
删除 VM 应用程序的所有版本后,删除该应用程序。
DELETE
https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}/applications/{galleryApplicationName}?api-version=2024-03-03
后续步骤
详细了解 Azure VM 应用程序。