对 Azure 虚拟机规模集使用自定义横向缩减策略Use custom scale-in policies with Azure virtual machine scale sets

可根据一系列指标(包括平台指标和用户定义的自定义指标)横向扩展或缩减虚拟机规模集部署。A virtual machine scale set deployment can be scaled-out or scaled-in based on an array of metrics, including platform and user-defined custom metrics. 在规模集工作负荷的演变过程中,横向扩展可根据规模集模型创建新的虚拟机,而横向缩减会影响正在运行的采用不同配置和/或功能的虚拟机。While a scale-out creates new virtual machines based on the scale set model, a scale-in affects running virtual machines that may have different configurations and/or functions as the scale set workload evolves.

横向缩减策略功能提供三项横向缩减配置,可让用户配置虚拟机的横向缩减顺序:The scale-in policy feature provides users a way to configure the order in which virtual machines are scaled-in, by way of three scale-in configurations:

  1. 默认Default
  2. NewestVMNewestVM
  3. OldestVMOldestVM

Default 横向缩减策略Default scale-in policy

默认情况下,虚拟机规模集会应用此策略来确定要横向缩减的实例。By default, virtual machine scale set applies this policy to determine which instance(s) will be scaled in. 使用 Default 策略时,将按以下顺序选择要横向缩减的 VM:With the Default policy, VMs are selected for scale-in in the following order:

  1. 跨可用性区域均衡虚拟机(如果规模集部署在区域配置中)Balance virtual machines across availability zones (if the scale set is deployed in zonal configuration)
  2. 跨容错域均衡虚拟机(尽力而为)Balance virtual machines across fault domains (best effort)
  3. 删除实例 ID 最大的虚拟机Delete virtual machine with the highest instance ID

如果用户只想遵循默认排序,他们无需指定横向缩减策略。Users do not need to specify a scale-in policy if they just want the default ordering to be followed.

请注意,跨可用性区域或容错域的均衡不会在可用性区域或容错域之间移动实例。Note that balancing across availability zones or fault domains does not move instances across availability zones or fault domains. 均衡的实现方式是从不均衡的可用性区域或容错域中删除虚拟机,直到虚拟机的分布均衡。The balancing is achieved through deletion of virtual machines from the unbalanced availability zones or fault domains until the distribution of virtual machines becomes balanced.

NewestVM 横向缩减策略NewestVM scale-in policy

此策略在跨可用性区域均衡 VM 之后删除规模集中创建的最新虚拟机(适用于区域部署)。This policy will delete the newest created virtual machine in the scale set, after balancing VMs across availability zones (for zonal deployments). 若要启用此策略,需要对虚拟机规模集模型进行配置更改。Enabling this policy requires a configuration change on the virtual machine scale set model.

OldestVM 横向缩减策略OldestVM scale-in policy

此策略在跨可用性区域均衡 VM 之后删除规模集中创建的最旧虚拟机(适用于区域部署)。This policy will delete the oldest created virtual machine in the scale set, after balancing VMs across availability zones (for zonal deployments). 若要启用此策略,需要对虚拟机规模集模型进行配置更改。Enabling this policy requires a configuration change on the virtual machine scale set model.

启用横向缩减策略Enabling scale-in policy

横向缩减策略在虚拟机规模集模型中定义。A scale-in policy is defined in the virtual machine scale set model. 如以上部分中所述,使用“NewestVM”和“OldestVM”策略时需要横向缩减策略定义。As noted in the sections above, a scale-in policy definition is needed when using the ‘NewestVM’ and ‘OldestVM’ policies. 如果在规模集模型中找不到横向缩减策略定义,虚拟机规模集将自动使用“Default”横向缩减策略。Virtual machine scale set will automatically use the ‘Default’ scale-in policy if there is no scale-in policy definition found on the scale set model.

可通过以下方式在虚拟机规模集模型上定义横向缩减策略:A scale-in policy can be defined on the virtual machine scale set model in the following ways:

Azure 门户Azure portal

以下步骤定义了创建新规模集时的横向缩减策略。The following steps define the scale-in policy when creating a new scale set.

  1. 转到“虚拟机规模集”。Go to Virtual machine scale sets.
  2. 选择“+ 添加”,创建新的规模集 。Select + Add to create a new scale set.
  3. 转到“缩放”选项卡。 Go to the Scaling tab.
  4. 找到“横向缩减策略”部分。 Locate the Scale-in policy section.
  5. 从下拉列表中选择横向缩减策略。Select a scale-in policy from the drop-down.
  6. 创建完新的规模集后,选择“查看 + 创建”按钮 。When you are done creating the new scale set, select Review + create button.

使用 APIUsing API

使用 API 2019-03-01 对虚拟机规模集执行 PUT:Execute a PUT on the virtual machine scale set using API 2019-03-01:

PUT
https://management.chinacloudapi.cn/subscriptions/<sub-id>/resourceGroups/<myRG>/providers/Microsoft.Compute/virtualMachineScaleSets/<myVMSS>?api-version=2019-03-01

{ 
"location": "<VMSS location>", 
    "properties": { 
        "scaleInPolicy": {  
            "rules": ["OldestVM"]  
        } 
    }    
} 

Azure PowerShellAzure PowerShell

创建一个资源组,然后创建一个新的规模集,并将横向缩减策略设置为 OldestVMCreate a resource group, then create a new scale set with scale-in policy set as OldestVM.

New-AzResourceGroup -ResourceGroupName "myResourceGroup" -Location "<VMSS location>"
New-AzVmss `
  -ResourceGroupName "myResourceGroup" `
  -Location "<VMSS location>" `
  -VMScaleSetName "myScaleSet" `
  -ScaleInPolicy “OldestVM”

Azure CLI 2.0Azure CLI 2.0

以下示例添加了创建新规模集时的横向缩减策略。The following example adds a scale-in policy while creating a new scale set. 首先创建一个资源组,然后创建一个新的规模集,将横向缩减策略设置为 OldestVMFirst create a resource group, then create a new scale set with scale-in policy as OldestVM.

az group create --name <myResourceGroup> --location <VMSSLocation>
az vmss create `
  --resource-group <myResourceGroup> `
  --name <myVMScaleSet> `
  --image UbuntuLTS `
  --admin-username <azureuser> `
  --generate-ssh-keys `
  --scale-in-policy OldestVM

使用模板Using Template

在模板中的“properties”下添加以下代码:In your template, under “properties”, add the following:

"scaleInPolicy": {  
      "rules": ["OldestVM"]  
}

以上代码块指定在触发横向缩减时,虚拟机规模集将删除区域均衡的规模集中的最旧 VM(通过自动缩放或手动删除)。The above blocks specify that the virtual machine scale set will delete the Oldest VM in a zone-balanced scale set, when a scale-in is triggered (through Autoscale or manual delete).

当虚拟机规模集未进行区域均衡时,规模集会先删除跨不均衡区域的 VM。When a virtual machine scale set is not zone balanced, the scale set will first delete VMs across the imbalanced zone(s). 在不均衡的区域中,规模集将使用上面指定的横向缩减策略来确定要横向缩减哪个 VM。Within the imbalanced zones, the scale set will use the scale-in policy specified above to determine which VM to scale in. 在这种情况下,在不均衡的区域中,规模集将选择删除该区域中的最旧 VM。In this case, within an imbalanced zone, the scale set will select the Oldest VM in that zone to be deleted.

对于非区域性虚拟机规模集,策略会选择删除整个规模集中的最旧 VM。For non-zonal virtual machine scale set, the policy selects the oldest VM across the scale set for deletion.

在上述横向缩减策略中使用“NewestVM”时,相同的过程适用。The same process applies when using ‘NewestVM’ in the above scale-in policy.

修改横向缩减策略Modifying scale-in policies

修改横向缩减策略的过程与应用横向缩减策略的过程相同。Modifying the scale-in policy follows the same process as applying the scale-in policy. 例如,如果在上述示例中你要将策略从“OldestVM”更改为“NewestVM”,可执行以下操作:For example, if in the above example, you want to change the policy from ‘OldestVM’ to ‘NewestVM’, you can do so by:

Azure 门户Azure portal

可以通过 Azure 门户修改现有规模集的横向缩减策略。You can modify the scale-in policy of an existing scale set through the Azure portal.

  1. 在现有的虚拟机规模集中,从左侧菜单中选择“缩放” 。In an existing virtual machine scale set, select Scaling from the menu on the left.
  2. 选择“横向缩减策略” 选项卡。Select the Scale-In Policy tab.
  3. 从下拉列表中选择横向缩减策略。Select a scale-in policy from the drop-down.
  4. 完成后,选择“保存” 。When you are done, select Save.

使用 APIUsing API

使用 API 2019-03-01 对虚拟机规模集执行 PUT:Execute a PUT on the virtual machine scale set using API 2019-03-01:

PUT
https://management.chinacloudapi.cn/subscriptions/<sub-id>/resourceGroups/<myRG>/providers/Microsoft.Compute/virtualMachineScaleSets/<myVMSS>?api-version=2019-03-01 

{ 
"location": "<VMSS location>", 
    "properties": { 
        "scaleInPolicy": {  
            "rules": ["NewestVM"]  
        } 
    }    
}

Azure PowerShellAzure PowerShell

更新现有规模集的横向缩减策略:Update the scale-in policy of an existing scale set:

Update-AzVmss `
 -ResourceGroupName "myResourceGroup" `
 -VMScaleSetName "myScaleSet" `
 -ScaleInPolicy “OldestVM”

Azure CLI 2.0Azure CLI 2.0

下面是一个示例,演示了如何更新现有规模集的横向缩减策略:The following is an example for updating the scale-in policy of an existing scale set:

az vmss update `
  --resource-group <myResourceGroup> `
  --name <myVMScaleSet> `
  --scale-in-policy OldestVM

使用模板Using Template

在模板中的“properties”下,按如下所示修改模板并重新部署:In your template, under “properties”, modify the template as below and redeploy:

"scaleInPolicy": {  
      "rules": ["NewestVM"]  
} 

如果你决定将“NewestVM”更改为“Default”或“OldestVM”,可应用相同的过程The same process will apply if you decide to change ‘NewestVM’ to ‘Default’ or ‘OldestVM’

实例保护和横向缩减策略Instance protection and scale-in policy

虚拟机规模集提供两种类型的实例保护Virtual machine scale sets provide two types of instance protection:

  1. 防止进行横向缩减Protect from scale-in
  2. 防止进行规模集操作Protect from scale-set actions

无论应用了哪个横向缩减策略,都不会通过横向缩减操作删除受保护的虚拟机。A protected virtual machine is not deleted through a scale-in action, regardless of the scale-in policy applied. 例如,如果防止横向缩减 VM_0(规模集中的最旧 VM),并为规模集启用了“OldestVM”横向缩减策略,则不考虑横向缩减 VM_0,尽管它是规模集中的最旧 VM。For example, if VM_0 (oldest VM in the scale set) is protected from scale-in, and the scale set has ‘OldestVM’ scale-in policy enabled, VM_0 will not be considered for being scaled in, even though it is the oldest VM in the scale set.

无论对规模集启用了哪个横向缩减策略,用户都随时可以手动删除受保护的虚拟机。A protected virtual machine can be manually deleted by the user at any time, regardless of the scale-in policy enabled on the scale set.

用法示例Usage examples

以下示例演示了当触发横向缩减事件时,虚拟机规模集如何选择要删除的 VM。The below examples demonstrate how a virtual machine scale set will select VMs to be deleted when a scale-in event is triggered. 假设实例 ID 最大的虚拟机为规模集中的最新 VM,实例 ID 最小的 VM 为规模集中的最旧 VM。Virtual machines with the highest instance IDs are assumed to be the newest VMs in the scale set and the VMs with the smallest instance IDs are assumed to be the oldest VMs in the scale set.

OldestVM 横向缩减策略OldestVM scale-in policy

事件Event 区域 1 中的实例 IDInstance IDs in Zone1 区域 2 中的实例 IDInstance IDs in Zone2 区域 3 中的实例 IDInstance IDs in Zone3 横向缩减选择Scale-in Selection
初始Initial 3、4、5、103, 4, 5, 10 2、6、9、112, 6, 9, 11 1、7、81, 7, 8
横向缩减Scale-in 3、4、5、103, 4, 5, 10 2、6、9、112, 6, 9, 11 1、7、81, 7, 8 尽管区域 3 包含最旧的 VM,但仍会在区域 1 和 2 之间选择。Choose between Zone 1 and 2, even though Zone 3 has the oldest VM. 删除区域 2 中的 VM2,因为它是该区域中的最旧 VM。Delete VM2 from Zone 2 as it is the oldest VM in that zone.
横向缩减Scale-in 3、4、5、103, 4, 5, 10 6、9、116, 9, 11 1、7、81, 7, 8 尽管区域 3 包含最旧的 VM,但仍会选择区域 1。Choose Zone 1 even though Zone 3 has the oldest VM. 删除区域 1 中的 VM3,因为它是该区域中的最旧 VM。Delete VM3 from Zone 1 as it is the oldest VM in that zone.
横向缩减Scale-in 4、5、104, 5, 10 6、9、116, 9, 11 1、7、81, 7, 8 区域已均衡。Zones are balanced. 删除区域 3 中的 VM1,因为它是规模集中的最旧 VM。Delete VM1 in Zone 3 as it is the oldest VM in the scale set.
横向缩减Scale-in 4、5、104, 5, 10 6、9、116, 9, 11 7, 87, 8 在区域 1 和区域 2 之间选择。Choose between Zone 1 and Zone 2. 删除区域 1 中的 VM4,因为它是两个区域中的最旧 VM。Delete VM4 in Zone 1 as it is the oldest VM across the two Zones.
横向缩减Scale-in 5、105, 10 6、9、116, 9, 11 7, 87, 8 尽管区域 1 包含最旧的 VM,但仍会选择区域 2。Choose Zone 2 even though Zone 1 has the oldest VM. 删除区域 1 中的 VM6,因为它是该区域中的最旧 VM。Delete VM6 in Zone 1 as it is the oldest VM in that zone.
横向缩减Scale-in 5、105, 10 9、119, 11 7, 87, 8 区域已均衡。Zones are balanced. 删除区域 1 中的 VM5,因为它是规模集中的最旧 VM。Delete VM5 in Zone 1 as it is the oldest VM in the scale set.

