修改虚拟机规模集Modify a virtual machine scale set

在应用程序的整个生命周期内,你可能需要修改或更新你的虚拟机规模集。Throughout the lifecycle of your applications, you may need to modify or update your virtual machine scale set. 这些更新可能包括更新规模集的配置,或更改应用程序配置。These updates may include how to update the configuration of the scale set, or change the application configuration. 本文介绍了如何使用 REST API、Azure PowerShell 或 Azure CLI 修改现有规模集。This article describes how to modify an existing scale set with the REST APIs, Azure PowerShell, or Azure CLI.

基本概念Fundamental concepts

规模集模型The scale set model

规模集有一个“规模集模型”,用于以整体方式捕获规模集的所需状态。 A scale set has a "scale set model" that captures the desired state of the scale set as a whole. 若要查询规模集的模型,可使用以下命令:To query the model for a scale set, you can use the

  • 如下所示通过 REST API 使用 compute/virtualmachinescalesets/getREST API with compute/virtualmachinescalesets/get as follows:

    GET https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSet?api-version={apiVersion}
    
  • 通过 Azure PowerShell 使用 Get-AzVmssAzure PowerShell with Get-AzVmss:

    Get-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"
    
  • 通过 Azure CLI 使用 az vmss showAzure CLI with az vmss show:

    az vmss show --resource-group myResourceGroup --name myScaleSet
    
  • 还可以使用特定于语言的 Azure SDKYou can also use the language-specific Azure SDKs.

输出的具体呈现取决于提供给命令的选项。The exact presentation of the output depends on the options you provide to the command. 下面的示例显示了来自 Azure CLI 的精简版示例输出:The following example shows condensed sample output from the Azure CLI:

az vmss show --resource-group myResourceGroup --name myScaleSet
{
  "location": "chinanorth",
  "overprovision": true,
  "plan": null,
  "singlePlacementGroup": true,
  "sku": {
    "additionalProperties": {},
    "capacity": 1,
    "name": "Standard_D2_v2",
    "tier": "Standard"
  },
}

这些属性作为一个整体应用到规模集。These properties apply to the scale set as a whole.

规模集实例视图The scale set instance view

规模集还有一个“规模集实例视图”,用于以整体方式捕获规模集当前的“运行时”状态。 A scale set also has a "scale set instance view" that captures the current runtime state of the scale set as a whole. 若要查询规模集的实例视图,可使用以下命令:To query the instance view for a scale set, you can use:

输出的具体呈现取决于提供给命令的选项。The exact presentation of the output depends on the options you provide to the command. 下面的示例显示了来自 Azure CLI 的精简版示例输出:The following example shows condensed sample output from the 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 的当前运行时状态,例如应用于规模集的扩展的状态。These properties provide a summary of the current runtime state of the VMs in the scale set, such as the status of extensions applied to the scale set.

规模集 VM 模型视图The scale set VM model view

规模集中的每个 VM 实例都有自己的模型视图,这类似于每个规模集都有模型视图的情况。Similar to how a scale set has a model view, each VM instance in the scale set has its own model view. 若要查询规模集中特定 VM 实例的模型视图,可使用以下命令:To query the model view for a particular VM instance in a scale set, you can use:

  • 如下所示通过 REST API 使用 compute/virtualmachinescalesetvms/getREST API with compute/virtualmachinescalesetvms/get as follows:

    GET https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSet/virtualmachines/instanceId?api-version={apiVersion}
    
  • 通过 Azure PowerShell 使用 Get-AzVmssVmAzure PowerShell with Get-AzVmssVm:

    Get-AzVmssVm -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId instanceId
    
  • 通过 Azure CLI 使用 az vmss showAzure CLI with az vmss show:

    az vmss show --resource-group myResourceGroup --name myScaleSet --instance-id instanceId
    
  • 还可以使用 Azure SDKYou can also use the Azure SDKs.

输出的具体呈现取决于提供给命令的选项。The exact presentation of the output depends on the options you provide to the command. 下面的示例显示了来自 Azure CLI 的精简版示例输出:The following example shows condensed sample output from the Azure CLI:

$ az vmss show --resource-group myResourceGroup --name myScaleSet
{
  "location": "chinanorth",
  "name": "{name}",
  "sku": {
    "name": "Standard_D2_v2",
    "tier": "Standard"
  },
}

这些属性描述的是规模集中某个 VM 实例的配置,而不是规模集作为一个整体的配置。These properties describe the configuration of a VM instance within a scale set, not the configuration of the scale set as a whole. 例如,规模集模型使用 overprovision 作为属性,而规模集中 VM 实例的模型则不是这样。For example, the scale set model has overprovision as a property, while the model for a VM instance within a scale set does not. 之所以存在这种差异,是因为过度预配是规模集作为一个整体的属性,而不是规模集中各个 VM 实例的属性(有关过度预配的详细信息,请参阅规模集的设计注意事项)。This difference is because overprovisioning is a property for the scale set as a whole, not individual VM instances in the scale set (for more information about overprovisioning, see Design considerations for scale sets).

