自动缩放可确保运行适当数量的资源来处理应用程序负载的波动。 可以使用 Azure 门户、Azure CLI、Azure PowerShell或 ARM 或 Bicep 模板来配置自动缩放。
本文将介绍如何使用 PowerShell 为虚拟机规模集配置自动缩放。 配置使用以下步骤:
- 创建可自动缩放的虚拟机规模集。
- 创建规则以缩容和横向扩展。
- 创建一个使用你的规则的配置文件。
- 应用自动缩放设置。
- 更新自动缩放设置,并设置通知。
先决条件
若要使用 PowerShell 配置自动缩放,则需要具有活动订阅的 Azure 帐户。 可创建试用帐户。
配置你的环境
Note
在已选择目标订阅的同一 PowerShell 会话中运行示例。
# Set variables
$resourceGroupName = "<ResourceGroupName>"
$vmssName = "<VirtualMachineScaleSetName>"
$autoscaleName = "<AutoscaleSettingName>"
$azureRegion = "<AzureRegion>"
$virtualNetworkName = "<VirtualNetworkName>"
$subnetName = "<SubnetName>"
$publicIpAddressName = "<PublicIpAddressName>"
$loadBalancerName = "<LoadBalancerName>"
# Get the subscription ID from the current Azure PowerShell context
$subscriptionId = (Get-AzContext).Subscription.Id
# Build virtual machine scale set resource ID
$vmssPath = "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName"
$vmssProvider = "Microsoft.Compute/virtualMachineScaleSets/$vmssName"
$vmssResourceId = "$vmssPath/providers/$vmssProvider"
创建虚拟机规模集
以下 Azure PowerShell 示例使用 New-AzResourceGroup 和 New-AzVmss cmdlet。
Note
使用此示例的专用资源组,以便在完成后删除其资源。
该示例创建公共 IP 地址。 若要进行管理访问,请使用Azure Bastion,而不是向 Internet 公开 RDP 或 SSH 端口。
# Create the resource group
New-AzResourceGroup -ResourceGroupName $resourceGroupName -Location $azureRegion
# Get credentials for the virtual machine scale set
$vmCredential = Get-Credential
# Define parameters for New-AzVmss
$newAzVmssParams = @{
ResourceGroupName = $resourceGroupName
Location = $azureRegion
VMScaleSetName = $vmssName
Credential = $vmCredential
VirtualNetworkName = $virtualNetworkName
SubnetName = $subnetName
PublicIpAddressName = $publicIpAddressName
LoadBalancerName = $loadBalancerName
OrchestrationMode = "Flexible"
}
# Create the virtual machine scale set
New-AzVmss @newAzVmssParams
创建自动缩放设置
若要使用 PowerShell 创建自动缩放设置,请遵循以下顺序:
- 使用
New-AzAutoscaleScaleRuleObject创建规则。 - 使用
New-AzAutoscaleProfileObject创建个人资料。 - 使用
New-AzAutoscaleSetting.. 创建自动缩放设置。 - 使用
Update-AzAutoscaleSetting.. 更新设置。
创建规则
创建横向缩减和横向扩展规则,然后将其与配置文件相关联。
以下Azure PowerShell示例使用 New-AzAutoscaleScaleRuleObject cmdlet。
Note
它创建两个规则:
- 当 CPU 百分比超过 70%时横向扩展。
- 当 CPU 百分比小于 30% 时进行横向缩容。
# Define parameters for New-AzAutoscaleScaleRuleObject
$newAzAutoscaleScaleRuleObjectParams = @{
MetricTriggerMetricName = "Percentage CPU"
MetricTriggerMetricResourceUri = $vmssResourceId
MetricTriggerTimeGrain = New-TimeSpan -Minutes 1
MetricTriggerStatistic = "Average"
MetricTriggerTimeWindow = New-TimeSpan -Minutes 5
MetricTriggerTimeAggregation = "Average"
MetricTriggerOperator = "GreaterThan"
MetricTriggerThreshold = 70
MetricTriggerDividePerInstance = $false
ScaleActionDirection = "Increase"
ScaleActionType = "ChangeCount"
ScaleActionValue = "1"
ScaleActionCooldown = New-TimeSpan -Minutes 5
}
$scaleOutRule = New-AzAutoscaleScaleRuleObject @newAzAutoscaleScaleRuleObjectParams
# Define parameters for New-AzAutoscaleScaleRuleObject
$newAzAutoscaleScaleRuleObjectParams = @{
MetricTriggerMetricName = "Percentage CPU"
MetricTriggerMetricResourceUri = $vmssResourceId
MetricTriggerTimeGrain = New-TimeSpan -Minutes 1
MetricTriggerStatistic = "Average"
MetricTriggerTimeWindow = New-TimeSpan -Minutes 5
MetricTriggerTimeAggregation = "Average"
MetricTriggerOperator = "LessThan"
MetricTriggerThreshold = 30
MetricTriggerDividePerInstance = $false
ScaleActionDirection = "Decrease"
ScaleActionType = "ChangeCount"
ScaleActionValue = "1"
ScaleActionCooldown = New-TimeSpan -Minutes 5
}
$scaleInRule = New-AzAutoscaleScaleRuleObject @newAzAutoscaleScaleRuleObjectParams
下表描述了 New-AzAutoscaleScaleRuleObject cmdlet 中使用的参数。
| 参数 | 说明 |
|---|---|
MetricTriggerMetricName |
设置自动缩放触发指标 |
MetricTriggerMetricResourceUri |
指定 MetricTriggerMetricName 指标所属的资源。
MetricTriggerMetricResourceUri 可以是任何资源,而不仅仅是要缩放的资源。 例如,可以根据负载均衡器、数据库或规模集本身创建的指标缩放虚拟机规模集。 对于指定的 MetricTriggerMetricName,MetricTriggerMetricResourceUri 必须存在。 |
MetricTriggerTimeGrain |
规则监视的指标的采样频率。
MetricTriggerTimeGrain 必须是指定指标的预定义值之一,并且必须介于 12 小时到 1 分钟之间。 例如,MetricTriggerTimeGrain = PT1M"* 表示每 1 分钟对指标进行采样,并使用 MetricTriggerStatistic 中指定的聚合方法进行聚合。 |
MetricTriggerTimeAggregation |
timeGrain 时间段内的聚合方法。 例如,statistic = "Average" 且 timeGrain = "PT1M" 表示每分钟取平均值来聚合指标。 |
MetricTriggerStatistic |
用于聚合已采样指标的聚合方法。 例如,TimeAggregation = "Average" 表示通过对采样指标取平均值进行聚合。 |
MetricTriggerTimeWindow |
自动缩放引擎回溯聚合指标的时间量。 此值必须大于指标集合的延迟,这因资源而异。 它必须介于 5 分钟和 12 个小时之间。 例如,如果该时间为 10 分钟,就表示自动缩放每次运行时,都会查询过去 10 分钟的指标。 该功能可以将指标稳定化,避免对暂时性的峰值作出反应。 |
MetricTriggerThreshold |
定义触发缩放事件的指标值。 |
MetricTriggerOperator |
指定计算指标值时要使用的逻辑比较操作。 |
MetricTriggerDividePerInstance |
设置为 true 时,触发器指标将除以实例总数。 例如,如果消息计数为 300 且有 5 个实例正在运行,则计算的指标值为每个实例 60 条消息。 此属性并不适用于所有指标。 |
ScaleActionDirection |
指定向内缩放或向外缩放。有效值为 Increase 和 Decrease。 |
ScaleActionType |
按特定实例数量伸缩、伸缩到特定实例数,或按当前实例数的百分比伸缩。 有效值包括 ChangeCount、ExactCount 和 PercentChangeCount。 |
ScaleActionCooldown |
在缩放操作之后必须经过的最小时间,此规则才有资格启动另一个缩放操作。 自动缩放会单独检查每个规则的冷却时间。 冷却时间可使各项指标趋于稳定,并避免抖动。 |
创建默认的自动缩放配置文件并关联规则
定义量表规则后,创建配置文件。 配置文件指定默认实例计数、实例计数上限和下限,以及可以应用关联规则的时间。
以下Azure PowerShell示例使用 New-AzAutoscaleProfileObject cmdlet。
Note
由于此配置文件是默认配置文件,因此它没有任何计划参数。 默认配置文件在没有其他配置文件处于活动状态时处于活动状态。
# Define parameters for New-AzAutoscaleProfileObject
$newAzAutoscaleProfileObjectParams = @{
Name = "default"
CapacityDefault = "1"
CapacityMaximum = "10"
CapacityMinimum = "1"
Rule = @($scaleOutRule, $scaleInRule)
}
$defaultProfile = New-AzAutoscaleProfileObject @newAzAutoscaleProfileObjectParams
下表描述了 New-AzAutoscaleProfileObject cmdlet 中使用的参数。
| 参数 | 说明 |
|---|---|
CapacityDefault |
当指标不可用于评估时的实例数量。 仅当当前实例计数低于默认值时,才使用默认值。 |
CapacityMaximum |
资源的最大实例数。 最大实例数还受订阅中可用内核数量的限制。 |
CapacityMinimum |
资源的最小实例数。 |
FixedDateEnd |
采用 ISO 8601 格式的配置文件的结束时间。 |
FixedDateStart |
配置文件的开始时间,采用 ISO 8601 格式表示。 |
Rule |
当此配置文件处于活动状态时,为缩放操作提供触发器和参数的规则的集合。 最多可以指定 10 个以逗号分隔的规则。 |
RecurrenceFrequency |
计划任务配置文件的生效频率。 此值必须为 week。 |
ScheduleDay |
指定重复计划时配置文件生效日期的集合。 可能的值包括周日到周六。 有关定期计划的详细信息,请参阅 使用 PowerShell 的重复性配置。 |
ScheduleHour |
配置文件生效的时间集合。 支持的值为 0 到 23。 |
ScheduleMinute |
配置文件生效时间点(按分钟计)的集合。 |
ScheduleTimeZone |
配置文件小时数的时区。 |
应用自动缩放设置
定义规则和配置文件后,应用自动缩放设置。 若要更新现有的自动缩放设置,请使用 Update-AzAutoscaleSetting。
以下Azure PowerShell示例使用 New-AzAutoscaleSetting cmdlet。
# Define parameters for New-AzAutoscaleSetting
$newAzAutoscaleSettingParams = @{
Name = $autoscaleName
ResourceGroupName = $resourceGroupName
Location = $azureRegion
Profile = @($defaultProfile)
Enabled = $true
PropertiesName = $autoscaleName
TargetResourceUri = $vmssResourceId
}
# Create the autoscale setting
New-AzAutoscaleSetting @newAzAutoscaleSettingParams
为自动缩放设置添加通知
将通知添加到自动缩放设置,以触发 Webhook 或在发生缩放事件时发送电子邮件通知。
以下Azure PowerShell示例使用 New-AzAutoscaleWebhookNotificationObject cmdlet。
# Set variables
$webhookUri = "<WebhookUri>"
# Create the webhook notification
$webhook = New-AzAutoscaleWebhookNotificationObject -Property @{} -ServiceUri $webhookUri
以下Azure PowerShell示例使用 New-AzAutoscaleNotificationObject cmdlet 配置 Webhook 和电子邮件通知。
Note
对 Webhook 使用 HTTPS 端点。
# Set variables
$notificationEmail = "<NotificationEmail>"
# Define parameters for New-AzAutoscaleNotificationObject
$newAzAutoscaleNotificationObjectParams = @{
EmailCustomEmail = @($notificationEmail)
EmailSendToSubscriptionAdministrator = $true
EmailSendToSubscriptionCoAdministrator = $true
Webhook = @($webhook)
}
$notification = New-AzAutoscaleNotificationObject @newAzAutoscaleNotificationObjectParams
以下 Azure PowerShell 示例使用 Update-AzAutoscaleSetting cmdlet 应用该通知设置。
# Define parameters for Update-AzAutoscaleSetting
$updateAzAutoscaleSettingParams = @{
Name = $autoscaleName
ResourceGroupName = $resourceGroupName
Notification = @($notification)
}
# Update the autoscale notification
Update-AzAutoscaleSetting @updateAzAutoscaleSettingParams
查看自动缩放设置
以下Azure PowerShell示例使用 Get-AzAutoscaleSetting cmdlet 检索自动缩放设置。
# Define parameters for Get-AzAutoscaleSetting
$getAzAutoscaleSettingParams = @{
ResourceGroupName = $resourceGroupName
Name = $autoscaleName
}
$autoscaleSetting = Get-AzAutoscaleSetting @getAzAutoscaleSettingParams
$autoscaleSetting | Select-Object -Property *
以下Azure PowerShell示例使用 Get-AzAutoscaleHistory cmdlet。
Note
它查询自动缩放设置的历史记录,而不是缩放集的资源 ID。
# Build autoscale setting resource ID
$autoscalePath = "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName"
$autoscaleProvider = "Microsoft.Insights/autoscaleSettings/$autoscaleName"
$autoscaleResourceId = "$autoscalePath/providers/$autoscaleProvider"
# Retrieve the autoscale history
Get-AzAutoscaleHistory -ResourceId $autoscaleResourceId
计划的配置文件和重复的配置文件
Note
更新 Profile时,请包含要保留的每个配置文件。 以下示例将配置文件添加到本文前面创建的设置。 将任何其他配置调整为另一个自动缩放设置时保留它们。
为特殊事件添加计划配置文件
设置自动缩放配置,以便针对特定事件采用不同的缩放方式。 例如,在某一天需求将高于平时时,创建一个提高了最大和最小实例数限制的配置文件。
以下 Azure PowerShell 示例使用 New-AzAutoscaleProfileObject 和 Update-AzAutoscaleSetting cmdlet。
Note
以下示例使用与前面定义的默认配置文件相同的规则,但为特定日期设置新的实例限制。 还可以配置不同的规则以用于新配置文件。 开始值和结束值使用 UTC 格式的 ISO 8601 时间戳。
# Set variables
$startTime = "<StartTime>"
$endTime = "<EndTime>"
# Define parameters for New-AzAutoscaleProfileObject
$newAzAutoscaleProfileObjectParams = @{
Name = "High-demand-day"
CapacityDefault = "7"
CapacityMaximum = "30"
CapacityMinimum = "5"
FixedDateEnd = [datetime]::Parse($endTime)
FixedDateStart = [datetime]::Parse($startTime)
FixedDateTimeZone = "UTC"
Rule = @($scaleOutRule, $scaleInRule)
}
$highDemandDay = New-AzAutoscaleProfileObject @newAzAutoscaleProfileObjectParams
# Define parameters for Update-AzAutoscaleSetting
$updateAzAutoscaleSettingParams = @{
Name = $autoscaleName
ResourceGroupName = $resourceGroupName
Profile = @($defaultProfile, $highDemandDay)
}
# Update the autoscale profiles
Update-AzAutoscaleSetting @updateAzAutoscaleSettingParams
添加定期计划配置文件
使用定期配置文件可以计划每周重复的缩放配置文件。
计划配置文件具有开始和结束日期,定期配置文件则没有结束时间。 配置文件会一直处于活动状态,直到下一个配置文件的开始时间。 因此,在创建定期配置文件时,必须创建一个定期默认配置文件,该配置文件在希望上一个定期配置文件完成时启动。
以下 Azure PowerShell 示例使用 New-AzAutoscaleProfileObject 和 Update-AzAutoscaleSetting cmdlet。
Note
例如,在周末期间,从周五晚上到周一早上,将实例数缩减为 1 个。 若要配置周末配置文件,该配置文件从星期五晚上开始,并在星期一早上结束,请创建一个从周五晚上开始的配置文件,然后使用从星期一早上开始的默认设置创建定期配置文件。
它会创建周末配置文件和一个循环默认配置文件,用于结束周末配置文件,同时保留前面步骤中的默认配置文件和固定日期配置文件。
# Define parameters for New-AzAutoscaleProfileObject
$newAzAutoscaleProfileObjectParams = @{
Name = "Weekend"
CapacityDefault = "1"
CapacityMaximum = "1"
CapacityMinimum = "1"
RecurrenceFrequency = "Week"
ScheduleDay = @("Friday")
ScheduleHour = @(22)
ScheduleMinute = @(0)
ScheduleTimeZone = "Pacific Standard Time"
Rule = @($scaleOutRule, $scaleInRule)
}
$fridayProfile = New-AzAutoscaleProfileObject @newAzAutoscaleProfileObjectParams
# Define parameters for New-AzAutoscaleProfileObject
$newAzAutoscaleProfileObjectParams = @{
Name = "default recurring profile"
CapacityDefault = "2"
CapacityMaximum = "10"
CapacityMinimum = "2"
RecurrenceFrequency = "Week"
ScheduleDay = @("Monday")
ScheduleHour = @(0)
ScheduleMinute = @(0)
ScheduleTimeZone = "Pacific Standard Time"
Rule = @($scaleOutRule, $scaleInRule)
}
$defaultRecurringProfile = New-AzAutoscaleProfileObject @newAzAutoscaleProfileObjectParams
# Define parameters for Update-AzAutoscaleSetting
$updateAzAutoscaleSettingParams = @{
Name = $autoscaleName
ResourceGroupName = $resourceGroupName
Profile = @(
$defaultProfile
$highDemandDay
$defaultRecurringProfile
$fridayProfile
)
}
# Update the autoscale profiles
Update-AzAutoscaleSetting @updateAzAutoscaleSettingParams
有关计划配置文件的详细信息,请参阅 具有多个配置文件的自动缩放。
其他自动缩放命令
有关自动缩放的 PowerShell cmdlet 的完整列表,请参阅 PowerShell 模块浏览器。
清理资源
若要清理在本教程中创建的资源,请删除创建的专用资源组。 以下 cmdlet 将删除资源组及其所有资源。
以下Azure PowerShell示例使用 Remove-AzResourceGroup cmdlet。
# Delete the example resource group
Remove-AzResourceGroup -Name $resourceGroupName