对于非区域性虚拟机规模集,策略会选择删除整个规模集中的最旧 VM。For non-zonal virtual machine scale sets, the policy selects the oldest VM across the scale set for deletion. 将跳过任何“受保护”VM 的删除。Any “protected” VM will be skipped for deletion.

NewestVM 横向缩减策略NewestVM scale-in policy

事件Event 区域 1 中的实例 IDInstance IDs in Zone1 区域 2 中的实例 IDInstance IDs in Zone2 区域 3 中的实例 IDInstance IDs in Zone3 横向缩减选择Scale-in Selection
初始Initial 3、4、5、103, 4, 5, 10 2、6、9、112, 6, 9, 11 1、7、81, 7, 8
横向缩减Scale-in 3、4、5、103, 4, 5, 10 2、6、9、112, 6, 9, 11 1、7、81, 7, 8 在区域 1 和 2 之间选择。Choose between Zone 1 and 2. 删除区域 2 中的 VM11,因为它是两个区域中的最新 VM。Delete VM11 from Zone 2 as it is the newest VM across the two zones.
横向缩减Scale-in 3、4、5、103, 4, 5, 10 2、6、92, 6, 9 1、7、81, 7, 8 选择区域 1,因为它包含的 VM 比其他两个区域要多。Choose Zone 1 as it has more VMs than the other two zones. 删除区域 1 中的 VM10,因为它是该区域中的最新 VM。Delete VM10 from Zone 1 as it is the newest VM in that Zone.
横向缩减Scale-in 3、4、53, 4, 5 2、6、92, 6, 9 1、7、81, 7, 8 区域已均衡。Zones are balanced. 删除区域 2 中的 VM9,因为它是规模集中的最新 VM。Delete VM9 in Zone 2 as it is the newest VM in the scale set.
横向缩减Scale-in 3、4、53, 4, 5 2、62, 6 1、7、81, 7, 8 在区域 1 和区域 3 之间选择。Choose between Zone 1 and Zone 3. 删除区域 3 中的 VM8,因为它是该区域中的最新 VM。Delete VM8 in Zone 3 as it is the newest VM in that Zone.
横向缩减Scale-in 3、4、53, 4, 5 2、62, 6 1、71, 7 尽管区域 3 包含最新的 VM,但仍会选择区域 1。Choose Zone 1 even though Zone 3 has the newest VM. 删除区域 1 中的 VM5,因为它是该区域中的最新 VM。Delete VM5 in Zone 1 as it is the newest VM in that Zone.
横向缩减Scale-in 3、43, 4 2、62, 6 1、71, 7 区域已均衡。Zones are balanced. 删除区域 3 中的 VM7,因为它是规模集中的最新 VM。Delete VM7 in Zone 3 as it is the newest VM in the scale set.

对于非区域性虚拟机规模集,策略会选择删除整个规模集中的最新 VM。For non-zonal virtual machine scale sets, the policy selects the newest VM across the scale set for deletion. 将跳过任何“受保护”VM 的删除。Any “protected” VM will be skipped for deletion.

故障排除Troubleshoot

  1. 启用横向缩减策略失败。如果收到“错误的请求”错误并出现错误消息“在 'properties' 类型的对象中找不到成员 'scaleInPolicy'”,请检查虚拟机规模集使用的 API 版本。Failure to enable scaleInPolicy If you get a ‘BadRequest’ error with an error message stating "Could not find member 'scaleInPolicy' on object of type 'properties'”, then check the API version used for virtual machine scale set. 此功能需要 API 版本 2019-03-01 或更高版本。API version 2019-03-01 or higher is required for this feature.

  2. 错误地选择了要横向缩减的 VM。请参阅上述示例。Wrong selection of VMs for scale-in Refer to the examples above. 如果虚拟机规模集是区域性部署,则会先对不均衡的区域应用横向缩减策略,并在区域均衡后,对整个规模集应用该策略。If your virtual machine scale set is a Zonal deployment, scale-in policy is applied first to the imbalanced Zones and then across the scale set once it is zone balanced. 如果横向缩减的顺序与上述示例不一致,请咨询虚拟机规模集团队进行故障排除。If the order of scale-in is not consistent with the examples above, raise a query with the virtual machine scale set team for troubleshooting.

后续步骤Next steps

了解如何在虚拟机规模集上部署应用程序Learn how to deploy your application on virtual machine scale sets.