规模集 VM 实例视图The scale set VM instance view

规模集中的每个 VM 实例都有自己的实例视图,这类似于每个规模集都有实例视图的情况。Similar to how a scale set has an instance view, each VM instance in the scale set has its own instance view. 若要查询规模集中特定 VM 实例的实例视图,可使用以下命令:To query the instance view for a particular VM instance within a scale set, you can use:

输出的具体呈现取决于提供给命令的选项。The exact presentation of the output depends on the options you provide to the command. 下面的示例显示了来自 Azure CLI 的精简版示例输出:The following example shows condensed sample output from the 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 实例的当前运行时状态,包括应用于规模集的任何扩展。These properties describe the current runtime state of a VM instance within a scale set, which includes any extensions applied to the scale set.

如何更新全局规模集属性How to update global scale set properties

若要更新某个全局规模集属性,必须在规模集模型中更新该属性。To update a global scale set property, you must update the property in the scale set model. 可通过以下方式进行该更新:You can do this update via:

  • 如下所示通过 REST API 使用 compute/virtualmachinescalesets/createorupdateREST API with compute/virtualmachinescalesets/createorupdate as follows:

    PUT https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSet?api-version={apiVersion}
    
  • 可以使用 REST API 中的属性部署资源管理器模板,以便更新全局规模集属性。You can deploy a Resource Manager template with the properties from the REST API to update global scale set properties.

  • 通过 Azure PowerShell 使用 Update-AzVmssAzure PowerShell with Update-AzVmss:

    Update-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -VirtualMachineScaleSet {scaleSetConfigPowershellObject}
    
  • 通过 Azure CLI 使用 az vmss updateAzure CLI with az vmss update:

    • 修改属性:To modify a property:

      az vmss update --set {propertyPath}={value}
      
    • 向规模集中的列表属性添加对象:To add an object to a list property in a scale set:

      az vmss update --add {propertyPath} {JSONObjectToAdd}
      
    • 从规模集中的列表属性删除对象:To remove an object from a list property in a scale set:

      az vmss update --remove {propertyPath} {indexToRemove}
      
    • 如果此前已使用 az vmss create 命令部署了规模集,则可再次运行 az vmss create 命令来更新规模集。If you previously deployed the scale set with the az vmss create command, you can run the az vmss create command again to update the scale set. 请确保 az vmss create 命令中的所有属性都与以前的一样,要修改的属性除外。Make sure that all properties in the az vmss create command are the same as before, except for the properties that you wish to modify.

  • 还可以使用 Azure SDKYou can also use the Azure SDKs.

更新规模集模型以后,新配置就会应用到在规模集中创建的新 VM。Once the scale set model is updated, the new configuration applies to any new VMs created in the scale set. 但是,规模集中现有 VM 的模型仍需使用总体说来最新的规模集模型进行更新。However, the models for the existing VMs in the scale set must still be brought up-to-date with the latest overall scale set model. 在每个 VM 的模型中有一个名为 latestModelApplied 的布尔属性,该属性指示 VM 是否已使用总体来说最新的规模集模型进行更新(true 意味着 VM 已使用最新模型进行更新)。In the model for each VM is a boolean property called latestModelApplied that indicates whether or not the VM is up-to-date with the latest overall scale set model (true means the VM is up-to-date with the latest model).

如何使用最新的规模集模型对 VM 进行更新How to bring VMs up-to-date with the latest scale set model

规模集有一项“升级策略”,该策略决定了如何使用最新的规模集模型对 VM 进行更新。Scale sets have an "upgrade policy" that determine how VMs are brought up-to-date with the latest scale set model. 升级策略的三种模式是:The three modes for the upgrade policy are:

  • 自动 - 在此模式下,规模集无法保证 VM 的关闭顺序。Automatic - In this mode, the scale set makes no guarantees about the order of VMs being brought down. 规模集可能同时关闭所有 VM。The scale set may take down all VMs at the same time.
  • 滚动 - 在此模式下,规模集以批次的方式推出更新,批次之间的暂停时间为可选。Rolling - In this mode, the scale set rolls out the update in batches with an optional pause time between batches.
  • 手动 - 在此模式下,对规模集模型进行更新时,现有的 VM 不会发生任何事情。Manual - In this mode, when you update the scale set model, nothing happens to existing VMs.

若要更新现有的 VM,必须对每个现有的 VM 进行“手动升级”。To update existing VMs, you must do a "manual upgrade" of each existing VM. 可通过以下方式进行此手动升级:You can do this manual upgrade with:

备注

