转到 Azure 门户 ,然后搜索并选择“Azure Compute Gallery”。
从列表中选择需要使用的库。
在库页面上,选择页面顶部的“添加”,然后从下拉列表中选择“VM 应用程序定义”。 此时会打开“创建 VM 应用程序定义 ”页。
在“基本信息”选项卡中输入应用程序的名称,然后选择该应用程序是用于运行 Linux 还是 Windows 的 VM。
如果要为 VM 应用程序定义指定以下任意可选设置,请选择“发布选项” 选项卡:
VM 应用程序定义的说明。
生命周期终结日期
EULA 的链接
隐私声明的 URI
发行说明的 URI
完成操作后,选择“查看 + 创建”。
验证完成后,选择“创建”以部署定义。
部署完成后,选择“转到资源”。
在应用程序页面上,选择“创建 VM 应用程序版本”。 此时会打开“创建 VM 应用程序版本 ”页。
输入版本号,例如 1.0.0。
选择已上传应用程序包的区域。
在“源应用程序包”下,选择“浏览”。 选择存储帐户,然后选择包所在的容器。 从列表中选择包,完成后再选择选择 。 或者,如果需要,可以将 SAS URI 粘贴到此字段中。
键入“安装脚本”。 还可以提供“卸载脚本”和“更新脚本”。 有关如何创建脚本的信息,请参阅概述 。
如果已将默认配置文件上传到存储帐户,可以在“默认配置”中选择它。
如果在创建 VM 时不希望该版本显示为最新版本,请选择“从最新版本中排除”。
对于“生命周期结束日期”,选择一个将来的日期来跟踪该版本应该何时停用。 它不会自动删除或移除,仅用于你自己的跟踪。
若要将此版本复制到其他区域,请选择“复制”选项卡并添加更多区域,然后更改每个区域的副本数。 创建版本的原始区域必须在列表中,并且不能删除。
完成更改后,在页面底部选择“审阅并创建”。
当验证结果显示为通过时,选择“创建”来部署你的 VM 应用程序版本。
现在,可以使用门户创建一个 VM 并将 VM 应用程序部署到其中。 只需像往常一样创建 VM,然后在“高级”选项卡下,选择“选择要安装的 VM 应用程序” 。
从列表中选择 VM 应用程序,然后选择页面底部的“保存”。
如果要安装多个 VM 应用程序,可以返回“高级”选项卡并设置每个 VM 应用程序的安装顺序 。
还可以将 VM 应用程序部署到当前正在运行的 VM。 在门户中查看 VM 详细信息时,在左侧菜单中的“设置”下选择“扩展 + 应用程序”选项。
选择“VM 应用程序”,然后选择“添加应用程序”以添加 VM 应用程序。
从列表中选择 VM 应用程序,然后选择页面底部的“保存”。
若要显示 VM 应用程序状态,请转到“扩展 + 应用程序”选项卡/设置,然后查看 VMAppExtension 的状态:
若要显示 VMSS 的 VM 应用程序状态,请转到 VMSS 页中的“实例”,选择其中一个实例,然后转到 VMAppExtension:
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 "China East"
使用 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 "China East" \
--resource-group myResourceGroup \
--package-file-link "https://<storage account name>.blob.core.chinacloudapi.cn/<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.chinacloudapi.cn/<container name>/<filename>"\
使用 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
若要将应用程序添加到 VMSS,请使用 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
若要将多个应用程序添加到 VMSS,请执行以下操作:
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 部署状态,请使用 az vm get-instance-view :
az vm get-instance-view -g myResourceGroup -n myVM --query "instanceView.extensions[?name == 'VMAppExtension']"
若要验证应用程序 VMSS 部署状态,请使用 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']"
备注
上述 VMSS 部署状态命令未列出结果的实例 ID。 若要显示每个实例中扩展状态的实例 ID,需要一些额外的脚本。 请参阅以下包含 PowerShell 语法的 VMSS 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']"
}
使用 New-AzGalleryApplication
创建 VM 应用程序定义。 在此示例中,我们将在 myGallery Azure Compute Gallery 中的 myGallery 资源组内创建名为 myApp 的 Linux 应用。我已提供 VM 应用程序的简短说明供我自己使用。 请根据需要替换值。
$galleryName = "myGallery"
$rgName = "myResourceGroup"
$applicationName = "myApp"
New-AzGalleryApplication `
-ResourceGroupName $rgName `
-GalleryName $galleryName `
-Location "China East" `
-Name $applicationName `
-SupportedOSType Linux `
-Description "Backend Linux application for finance."
使用 New-AzGalleryApplicationVersion
创建应用程序的版本。 允许用于版本的字符为数字和句点。 数字必须在 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.chinacloudapi.cn/<container name>/<filename>" `
-DefaultConfigFileLink "https://<storage account name>.blob.core.chinacloudapi.cn/<container name>/<filename>" `
-Location "China East" `
-Install "mv myApp .\myApp\myApp" `
-Remove "rm .\myApp\myApp" `
若要将应用程序添加到现有 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:
$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
验证应用程序成功:
$rgName = "myResourceGroup"
$vmName = "myVM"
$result = Get-AzVM -ResourceGroupName $rgName -VMName $vmName -Status
$result.Extensions | Where-Object {$_.Name -eq "VMAppExtension"} | ConvertTo-Json
验证 VMSS:
$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
创建应用程序定义。
PUT
/subscriptions/\<**subscriptionId**\>/resourceGroups/\<**resourceGroupName**\>/providers/Microsoft.Compute/galleries/\<**galleryName**\>/applications/\<**applicationName**\>?api-version=2019-03-01
{
"location": "China North",
"name": "myApp",
"properties": {
"supportedOSType": "Windows | Linux",
"endOfLifeDate": "2020-01-01"
}
}
展开表
字段名称
说明
限制
name
库中 VM 应用程序的唯一名称
最大长度为 117 个字符。 允许的字符为大写或小写字母、数字、连字符 (-)、句点 (.)、下划线 (_)。 名称不能以句点 (.) 结尾。
supportedOSType
这是 Windows 还是 Linux 应用程序
“Windows”或“Linux”
endOfLifeDate
应用程序的未来生命周期结束日期。 请注意,这仅供参考,不是强制性的。
有效的未来日期
创建 VM 应用程序版本。
PUT
/subscriptions/\<**subscriptionId**\>/resourceGroups/\<**resourceGroupName**\>/providers/Microsoft.Compute/galleries/\<**galleryName**\>/applications/\<**applicationName**\>/versions/\<**versionName**\>?api-version=2019-03-01
{
"location": "$location",
"properties": {
"publishingProfile": {
"source": {
"mediaLink": "$mediaLink",
"defaultConfigurationLink": "$configLink"
},
"manageActions": {
"install": "echo installed",
"remove": "echo removed",
"update": "echo update"
},
"targetRegions": [
{
"name": "$location1",
"regionalReplicaCount": 1
},
{ "name": "$location1" }
]
},
"endofLifeDate": "datetime",
"excludeFromLatest": "true | false"
}
}
展开表
字段名称
说明
限制
location
VM 应用程序版本的源位置
有效的 Azure 区域
mediaLink
包含应用程序版本包的 URL
现有的有效存储 URL
defaultConfigurationLink
可选。 包含默认配置的 URL,在部署时可替代。
现有的有效存储 URL
安装
用于安装应用程序的命令
适用于给定 OS 的有效命令
删除
用于删除应用程序的命令
适用于给定 OS 的有效命令
更新
可选。 用于更新应用程序的命令。 如果未指定并且需要更新,则会删除旧版本并安装新的版本。
适用于给定 OS 的有效命令
targetRegions/name
要复制到的区域的名称
验证 Azure 区域
targetRegions/regionalReplicaCount
可选。 要创建的区域中的副本数。 默认值为 1。
介于 1 和 3 之间(含限值)的整数
endOfLifeDate
应用程序版本的未来生命周期结束日期。 请注意,这仅供客户参考,不是强制性的。
有效的未来日期
excludeFromLatest
如果指定此字段,则不会将此版本视为最新版本。
True 或 False
若要将 VM 应用程序版本添加到某个 VM,请对该 VM 执行 PUT。
PUT
/subscriptions/\<**subscriptionId**\>/resourceGroups/\<**resourceGroupName**\>/providers/Microsoft.Compute/virtualMachines/\<**VMName**\>?api-version=2019-03-01
{
"properties": {
"applicationProfile": {
"galleryApplications": [
{
"order": 1,
"packageReferenceId": "/subscriptions/{subscriptionId}/resourceGroups/<resource group>/providers/Microsoft.Compute/galleries/{gallery name}/applications/{application name}/versions/{version}",
"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=2019-03-01
{
"properties": {
"virtualMachineProfile": {
"applicationProfile": {
"galleryApplications": [
{
"order": 1,
"packageReferenceId": "/subscriptions/{subscriptionId}/resourceGroups/<resource group>/providers/Microsoft.Compute/galleries/{gallery name}/applications/{application name}/versions/{version}",
"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}"
}
展开表
字段名称
说明
限制
顺序
可选。 应用程序的部署顺序。 请参阅下文。
验证整数
packageReferenceId
对库应用程序版本的引用
有效的应用程序版本引用
configurationReference
可选。 包含此部署的配置的存储 Blob 的完整 URL。 这会替代之前为 defaultConfiguration 提供的任何值。
有效的存储 Blob 引用
treatFailureAsDeploymentFailure
可选。 VM 应用的预配状态。 若设置为 false,则即使应用部署失败,预配状态也始终显示“成功”。
是或否
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": "chinaeast",
"properties": "@{autoUpgradeMinorVersion=True; forceUpdateTag=7c4223fc-f4ea-4179-ada8-c8a85a1399f5; provisioningState=Creating; publisher=Microsoft.CPlat.Core; type=VMApplicationManagerLinux; typeHandlerVersion=1.0; settings=}"
}
]
}
如果 VM 上尚未安装 VM 应用程序,则该值为空。
若要获取 VM 实例视图的结果,请执行以下操作:
GET
/subscriptions/\<**subscriptionId**\>/resourceGroups/\<**resourceGroupName**\>/providers/Microsoft.Compute/virtualMachines/\<**VMName**\>/instanceView?api-version=2019-03-01
结果类似以下形式:
{
...
"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 应用扩展结果的状态消息中。
若要获取 VMSS 应用程序的状态,请执行以下操作:
GET
/subscriptions/\<**subscriptionId**\>/resourceGroups/\<**resourceGroupName**\>/providers/Microsoft.Compute/ virtualMachineScaleSets/\<**VMSSName**\>/virtualMachines/<**instanceId**>/instanceView?api-version=2019-03-01
输出类似于前面的 VM 示例。