教程:使用 Azure CLI 修改虚拟机规模集

在应用程序的整个生命周期内,你可能需要修改或更新你的虚拟机规模集。 这些更新可能包括更新规模集的配置,或更改应用程序配置。 本文介绍如何使用 Azure CLI 修改现有的规模集。

更新规模集模型

规模集有一个“规模集模型”,用于以整体方式捕获规模集的所需状态。 若要查询规模集的模型,可使用 az vmss show

az vmss show --resource-group myResourceGroup --name myScaleSet

输出的具体呈现取决于提供给命令的选项。 下面的示例显示了来自 Azure CLI 的精简版示例输出:

{
  "id": "/subscriptions/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSet",
  "location": "chinanorth2",
  "name": "myScaleSet",
  "orchestrationMode": "Flexible",
  "platformFaultDomainCount": 1,
  "resourceGroup": "myResourceGroup",
  "sku": {
    "capacity": 2,
    "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/resourceGroups/myResourceGroup/providers/Microsoft.Network/loadBalancers/myScaleSetLB/backendAddressPools/myScaleSetLBBEPool",
                  "resourceGroup": "myResourceGroup"
                }
              ],
              "name": "mysca2215IPConfig",
              "privateIpAddressVersion": "IPv4",
              "subnet": {
                "id": "/subscriptions/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myScaleSetVNET/subnets/myScaleSetSubnet",
                "resourceGroup": "myResourceGroup"
              }
            }
          ],
          "name": "mysca2215Nic",
          "networkSecurityGroup": {
            "id": "/subscriptions/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/myScaleSetNSG",
            "resourceGroup": "myResourceGroup"
          },
          "primary": true
        }
      ]
    },
    "osProfile": {
      "allowExtensionOperations": true,
      "computerNamePrefix": "myScaleS",
      "linuxConfiguration": {
        "disablePasswordAuthentication": true,
        "enableVmAgentPlatformUpdates": false,
        "patchSettings": {
          "assessmentMode": "ImageDefault",
          "patchMode": "ImageDefault"
        },
        "provisionVmAgent": true,
      },
    },
    "storageProfile": {
      "imageReference": {
        "offer": "0001-com-ubuntu-server-jammy",
        "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 实例保护策略。

az vmss update --name MyScaleSet --resource-group MyResourceGroup --license-type windows_server
az vmss update --name MyScaleSet --resource-group MyResourceGroup --instance-id 4 --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 myResourceGroup \
  --name myScaleSet \
  --orchestration-mode flexible \
  --image RHELRaw8LVMGen2 \
  --admin-username azureuser \
  --generate-ssh-keys \
  --instance-count 5

更新规模集中的单个 VM 实例

规模集中的每个 VM 实例都有自己的模型视图,这类似于每个规模集都有模型视图的情况。 若要查询规模集中特定 VM 实例的模型视图,可使用 az vm show

az vm show --resource-group myResourceGroup --name myScaleSet_Instanace1

输出的具体呈现取决于提供给命令的选项。 下面的示例显示了来自 Azure CLI 的精简版示例输出:

{
  "hardwareProfile": {
    "vmSize": "Standard_DS1_v2",
  },
  "id": "/subscriptions/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myScaleSet_Instance1",
  "location": "chinanorth2",
  "name": "myScaleSet_Instance1",
  "networkProfile": {
    "networkInterfaces": [
      {
        "deleteOption": "Delete",
        "id": "/subscriptions/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/mysca2215Nic-5cf164f7",
        "primary": true,
        "resourceGroup": "myResourceGroup"
      }
    ]
  },
  "osProfile": {
    "allowExtensionOperations": true,
    "computerName": "myScaleset_Computer1",
    "linuxConfiguration": {
      "disablePasswordAuthentication": true,
      "enableVmAgentPlatformUpdates": false,
      "patchSettings": {
        "assessmentMode": "ImageDefault",
        "patchMode": "ImageDefault"
      },
      "provisionVmAgent": true,
    },
  },
  "provisioningState": "Succeeded",
  "resourceGroup": "myResourceGroup",
  "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/resourceGroups/myResourceGroup/providers/Microsoft.Compute/disks/myScaleSet_Instance1_disk1_e1a6c46a6b5f44d695fc9e38727267c2",
        "resourceGroup": "myResourceGroup",
        "storageAccountType": "Premium_LRS"
      },
      "name": "myScaleSet_Instance1_disk1_e1a6c46a6b5f44d695fc9e38727267c2",
      "osType": "Linux",
    }
  },
  "timeCreated": "2022-11-29T22:16:44.500895+00:00",
  "type": "Microsoft.Compute/virtualMachines",
  "virtualMachineScaleSet": {
    "id": "/subscriptions/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSet",
    "resourceGroup": "myResourceGroup"
  },
}

这些属性描述的是规模集中某个 VM 实例的配置,而不是规模集作为一个整体的配置。