Service Fabric 群集只能使用“自动”模式,但采用不同方式来处理更新。 Service Fabric clusters can only use Automatic mode, but the update is handled differently. 有关详细信息,请参阅 Service Fabric 应用程序升级For more information, see Service Fabric application upgrades.

有一类对全局规模集属性的修改不遵循升级策略。There is one type of modification to global scale set properties that does not follow the upgrade policy. 只能通过 API 版本 2017-12-01 或更高版本更改规模集 OS 和数据磁盘配置文件(例如管理员用户名和密码)。Changes to the scale set OS and Data disk Profile (such as admin username and password) can only be changed in API version 2017-12-01 or later. 这些更改仅适用于在对规模集模型进行更改后创建的 VM。These changes only apply to VMs created after the change in the scale set model. 若要更新现有的 VM,必须对每个现有的 VM 执行“重置映像”操作。To bring existing VMs up-to-date, you must do a "reimage" of each existing VM. 可通过以下方式执行此重置映像操作:You can do this reimage via:

  • 如下所示通过 REST API 使用 compute/virtualmachinescalesets/reimageREST API with compute/virtualmachinescalesets/reimage as follows:

    POST https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSet/reimage?api-version={apiVersion}
    
  • 通过 Azure PowerShell 使用 Set-AzVmssVmAzure PowerShell with Set-AzVmssVm:

    Set-AzVmssVM -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId instanceId -Reimage
    
  • 通过 Azure CLI 使用 az vmss reimageAzure CLI with az vmss reimage:

    az vmss reimage --resource-group myResourceGroup --name myScaleSet --instance-id instanceId
    
  • 还可以使用特定于语言的 Azure SDKYou can also use the language-specific Azure SDKs.

对修改有限制的属性Properties with restrictions on modification

创建时属性Create-time properties

某些属性只能在创建规模集时设置。Some properties can only be set when you create the scale set. 这些属性包括:These properties include:

  • Image reference publisherImage reference publisher
  • image reference offerImage reference offer
  • 托管 OS 磁盘存储帐户类型Managed OS disk storage account type

只能在当前值的基础上更改的属性Properties that can only be changed based on the current value

某些属性可以更改,但也有例外,具体取决于当前值。Some properties may be changed, with exceptions depending on the current value. 这些属性包括:These properties include:

  • singlePlacementGroup - 如果 singlePlacementGroup 为 true,则可将其修改为 false。singlePlacementGroup - If singlePlacementGroup is true, it may be modified to false. 但是,如果 singlePlacementGroup 为 false,则不可将其修改为 true。However, if singlePlacementGroup is false, it may not be modified to true.
  • subnet - 修改规模集的子网的前提是,原始子网和新子网在同一虚拟网络中。subnet - The subnet of a scale set may be modified as long as the original subnet and the new subnet are in the same virtual network.

需要解除分配才能更改的属性Properties that require deallocation to change

某些属性只有在规模集中的 VM 已解除分配的情况下,才能更改为特定值。Some properties may only be changed to certain values if the VMs in the scale set are deallocated. 这些属性包括:These properties include:

  • SKU 名称 - 如果新 VM SKU 在规模集当前所在的硬件上不受支持,则需先将规模集中的 VM 解除分配,然后才能修改 SKU 名称。SKU name- If the new VM SKU is not supported on the hardware the scale set is currently on, you need to deallocate the VMs in the scale set before you modify the SKU name. 有关详细信息,请参阅如何调整 Azure VM 的大小For more information, see how to resize an Azure VM.

特定于 VM 的更新VM-specific updates

某些修改可以应用于特定的 VM,但不能应用于全局规模集属性。Certain modifications may be applied to specific VMs instead of the global scale set properties. 目前,唯一受支持的特定于 VM 的更新是将数据磁盘附加到规模集中的 VM 或者从其分离数据磁盘。Currently, the only VM-specific update that is supported is to attach/detach data disks to/from VMs in the scale set. 此功能为预览版。This feature is in preview. 有关详细信息,请参阅预览版文档For more information, see the preview documentation.

方案Scenarios

应用程序更新Application updates

如果应用程序已通过扩展部署到规模集,则更新扩展配置会导致应用程序按升级策略进行更新。If an application is deployed to a scale set through extensions, an update to the extension configuration causes the application to update in accordance with the upgrade policy. 例如,如果新版脚本在自定义脚本扩展中运行,则可更新 fileUris 属性,使之指向新脚本。For instance, if you have a new version of a script to run in a Custom Script Extension, you could update the fileUris property to point to the new script. 在某些情况下,可能需要强制进行更新,即使扩展配置未更改(例如,在未更改脚本 URI 的情况下更新脚本)。In some cases, you may wish to force an update even though the extension configuration is unchanged (for example, you updated the script without a change to the URI of the script). 在这些情况下,可以通过修改 forceUpdateTag 来强制进行更新。In these cases, you can modify the forceUpdateTag to force an update. Azure 平台不解释此属性。The Azure platform does not interpret this property. 如果更改此值,不会影响扩展的运行方式。If you change the value, there is no effect on how the extension runs. 更改只是会强制扩展重新运行。A change simply forces the extension to rerun. 有关 forceUpdateTag 的详细信息,请参阅针对扩展的 REST API 文档For more information on the forceUpdateTag, see the REST API documentation for extensions. 请注意,forceUpdateTag 可用于所有扩展,而不仅仅是自定义脚本扩展。Note that the forceUpdateTag can be used with all extensions, not just the custom script extension.

