你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
注意
本文档中列出的许多步骤都适用于使用统一业务流程模式的虚拟机规模集。 建议对新工作负载使用灵活业务流程。 有关详细信息,请参阅 Azure 中虚拟机规模集的业务流程模式。
在应用程序的整个生命周期内,你可能需要修改或更新你的虚拟机规模集。 这些更新可能包括更新规模集的配置,或更改应用程序配置。 本文介绍了如何使用 REST API、Azure PowerShell 或 Azure CLI 修改现有规模集。
基本概念
规模集模型
规模集有一个“规模集模型”,用于描述整个规模集的预期状态。 若要查询规模集的模型,可使用以下命令:
如下所示通过 REST API 使用 compute/virtualmachinescalesets/get:
GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSet?api-version={apiVersion}使用 Azure PowerShell 和 Get-AzVmss:
Get-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"Azure CLI 中的 az vmss show:
az vmss show --resource-group myResourceGroup --name myScaleSet还可以使用 resources.azure.com 或特定语言的 Azure SDK。
输出的具体呈现取决于提供给命令的选项。 下面的示例显示了来自 Azure CLI 的精简版示例输出:
az vmss show --resource-group myResourceGroup --name myScaleSet
{
"location": "westus",
"overprovision": true,
"plan": null,
"singlePlacementGroup": true,
"sku": {
"additionalProperties": {},
"capacity": 1,
"name": "Standard_D2_v2",
"tier": "Standard"
},
}
这些属性作为一个整体应用到规模集。
规模集实例视图
规模集还有一个“规模集实例视图”,用于从整体上反映该规模集当前的运行时状态。 若要查询规模集的实例视图,可使用以下命令:
如下所示通过 REST API 使用 compute/virtualmachinescalesets/getinstanceview:
GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSet/instanceView?api-version={apiVersion}使用 Azure PowerShell 和 Get-AzVmss:
Get-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceView通过 Azure CLI 使用 az vmss get-instance-view:
az vmss get-instance-view --resource-group myResourceGroup --name myScaleSet您还可以使用 resources.azure.com 或特定语言的 Azure SDK
输出的具体呈现取决于提供给命令的选项。 下面的示例显示了来自 Azure CLI 的精简版示例输出:
$ az vmss get-instance-view --resource-group myResourceGroup --name myScaleSet
{
"statuses": [
{
"additionalProperties": {},
"code": "ProvisioningState/succeeded",
"displayStatus": "Provisioning succeeded",
"level": "Info",
"message": null,
"time": "{time}"
}
],
"virtualMachine": {
"additionalProperties": {},
"statusesSummary": [
{
"additionalProperties": {},
"code": "ProvisioningState/succeeded",
"count": 1
}
]
}
}
这些属性汇总了规模集中 VM 的当前运行时状态,例如应用于规模集的扩展的状态。
规模集 VM 模型视图
规模集中的每个 VM 实例都有自己的模型视图,这类似于每个规模集都有模型视图的情况。 若要查询规模集中特定 VM 实例的模型视图,可使用以下命令:
如下所示使用 compute/virtualmachinescalesetvms/get 的 REST API:
GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSet/virtualmachines/instanceId?api-version={apiVersion}通过 Azure PowerShell 使用 Get-AzVmssVm:
Get-AzVmssVm -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId instanceIdAzure CLI 中的 az vmss show:
az vmss show --resource-group myResourceGroup --name myScaleSet --instance-id instanceId你还可以使用 resources.azure.com 或 Azure SDK。
输出的具体呈现取决于提供给命令的选项。 下面的示例显示了来自 Azure CLI 的精简版示例输出:
$ az vmss show --resource-group myResourceGroup --name myScaleSet
{
"location": "westus",
"name": "{name}",
"sku": {
"name": "Standard_D2_v2",
"tier": "Standard"
},
}
这些属性描述的是规模集中某个 VM 实例的配置,而不是规模集作为一个整体的配置。 例如,规模集模型将 overprovision 作为一个属性,而规模集中的 VM 实例模型则没有该属性。 之所以存在这种差异,是因为过度预配是整个规模集的属性,而不是规模集中单个虚拟机实例的属性(有关过度预配的详细信息,请参阅 规模集的设计注意事项)。
规模集 VM 实例视图
规模集中的每个 VM 实例都有自己的实例视图,这类似于每个规模集都有实例视图的情况。 若要查询规模集中特定 VM 实例的实例视图,可使用以下命令:
通过 REST API 使用 compute/virtualmachinescalesetvms/getinstanceview:
GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSet/virtualmachines/instanceId/instanceView?api-version={apiVersion}通过 Azure PowerShell 使用 Get-AzVmssVm:
Get-AzVmssVm -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId instanceId -InstanceView通过 Azure CLI 使用 az vmss get-instance-view
az vmss get-instance-view --resource-group myResourceGroup --name myScaleSet --instance-id instanceId还可以使用 resources.azure.com 或 Azure SDK
输出的具体呈现取决于提供给命令的选项。 下面的示例显示了来自 Azure CLI 的精简版示例输出:
$ az vmss get-instance-view --resource-group myResourceGroup --name myScaleSet --instance-id instanceId
{
"additionalProperties": {
"osName": "ubuntu",
"osVersion": "16.04"
},
"disks": [
{
"name": "{name}",
"statuses": [
{
"additionalProperties": {},
"code": "ProvisioningState/succeeded",
"displayStatus": "Provisioning succeeded",
"time": "{time}"
}
]
}
],
"statuses": [
{
"additionalProperties": {},
"code": "ProvisioningState/succeeded",
"displayStatus": "Provisioning succeeded",
"time": "{time}"
},
{
"additionalProperties": {},
"code": "PowerState/running",
"displayStatus": "VM running"
}
],
"vmAgent": {
"statuses": [
{
"additionalProperties": {},
"code": "ProvisioningState/succeeded",
"displayStatus": "Ready",
"level": "Info",
"message": "Guest Agent is running",
"time": "{time}"
}
],
"vmAgentVersion": "{version}"
},
}
这些属性描述的是规模集中 VM 实例的当前运行时状态,包括应用于规模集的任何扩展。
如何更新全局缩放集属性
若要更新某个全局规模集属性,必须在规模集模型中更新该属性。 可通过以下方式进行该更新:
如下所示使用 compute/virtualmachinescalesets/createorupdate 的 REST API:
PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSet?api-version={apiVersion}可以使用 REST API 中的属性部署资源管理器模板,以便更新全局规模集属性。
Azure PowerShell 与 Update-AzVmss 配合使用:
Update-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -VirtualMachineScaleSet {scaleSetConfigPowershellObject}使用 az vmss update 的 Azure CLI:
若要修改属性:
az vmss update --set {propertyPath}={value}向规模集中的列表属性添加对象:
az vmss update --add {propertyPath} {JSONObjectToAdd}若要从缩放集中的列表属性中移除对象:
az vmss update --remove {propertyPath} {indexToRemove}如果之前已使用
az vmss create命令部署了规模集,则可再次运行az vmss create命令来更新规模集。 请确保az vmss create命令中的所有属性都与之前保持一致,除了您希望修改的属性外。
你还可以使用 resources.azure.com 或 Azure SDK。
更新规模集模型以后,新配置就会应用到在规模集中创建的新 VM。 但是,规模集中现有 VM 的模型仍需使用总体说来最新的规模集模型进行更新。 在每个 VM 的模型中,都有一个名为 latestModelApplied 的布尔属性,用于指示该 VM 是否与最新的整个规模集模型保持一致(true 表示该 VM 是最新模型的)。
对修改有限制的属性
创建时属性
某些属性只能在创建规模集时设置。 一些例子包括托管 OS 磁盘存储帐户类型和容错域。
只能在当前值的基础上更改的属性
某些属性可以更改,但也有例外,具体取决于当前值。 这些属性包括:
- singlePlacementGroup
- 子网
- imageReferenceSku
- imageReferenceOffer
- zones
示例 1
若要将规模集更新为使用不同的 OS 版本,需要在一次调用中设置所有更新的属性。 在此示例中,我们将从 Ubuntu Server 20.04 更改为 22.04。
az vmss update \
--resource-group myResourceGroup \
--name myScaleSet \
--set virtualMachineProfile.storageProfile.imageReference.offer=0001-com-ubuntu-server-jammy \
--set virtualMachineProfile.storageProfile.imageReference.publisher=Canonical \
--set virtualMachineProfile.storageProfile.imageReference.sku=22_04-lts-gen2 \
--set virtualMachineProfile.storageProfile.imageReference.version=latest
示例 2
若要将规模集更新为使用不同的 OS 版本,需要在一次调用中设置所有更新的属性。 在此示例中,将从 Windows Server 2016 更改为 Windows Server 2019。
$VMSS = Get-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"
Set-AzVmssStorageProfile $vmss `
-OsDiskCreateOption "FromImage" `
-ImageReferencePublisher "MicrosoftWindowsServer" `
-ImageReferenceOffer "WindowsServer" `
-ImageReferenceSku "2019-datacenter" `
-ImageReferenceVersion "latest"
Update-AzVmss -ResourceGroupName "myResourceGroup" -Name "myScaleSet" -VirtualMachineScaleSet $VMSS
需要解除分配才能更改的属性
某些属性只有在规模集中的 VM 已解除分配的情况下,才能更改为特定值。 这些属性包括:
- SKU 名称 - 如果新 VM SKU 在规模集当前所在的硬件上不受支持,则需先将规模集中的 VM 解除分配,然后才能修改 SKU 名称。 有关详细信息,请参阅 如何调整 Azure VM 的大小。
场景
应用程序更新
如果应用程序已通过扩展部署到规模集,则对扩展配置的更新将导致应用程序按照升级策略进行更新。 例如,如果新版脚本在自定义脚本扩展中运行,则可更新 fileUris 属性,使之指向新脚本。 在某些情况下,可能需要强制进行更新,即使扩展配置未更改(例如,在未更改脚本 URI 的情况下更新脚本)。 在这些情况下,可以通过修改 forceUpdateTag 来强制进行更新。 Azure 平台不解释此属性。 如果更改此值,不会影响扩展的运行方式。 更改会强制扩展程序重新运行。 有关 forceUpdateTag 的更多信息,请参阅扩展的 REST API 文档。 请注意,forceUpdateTag 可用于所有扩展,而不仅仅是自定义脚本扩展。
通过自定义映像来部署应用程序也很常见。 以下部分介绍此情景。
操作系统更新
如果您使用 Azure 平台映像,可以通过修改 imageReference 来更新映像(有关详细信息,请参阅 REST API 文档)。
注意
使用平台映像时,通常指定 "latest" 作为映像引用版本。 在创建、横向扩展和重新映像时,虚拟机将使用最新可用版本创建。 但是,这并不意味着操作系统映像会随着时间推移在新映像版本发布后自动更新。 一个独立功能提供了自动 OS 升级功能。 有关更多信息,请参阅 自动 OS 升级文档。
如果使用自定义映像,则可以通过更新 imageReference ID 来更新该映像(更多信息,请参阅 REST API 文档)。
示例
更新规模集的 OS 映像
你可能具有运行旧版 Ubuntu LTS 16.04 的规模集。 你希望将其更新到较新的 Ubuntu LTS 16.04 版本,例如版本 16.04.201801090。 映像引用版本属性不是列表的一部分,因此可以使用下列命令之一直接修改这些属性:
如下所示,通过 Azure PowerShell 使用 Update-AzVmss:
Update-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -ImageReferenceVersion 16.04.201801090使用 az vmss update 的 Azure CLI:
az vmss update --resource-group myResourceGroup --name myScaleSet --set virtualMachineProfile.storageProfile.imageReference.version=16.04.201801090
或者,你可能想要更改规模集使用的映像。 例如,你可能想要更新或更改规模集使用的自定义映像。 可以通过更新“映像引用 ID”属性来更改规模集使用的映像。 “映像引用 ID”属性不是列表的一部分,因此可以使用下列命令之一直接修改该属性:
如下所示,通过 Azure PowerShell 使用 Update-AzVmss:
Update-AzVmss ` -ResourceGroupName "myResourceGroup" ` -VMScaleSetName "myScaleSet" ` -ImageReferenceId /subscriptions/{subscriptionID}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/images/myNewImage使用 az vmss update 的 Azure CLI:
az vmss update \ --resource-group myResourceGroup \ --name myScaleSet \ --set virtualMachineProfile.storageProfile.imageReference.id=/subscriptions/{subscriptionID}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/images/myNewImage
更新缩放集的负载均衡器
假设你有一个使用 Azure 负载均衡器的规模集,并且你想要将 Azure 负载均衡器替换为 Azure 应用程序网关。 规模集的负载均衡器和应用程序网关属性是列表的一部分,因此可以使用以下命令来删除或添加列表元素,而不必直接修改属性:
Azure PowerShell:
# Get the current model of the scale set and store it in a local PowerShell object named $vmss $vmss=Get-AzVmss -ResourceGroupName "myResourceGroup" -Name "myScaleSet" # Create a local PowerShell object for the new desired IP configuration, which includes the reference to the application gateway $ipconf = New-AzVmssIPConfig -ApplicationGatewayBackendAddressPoolsId /subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.Network/applicationGateways/{applicationGatewayName}/backendAddressPools/{applicationGatewayBackendAddressPoolName} -SubnetId $vmss.VirtualMachineProfile.NetworkProfile.NetworkInterfaceConfigurations[0].IpConfigurations[0].Subnet.Id -Name $vmss.VirtualMachineProfile.NetworkProfile.NetworkInterfaceConfigurations[0].IpConfigurations[0].Name # Replace the existing IP configuration in the local PowerShell object (which contains the references to the current Azure Load Balancer) with the new IP configuration $vmss.VirtualMachineProfile.NetworkProfile.NetworkInterfaceConfigurations[0].IpConfigurations[0] = $ipconf # Update the model of the scale set with the new configuration in the local PowerShell object Update-AzVmss -ResourceGroupName "myResourceGroup" -Name "myScaleSet" -virtualMachineScaleSet $vmssAzure CLI:
# Remove the load balancer backend pool from the scale set model az vmss update --resource-group myResourceGroup --name myScaleSet --remove virtualMachineProfile.networkProfile.networkInterfaceConfigurations[0].ipConfigurations[0].loadBalancerBackendAddressPools 0 # Remove the load balancer backend pool from the scale set model; only necessary if you have NAT pools configured on the scale set az vmss update --resource-group myResourceGroup --name myScaleSet --remove virtualMachineProfile.networkProfile.networkInterfaceConfigurations[0].ipConfigurations[0].loadBalancerInboundNatPools 0 # Add the application gateway backend pool to the scale set model az vmss update --resource-group myResourceGroup --name myScaleSet --add virtualMachineProfile.networkProfile.networkInterfaceConfigurations[0].ipConfigurations[0].ApplicationGatewayBackendAddressPools '{"id": "/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.Network/applicationGateways/{applicationGatewayName}/backendAddressPools/{applicationGatewayBackendPoolName}"}'
注意
这些命令假定规模集上只有一个 IP 配置和负载均衡器。 如果有多个,则可能需要使用 0 以外的列表索引。
后续步骤
还可以通过 Azure CLI 或 Azure PowerShell 对虚拟机规模集执行常见管理任务。