在应用程序的整个生命周期内,你可能需要修改或更新你的虚拟机规模集。 这些更新可能包括更新规模集的配置,或更改应用程序配置。 本文介绍如何使用 Azure CLI 修改现有的规模集。
下面,我们声明将在整个文档中使用的环境变量。 为了使每次部署的资源名称唯一,都会在其后面附加一个随机后缀。 REGION
被设置为 WestUS2。
设置资源组
在继续之前,请确保资源组存在。 此步骤创建资源组(如果尚不存在)。
export RANDOM_SUFFIX=$(openssl rand -hex 3)
export MY_RESOURCE_GROUP_NAME="myResourceGroup$RANDOM_SUFFIX"
export REGION="WestUS2"
az group create --name $MY_RESOURCE_GROUP_NAME --___location $REGION
{
"id": "/subscriptions/xxxxx/resourceGroups/myResourceGroupxxx",
"___location": "WestUS2",
"managedBy": null,
"name": "myResourceGroupxxx",
"properties": {
"provisioningState": "Succeeded"
},
"tags": null,
"type": "Microsoft.Resources/resourceGroups"
}
创建虚拟机规模集
若要确保后续的更新和查询命令具有有效的资源来运行,请创建虚拟机规模集。 在此步骤中,使用有效的映像 (Ubuntu2204) 部署一个基本规模集,并将实例计数设置为 5,使特定于实例的更新可以面向现有实例 ID。
export SCALE_SET_NAME="myScaleSet$RANDOM_SUFFIX"
az vmss create \
--resource-group $MY_RESOURCE_GROUP_NAME \
--name $SCALE_SET_NAME \
--image Ubuntu2204 \
--upgrade-policy-mode manual \
--instance-count 5 \
--admin-username azureuser \
--generate-ssh-keys
{
"id": "/subscriptions/xxxxx/resourceGroups/myResourceGroupxxx/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSetxxx",
"___location": "WestUS2",
"name": "myScaleSetxxx",
"provisioningState": "Succeeded"
}
更新规模集模型
规模集有一个“规模集模型”,用于以整体方式捕获规模集的所需状态。 若要查询规模集的模型,可使用 az vmss show:
az vmss show --resource-group $MY_RESOURCE_GROUP_NAME --name $SCALE_SET_NAME
输出的具体呈现取决于提供给命令的选项。 下面的示例显示了来自 Azure CLI 的精简版示例输出:
{
"id": "/subscriptions/xxxxx/resourceGroups/myResourceGroupxxx/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSetxxx",
"___location": "WestUS2",
"name": "myScaleSetxxx",
"orchestrationMode": "Flexible",
"platformFaultDomainCount": 1,
"resourceGroup": "myResourceGroupxxx",
"sku": {
"capacity": 5,
"name": "Standard_DS1_v2",
"tier": "Standard"
},
"timeCreated": "2022-11-29T22:16:43.250912+00:00",
"type": "Microsoft.Compute/virtualMachineScaleSets",
"networkProfile": {
"networkApiVersion": "2020-11-01",
"networkInterfaceConfigurations": [
{
"deleteOption": "Delete",
"disableTcpStateTracking": false,
"dnsSettings": {
"dnsServers": []
},
"enableIpForwarding": false,
"ipConfigurations": [
{
"applicationGatewayBackendAddressPools": [],
"applicationSecurityGroups": [],
"loadBalancerBackendAddressPools": [
{
"id": "/subscriptions/xxxxx/resourceGroups/myResourceGroupxxx/providers/Microsoft.Network/loadBalancers/myScaleSetLB/backendAddressPools/myScaleSetLBBEPool",
"resourceGroup": "myResourceGroupxxx"
}
],
"name": "mysca2215IPConfig",
"privateIpAddressVersion": "IPv4",
"subnet": {
"id": "/subscriptions/xxxxx/resourceGroups/myResourceGroupxxx/providers/Microsoft.Network/virtualNetworks/myScaleSetVNET/subnets/myScaleSetSubnet",
"resourceGroup": "myResourceGroupxxx"
}
}
],
"name": "mysca2215Nic",
"networkSecurityGroup": {
"id": "/subscriptions/xxxxx/resourceGroups/myResourceGroupxxx/providers/Microsoft.Network/networkSecurityGroups/myScaleSetNSG",
"resourceGroup": "myResourceGroupxxx"
},
"primary": true
}
]
},
"osProfile": {
"allowExtensionOperations": true,
"computerNamePrefix": "myScaleS",
"linuxConfiguration": {
"disablePasswordAuthentication": true,
"enableVmAgentPlatformUpdates": false,
"patchSettings": {
"assessmentMode": "ImageDefault",
"patchMode": "ImageDefault"
},
"provisionVmAgent": true
}
},
"storageProfile": {
"imageReference": {
"offer": "UbuntuServer",
"publisher": "Canonical",
"sku": "22_04-lts",
"version": "latest"
},
"osDisk": {
"caching": "ReadWrite",
"createOption": "FromImage",
"deleteOption": "Delete",
"diskSizeGb": 30,
"managedDisk": {
"storageAccountType": "Premium_LRS"
},
"osType": "Linux"
}
}
}
可以使用 az vmss update 更新规模集的各种属性。 例如,更新许可证类型或 VM 的实例保护策略。 请注意,允许的许可证类型值 RHEL_BYOS 而不是 Windows_Server。
az vmss update --name $SCALE_SET_NAME --resource-group $MY_RESOURCE_GROUP_NAME --license-type RHEL_BYOS
export INSTANCE_ID=$(az vmss list-instances \
--resource-group $MY_RESOURCE_GROUP_NAME \
--name $SCALE_SET_NAME \
--query "[0].instanceId" \
-o tsv)
az vmss update \
--name $SCALE_SET_NAME \
--resource-group $MY_RESOURCE_GROUP_NAME \
--instance-id "$INSTANCE_ID" \
--protect-from-scale-set-actions False \
--protect-from-scale-in
此外,如果此前已使用 az vmss create
命令部署了规模集,则可再次运行 az vmss create
命令来更新规模集。 请确保 az vmss create
命令中的所有属性都与以前的一样,要修改的属性除外。 例如,下面我们将实例计数增加到 5。
重要
从 2023 年 11 月开始,使用 PowerShell 和 Azure CLI 创建的 VM 规模集将默认为灵活业务流程模式(如果未指定业务流程模式)。 若要详细了解此更改以及你应采取哪些操作,请访问针对 VMSS PowerShell/CLI 客户的中断性变更 - Microsoft 社区中心
az vmss create \
--resource-group $MY_RESOURCE_GROUP_NAME \
--name $SCALE_SET_NAME \
--orchestration-mode flexible \
--image RHELRaw8LVMGen2 \
--admin-username azureuser \
--generate-ssh-keys \
--instance-count 5 \
--os-disk-size-gb 64
更新规模集中的单个 VM 实例
规模集中的每个 VM 实例都有自己的模型视图,这类似于每个规模集都有模型视图的情况。 若要查询规模集中特定 VM 实例的模型视图,可使用 az vm show。
export INSTANCE_NAME=$(az vmss list-instances \
--resource-group $MY_RESOURCE_GROUP_NAME \
--name $SCALE_SET_NAME \
--query "[0].name" \
-o tsv)
az vm show --resource-group $MY_RESOURCE_GROUP_NAME --name $INSTANCE_NAME
输出的具体呈现取决于提供给命令的选项。 下面的示例显示了来自 Azure CLI 的精简版示例输出:
{
"hardwareProfile": {
"vmSize": "Standard_DS1_v2"
},
"id": "/subscriptions/xxxxx/resourceGroups/myResourceGroupxxx/providers/Microsoft.Compute/virtualMachines/myScaleSet_Instance1",
"___location": "WestUS2",
"name": "myScaleSet_Instance1",
"networkProfile": {
"networkInterfaces": [
{
"deleteOption": "Delete",
"id": "/subscriptions/xxxxx/resourceGroups/myResourceGroupxxx/providers/Microsoft.Network/networkInterfaces/mysca2215Nic-5cf164f7",
"primary": true,
"resourceGroup": "myResourceGroupxxx"
}
]
},
"osProfile": {
"allowExtensionOperations": true,
"computerName": "myScaleset_Computer1",
"linuxConfiguration": {
"disablePasswordAuthentication": true,
"enableVmAgentPlatformUpdates": false,
"patchSettings": {
"assessmentMode": "ImageDefault",
"patchMode": "ImageDefault"
},
"provisionVmAgent": true
}
},
"provisioningState": "Succeeded",
"resourceGroup": "myResourceGroupxxx",
"storageProfile": {
"dataDisks": [],
"imageReference": {
"exactVersion": "22.04.202204200",
"offer": "0001-com-ubuntu-server-jammy",
"publisher": "Canonical",
"sku": "22_04-lts",
"version": "latest"
},
"osDisk": {
"caching": "ReadWrite",
"createOption": "FromImage",
"deleteOption": "Delete",
"diskSizeGb": 30,
"managedDisk": {
"id": "/subscriptions/xxxxx/resourceGroups/myResourceGroupxxx/providers/Microsoft.Compute/disks/myScaleSet_Instance1_disk1_xxx",
"resourceGroup": "myResourceGroupxxx",
"storageAccountType": "Premium_LRS"
},
"name": "myScaleSet_Instance1_disk1_xxx",
"osType": "Linux"
}
},
"timeCreated": "2022-11-29T22:16:44.500895+00:00",
"type": "Microsoft.Compute/virtualMachines",
"virtualMachineScaleSet": {
"id": "/subscriptions/xxxxx/resourceGroups/myResourceGroupxxx/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSetxxx",
"resourceGroup": "myResourceGroupxxx"
}
}
这些属性描述的是规模集中某个 VM 实例的配置,而不是规模集作为一个整体的配置。
可以像对独立 VM 一样,对规模集中的单个 VM 实例执行更新。 例如,将新的数据磁盘附加到实例 1:
az vm disk attach --resource-group $MY_RESOURCE_GROUP_NAME --vm-name $INSTANCE_NAME --name disk_name1 --new
再次运行 az vm show,我们现在会看到 VM 实例附加了新磁盘。
{
"storageProfile": {
"dataDisks": [
{
"caching": "None",
"createOption": "Empty",
"deleteOption": "Detach",
"diskSizeGb": 1023,
"lun": 0,
"managedDisk": {
"id": "/subscriptions/xxxxx/resourceGroups/myResourceGroupxxx/providers/Microsoft.Compute/disks/disk_name1",
"resourceGroup": "myResourceGroupxxx",
"storageAccountType": "Premium_LRS"
},
"name": "disk_name1",
"toBeDetached": false
}
]
}
}
将实例添加到规模集
有时您可能想向规模集中添加新的虚拟机,但希望这些虚拟机的配置选项不同于规模集模型中列出的选项。 使用 az vm create 命令并指定要向其添加实例的规模集名称,可以在创建期间将 VM 添加到规模集。
export NEW_INSTANCE_NAME="myNewInstance$RANDOM_SUFFIX"
az vm create --name $NEW_INSTANCE_NAME --resource-group $MY_RESOURCE_GROUP_NAME --vmss $SCALE_SET_NAME --image RHELRaw8LVMGen2
{
"fqdns": "",
"id": "/subscriptions/xxxxx/resourceGroups/myResourceGroupxxx/providers/Microsoft.Compute/virtualMachines/myNewInstancexxx",
"___location": "WestUS2",
"macAddress": "60-45-BD-D7-13-DD",
"powerState": "VM running",
"privateIpAddress": "10.0.0.6",
"publicIpAddress": "20.172.144.96",
"resourceGroup": "myResourceGroupxxx",
"zones": ""
}
如果随后检查规模集,将看到添加了新实例。
az vm list --resource-group $MY_RESOURCE_GROUP_NAME --output table
Name ResourceGroup Location
-------------------- --------------- ----------
myNewInstancexxx myResourceGroupxxx WestUS2
myScaleSet_Instance1 myResourceGroupxxx WestUS2
myScaleSet_Instance1 myResourceGroupxxx WestUS2
使用最新的规模集模型对 VM 进行更新
注意
使用灵活业务流程模式的虚拟机规模集当前不支持升级模式。
规模集有一项“升级策略”,该策略决定了如何使用最新的规模集模型对 VM 进行更新。 升级策略的三种模式是:
- 自动 - 在此模式下,规模集无法保证 VM 的关闭顺序。 规模集可能同时关闭所有 VM。
- 滚动 - 在此模式下,规模集以批次的方式推出更新,批次之间的暂停时间为可选。
- 手动 - 在此模式下,更新规模集模型时,在触发手动更新之前,不会对现有的 VM 执行任何操作。
如果规模集设置为手动升级,则可以使用 az vmss update 触发手动升级。
az vmss update --resource-group $MY_RESOURCE_GROUP_NAME --name $SCALE_SET_NAME
注意
Service Fabric 群集只能使用“自动”模式,但采用不同方式来处理更新。 有关详细信息,请参阅 Service Fabric 应用程序升级。
重置规模集的映像
虚拟机规模集将为规模集中的每个 VM 生成独一无二的名称。 命名约定因业务流程模式而异:
- 灵活业务流程模式:{scale-set-name}_{8-char-guid}
- 统一业务流程模式:{scale-set-name}_{instance-id}
在需要重置特定实例映像的情况下,请使用 az vmss reimage 并指定实例 ID。另一种方法是使用 az vm 重新部署 直接重置 VM 映像。 如果要重新映像 VM,而无需指定实例 ID,则此命令非常有用。
# Get the VM name first
VM_NAME=$(az vmss list-instances \
--resource-group $MY_RESOURCE_GROUP_NAME \
--name $SCALE_SET_NAME \
--query "[0].name" \
-o tsv)
# Reimage the VM directly
az vm redeploy \
--resource-group $MY_RESOURCE_GROUP_NAME \
--name $VM_NAME
更新规模集的 OS 映像
你可能具有运行旧版 Ubuntu 的规模集。 你想要更新到较新版本的 Ubuntu,例如最新版本。 映像引用版本属性不是列表的一部分,因此可以使用 az vmss update 直接修改这些属性。
az vmss update --resource-group $MY_RESOURCE_GROUP_NAME --name $SCALE_SET_NAME --set virtualMachineProfile.storageProfile.imageReference.version=latest
或者,你可能想要更改规模集使用的映像。 例如,你可能想要更新或更改规模集使用的自定义映像。 可以通过更新“映像引用 ID”属性来更改规模集使用的映像。 映像引用 ID 属性不是列表的一部分,因此可以使用 az vmss update 直接修改此属性。
如果使用 Azure 平台映像,可以通过修改 imageReference 来更新映像(有关详细信息,请参阅 REST API 文档)。
注意
使用平台映像时,通常指定 "latest" 作为映像引用版本。 在你执行创建、横向扩展和重置映像操作时,将使用最新发布的脚本创建 VM。 但是,这并不意味着 OS 映像会随新映像版本的发布自动进行更新。 一个独立功能提供了自动 OS 升级功能。 有关详细信息,请参阅自动 OS 升级文档。
如果使用自定义映像,可以通过更新 imageReference ID 来更新映像(有关详细信息,请参阅 REST API 文档)。
更新规模集的负载均衡器
假设在规模集带有 Azure 负载均衡器的情况下,需要将 Azure 负载均衡器替换为 Azure 应用程序网关。 规模集的负载均衡器和应用程序网关属性是列表的一部分,因此可以使用以下命令来删除或添加列表元素,而不必直接修改属性。
# Remove the load balancer backend pool from the scale set model
az vmss update --resource-group $MY_RESOURCE_GROUP_NAME --name $SCALE_SET_NAME --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 $MY_RESOURCE_GROUP_NAME --name $SCALE_SET_NAME --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 $MY_RESOURCE_GROUP_NAME --name $SCALE_SET_NAME --add virtualMachineProfile.networkProfile.networkInterfaceConfigurations[0].ipConfigurations[0].ApplicationGatewayBackendAddressPools '{"id": "/subscriptions/xxxxx/resourceGroups/'$MY_RESOURCE_GROUP_NAME'/providers/Microsoft.Network/applicationGateways/{applicationGatewayName}/backendAddressPools/{applicationGatewayBackendPoolName}"}'
注意
这些命令假定规模集上只有一个 IP 配置和负载均衡器。 如果有多个,则可能需要使用 0 之外的列表索引。
后续步骤
本教程介绍了如何修改规模集的各个方面以及单个实例。
- 更新规模集模型
- 更新规模集中的单个 VM 实例
- 将实例添加到规模集
- 使用最新的规模集模型对 VM 进行更新
- 重置规模集的映像
- 更新规模集的 OS 映像
- 更新规模集的负载均衡器