通过自定义映像来部署应用程序也很常见。It's also common for applications to be deployed through a custom image. 以下部分介绍此情景。This scenario is covered in the following section.

操作系统更新OS Updates

如果使用 Azure 平台映像,可以通过修改 imageReference 来更新映像(有关详细信息,请参阅 REST API 文档)。If you use Azure platform images, you can update the image by modifying the imageReference (more information, see the REST API documentation).

备注

使用平台映像时,通常指定 "latest" 作为映像引用版本。With platform images, it is common to specify "latest" for the image reference version. 在你执行创建、横向扩展和重置映像操作时,将使用最新发布的脚本创建 VM。When you create, scale out, and reimage, VMs are created with the latest available version. 但是,这并不意味着 OS 映像会随新映像版本的发布自动进行更新。However, it does not mean that the OS image is automatically updated over time as new image versions are released. 当前处于预览版状态的一个独立功能提供了自动 OS 升级功能。A separate feature is currently in preview that provides automatic OS upgrades.

如果使用自定义映像,可以通过更新 imageReference ID 来更新映像(有关详细信息,请参阅 REST API 文档)。If you use custom images, you can update the image by updating the imageReference ID (more information, see the REST API documentation).

示例Examples

更新规模集的 OS 映像Update the OS image for your scale set

你可能具有运行旧版 Ubuntu LTS 16.04 的规模集。You may have a scale set that runs an old version of Ubuntu LTS 16.04. 你希望将其更新到新版 Ubuntu LTS 16.04,例如版本 16.04.201801090You want to update to a newer version of Ubuntu LTS 16.04, such as version 16.04.201801090. 映像引用版本属性不是列表的一部分,因此可以使用下列命令之一直接修改这些属性:The image reference version property is not part of a list, so you can directly modify these properties with one of the following commands:

  • 如下所示,通过 Azure PowerShell 使用 Update-AzVmssAzure PowerShell with Update-AzVmss as follows:

    Update-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -ImageReferenceVersion 16.04.201801090
    
  • 通过 Azure CLI 使用 az vmss updateAzure CLI with az vmss update:

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

或者,你可能想要更改规模集使用的映像。Alternatively, you may want to change the image your scale set uses. 例如,你可能想要更新或更改规模集使用的自定义映像。For example, you may want to update or change a custom image used by your scale set. 可以通过更新“映像引用 ID”属性来更改规模集使用的映像。You can change the image your scale set uses by updating the image reference ID property. “映像引用 ID”属性不是列表的一部分,因此可以使用下列命令之一直接修改该属性:The image reference ID property is not part of a list, so you can directly modify this property with one of the following commands:

  • 如下所示,通过 Azure PowerShell 使用 Update-AzVmssAzure PowerShell with Update-AzVmss as follows:

    Update-AzVmss `
        -ResourceGroupName "myResourceGroup" `
        -VMScaleSetName "myScaleSet" `
        -ImageReferenceId /subscriptions/{subscriptionID}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/images/myNewImage
    
  • 通过 Azure CLI 使用 az vmss updateAzure CLI with 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
    

更新规模集的负载均衡器Update the load balancer for your scale set

假设在规模集带有 Azure 负载均衡器的情况下,需要将 Azure 负载均衡器替换为 Azure 应用程序网关。Let's say you have a scale set with an Azure Load Balancer, and you want to replace the Azure Load Balancer with an Azure Application Gateway. 规模集的负载均衡器和应用程序网关属性是列表的一部分,因此可以使用以下命令来删除或添加列表元素,而不必直接修改属性:The load balancer and Application Gateway properties for a scale set are part of a list, so you can use the commands to remove or add list elements instead of modifying the properties directly:

  • Azure Powershell: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 "myNic" -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 $vmss
    
  • Azure CLI:Azure 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 配置和负载均衡器。These commands assume there is only one IP configuration and load balancer on the scale set. 如果有多个,则可能需要使用 0 之外的列表索引。If there are multiple, you may need to use a list index other than 0.

后续步骤Next steps

还可以使用 Azure CLIAzure PowerShell 对规模集执行常见的管理任务。You can also perform common management tasks on scale sets with the Azure CLI or Azure PowerShell.