可以像对独立 VM 一样,对规模集中的单个 VM 实例执行更新。 例如,将新的数据磁盘附加到实例 1:

az vm disk attach --resource-group myResourceGroup --vm-name myScaleSet_Instance1 --name disk_name1 --new

再次运行 az vm show,我们现在会看到 VM 实例附加了新磁盘。

  "storageProfile": {
    "dataDisks": [
      {
        "caching": "None",
        "createOption": "Empty",
        "deleteOption": "Detach",
        "diskSizeGb": 1023,
        "lun": 0,
        "managedDisk": {
          "id": "/subscriptions/49d84582-7207-4a4f-824e-044e83c71887/resourceGroups/myResourceGroup/providers/Microsoft.Compute/disks/disk_name1",
          "resourceGroup": "myResourceGroup",
          "storageAccountType": "Premium_LRS"
        },
        "name": "disk_name1",
        "toBeDetached": false,
      }
    ],

将实例添加到规模集

有时,你可能想要将新的 VM 添加到规模集,但希望配置选项不同于规模集模型中列出的选项。 使用 az vm create 命令并指定要向其添加实例的规模集名称,可以在创建期间将 VM 添加到规模集。

az vm create --name myNewInstance --resource-group myResourceGroup --vmss myScaleSet --image RHELRaw8LVMGen2
{
  "fqdns": "",
  "id": "/subscriptions/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myNewInstance",
  "location": "chinanorth2",
  "macAddress": "60-45-BD-D7-13-DD",
  "powerState": "VM running",
  "privateIpAddress": "10.0.0.6",
  "publicIpAddress": "20.172.144.96",
  "resourceGroup": "myResourceGroup",
  "zones": ""

如果随后检查规模集,将看到添加了新实例。

az vm list --resource-group myResourceGroup --output table
Name                 ResourceGroup    Location
-------------------  ---------------  ----------
myNewInstance         myResourceGroup  chinanorth2
myScaleSet_Instance1  myResourceGroup  chinanorth2
myScaleSet_Instance1  myResourceGroup  chinanorth2

使用最新的规模集模型对 VM 进行更新

注意

使用灵活业务流程模式的虚拟机规模集当前不支持升级模式。

规模集有一项“升级策略”,该策略决定了如何使用最新的规模集模型对 VM 进行更新。 升级策略的三种模式是:

  • 自动 - 在此模式下,规模集无法保证 VM 的关闭顺序。 规模集可能同时关闭所有 VM。
  • 滚动 - 在此模式下,规模集以批次的方式推出更新,批次之间的暂停时间为可选。
  • 手动 - 在此模式下,更新规模集模型时,在触发手动更新之前,不会对现有的 VM 执行任何操作。

如果规模集设置为手动升级,则可以使用 az vmss update 触发手动升级。

az vmss update --resource-group myResourceGroup --name myScaleSet

注意

Service Fabric 群集只能使用“自动”模式,但采用不同方式来处理更新。 有关详细信息,请参阅 Service Fabric 应用程序升级

重置规模集的映像

虚拟机规模集将为规模集中的每个 VM 生成独一无二的名称。 命名约定因业务流程模式而异:

  • 灵活业务流程模式:{scale-set-name}_{8-char-guid}
  • 统一业务流程模式:{scale-set-name}_{instance-id}

如果需要重置特定实例的映像,请使用 az vmss reimage 并指定实例名称。

az vmss reimage --resource-group myResourceGroup --name myScaleSet --instance-id myScaleSet_Instance1

更新规模集的 OS 映像

你可能具有运行旧版 Ubuntu 的规模集。 你希望将其更新到新版 Ubuntu,例如版本 22.04.202204200. 映像引用版本属性不是列表的一部分,因此可以使用 az vmss update 直接修改这些属性。

az vmss update --resource-group myResourceGroup --name myScaleSet --set virtualMachineProfile.storageProfile.imageReference.version=22.04.202204200

或者,你可能想要更改规模集使用的映像。 例如,你可能想要更新或更改规模集使用的自定义映像。 可以通过更新“映像引用 ID”属性来更改规模集使用的映像。 映像引用 ID 属性不是列表的一部分,因此可以使用 az vmss update 直接修改此属性。

az vmss update \
--resource-group myResourceGroup \
--name myScaleSet \
--set virtualMachineProfile.storageProfile.imageReference.id=/subscriptions/{subscriptionID}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/images/myNewImage

如果使用 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 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 之外的列表索引。

后续步骤

本教程介绍了如何修改规模集的各个方面以及单个实例。

  • 更新规模集模型
  • 更新规模集中的单个 VM 实例
  • 将实例添加到规模集
  • 使用最新的规模集模型对 VM 进行更新
  • 重置规模集的映像
  • 更新规模集的 OS 映像
  • 更新规模集的负载均衡器