使用 PowerShell 設定自動調整
自動調整設定可協助您確保執行中的資源數量正確,能夠處理變動的應用程式負載。 您可以使用 Azure 入口網站、Azure CLI、PowerShell 或 ARM 或 Bicep 範本來設定自動調整。
本文說明如何使用下列步驟,使用 PowerShell 設定虛擬機器擴展集的自動調整:
- 建立您可以自動調整的擴展集
- 建立規則以相應縮小和相應放大
- 建立使用規則的設定檔
- 套用自動調整設定
- 使用通知更新您的自動調整設定
必要條件
若要使用 PowerShell 設定自動調整,您需要具有作用中訂用帳戶的 Azure 帳戶。 您可以免費建立帳戶。
設定環境
#Set the subscription Id, VMSS name, and resource group name
$subscriptionId = (Get-AzContext).Subscription.Id
$resourceGroupName="rg-powershell-autoscale"
$vmssName="vmss-001"
建立虛擬機器擴展集
使用下列 Cmdlet 建立擴展集。 $resourceGroupName
設定 和 $vmssName
變數以符合您的環境。
# create a new resource group
New-AzResourceGroup -ResourceGroupName $resourceGroupName -Location "EastUS"
# Create login credentials for the VMSS
$Cred = Get-Credential
$vmCred = New-Object System.Management.Automation.PSCredential($Cred.UserName, $Cred.Password)
New-AzVmss `
-ResourceGroupName $resourceGroupName `
-Location "EastUS" `
-VMScaleSetName $vmssName `
-Credential $vmCred `
-VirtualNetworkName "myVnet" `
-SubnetName "mySubnet" `
-PublicIpAddressName "myPublicIPAddress" `
-LoadBalancerName "myLoadBalancer" `
-OrchestrationMode "Flexible"
建立自動調整設定
若要使用 PowerShell 建立自動調整設定,請遵循下列順序:
- 使用 建立規則
New-AzAutoscaleScaleRuleObject
- 使用 建立設定檔
New-AzAutoscaleProfileObject
- 使用 建立自動調整設定
New-AzAutoscaleSetting
- 使用 更新設定
Update-AzAutoscaleSetting
建立規則
建立相應縮小和相應放大規則,然後將它們與設定檔相關聯。
規則是使用 建立的 New-AzAutoscaleScaleRuleObject
。
下列 PowerShell 腳本會建立兩個規則。
- 當百分比 CPU 超過 70% 時相應放大
- 當百分比 CPU 小於 30% 時相應縮小
$rule1=New-AzAutoscaleScaleRuleObject `
-MetricTriggerMetricName "Percentage CPU" `
-MetricTriggerMetricResourceUri "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/$vmssName" `
-MetricTriggerTimeGrain ([System.TimeSpan]::New(0,1,0)) `
-MetricTriggerStatistic "Average" `
-MetricTriggerTimeWindow ([System.TimeSpan]::New(0,5,0)) `
-MetricTriggerTimeAggregation "Average" `
-MetricTriggerOperator "GreaterThan" `
-MetricTriggerThreshold 70 `
-MetricTriggerDividePerInstance $false `
-ScaleActionDirection "Increase" `
-ScaleActionType "ChangeCount" `
-ScaleActionValue 1 `
-ScaleActionCooldown ([System.TimeSpan]::New(0,5,0))
$rule2=New-AzAutoscaleScaleRuleObject `
-MetricTriggerMetricName "Percentage CPU" `
-MetricTriggerMetricResourceUri "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/$vmssName" `
-MetricTriggerTimeGrain ([System.TimeSpan]::New(0,1,0)) `
-MetricTriggerStatistic "Average" `
-MetricTriggerTimeWindow ([System.TimeSpan]::New(0,5,0)) `
-MetricTriggerTimeAggregation "Average" `
-MetricTriggerOperator "LessThan" `
-MetricTriggerThreshold 30 `
-MetricTriggerDividePerInstance $false `
-ScaleActionDirection "Decrease" `
-ScaleActionType "ChangeCount" `
-ScaleActionValue 1 `
-ScaleActionCooldown ([System.TimeSpan]::New(0,5,0))
下表描述 Cmdlet 中使用的 New-AzAutoscaleScaleRuleObject
參數。
參數 | 描述 |
---|---|
MetricTriggerMetricName |
設定自動調整觸發程式計量 |
MetricTriggerMetricResourceUri |
指定計量所屬的資源 MetricTriggerMetricName 。 MetricTriggerMetricResourceUri 可以是任何資源,而不只是正在調整的資源。 例如,您可以根據負載平衡器、資料庫或擴展集本身所建立的計量來調整虛擬機器擴展集。 MetricTriggerMetricName 指定的 必須存在 MetricTriggerMetricResourceUri 。 |
MetricTriggerTimeGrain |
規則所監視計量的取樣頻率。 MetricTriggerTimeGrain 必須是指定計量的其中一個預先定義值,而且必須介於 12 小時到 1 分鐘之間。 例如, MetricTriggerTimeGrain = PT1M「* 表示計量每隔 1 分鐘取樣一次,並使用 中指定的 MetricTriggerStatistic 匯總方法進行匯總。 |
MetricTriggerTimeAggregation |
timeGrain 期間內的彙總方法。 例如,統計資料 = 「Average」 和 timeGrain = 「PT1M」 表示計量每 1 分鐘匯總一次,方法是採用平均值。 |
MetricTriggerStatistic |
用來彙總所取樣計量的彙總方法。 例如,TimeAggregation = 「Average」 會採用平均值來匯總取樣的計量。 |
MetricTriggerTimeWindow |
自動調整引擎回頭查看以匯總計量的時間量。 此值必須大於計量集合中的延遲,其會因資源而異。 其值必須介於 5 分鐘到 12 小時之間。 例如,10 分鐘表示每次自動調整執行時,都會查詢過去 10 分鐘的計量。 這項功能可讓您的計量穩定並避免回應暫時性尖峰。 |
MetricTriggerThreshold |
定義觸發縮放事件的計量值。 |
MetricTriggerOperator |
指定評估計量值時要使用的邏輯比較作業。 |
MetricTriggerDividePerInstance |
設定為 true 將觸發程式計量除以實例總數。 例如,如果訊息計數是 300,而且有 5 個實例正在執行,則計算的計量值是每個實例 60 則訊息。 此屬性不適用於所有計量。 |
ScaleActionDirection |
指定相應縮小或相應放大。有效值為 Increase 和 Decrease 。 |
ScaleActionType |
依特定數目的實例調整、調整為特定實例計數,或依目前實例計數的百分比進行調整。 有效值包括 ChangeCount 、ExactCount 和 PercentChangeCount 。 |
ScaleActionCooldown |
調整作業之間等候的時間下限。 這是允許計量穩定並避免 flapping。 例如,如果 ScaleActionCooldown 是 10 分鐘且剛發生調整作業,則自動調整不會嘗試再次調整 10 分鐘。 |
建立預設自動調整設定檔並建立規則的關聯
定義縮放規則之後,請建立設定檔。 設定檔會指定預設、上限和較低的實例計數限制,以及可以套用相關聯規則的時間。 New-AzAutoscaleProfileObject
使用 Cmdlet 來建立新的自動調整設定檔。 由於這是預設設定檔,因此沒有任何排程參數。 預設設定檔在沒有任何其他設定檔作用中時作用中
$defaultProfile=New-AzAutoscaleProfileObject `
-Name "default" `
-CapacityDefault 1 `
-CapacityMaximum 10 `
-CapacityMinimum 1 `
-Rule $rule1, $rule2
下表描述 Cmdlet 中使用的 New-AzAutoscaleProfileObject
參數。
參數 | 描述 |
---|---|
CapacityDefault |
如果計量無法用於評估,則為 的實例數目。 只有當目前的執行個體計數低於預設值時,才會使用預設值。 |
CapacityMaximum |
資源的最大實例數目。 實例數目上限會進一步受限於訂用帳戶中可用的核心數目。 |
CapacityMinimum |
資源的實例數目下限。 |
FixedDateEnd |
設定檔的結束時間,格式為 ISO 8601。 |
FixedDateStart |
ISO 8601 格式設定檔的開始時間。 |
Rule |
規則集合,提供此設定檔作用中時調整動作的觸發程式和參數。 最多可以指定 10 個逗號分隔規則。 |
RecurrenceFrequency |
排程設定檔生效的頻率。 這個值必須為 week 。 |
ScheduleDay |
設定檔在指定週期性排程時生效的天數集合。 可能的值為星期日到星期六。 如需週期性排程的詳細資訊,請參閱 使用 CLI 新增週期性設定檔 |
ScheduleHour |
設定檔生效的時數集合。 支援的值是 0 到 23。 |
ScheduleMinute |
設定檔生效的分鐘集合。 |
ScheduleTimeZone |
設定檔時數的時區。 |
套用自動調整設定
在取得規則和設定檔之後,請使用 套用 New-AzAutoscaleSetting
自動調整設定。 若要更新現有的自動調整設定使用 Update-AzAutoscaleSetting
New-AzAutoscaleSetting `
-Name vmss-autoscalesetting1 `
-ResourceGroupName $resourceGroupName `
-Location eastus `
-Profile $defaultProfile `
-Enabled `
-PropertiesName "vmss-autoscalesetting1" `
-TargetResourceUri "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/$vmssName"
將通知新增至自動調整設定
將通知新增至您的銷售設定,以在發生縮放事件時觸發 Webhook 或傳送電子郵件通知。
如需 Webhook 通知的詳細資訊,請參閱 New-AzAutoscaleWebhookNotificationObject
使用下列 Cmdlet 設定 Webhook;
$webhook1=New-AzAutoscaleWebhookNotificationObject -Property @{} -ServiceUri "http://contoso.com/webhook1"
使用 Webhook 設定通知,並使用 Cmdlet 設定電子郵件通知 New-AzAutoscaleNotificationObject
:
$notification1=New-AzAutoscaleNotificationObject `
-EmailCustomEmail "jason@contoso.com" `
-EmailSendToSubscriptionAdministrator $true `
-EmailSendToSubscriptionCoAdministrator $true `
-Webhook $webhook1
更新自動調整設定以套用通知
Update-AzAutoscaleSetting `
-Name vmss-autoscalesetting1 `
-ResourceGroupName $resourceGroupName `
-Profile $defaultProfile `
-Notification $notification1 `
-TargetResourceUri "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/$vmssName"
檢閱自動調整設定
若要檢閱自動調整設定,請使用 將 Get-AzAutoscaleSetting
設定載入變數,然後輸出變數,如下所示:
$autoscaleSetting=Get-AzAutoscaleSetting -ResourceGroupName $resourceGroupName -Name vmss-autoscalesetting1
$autoscaleSetting | Select-Object -Property *
使用 取得自動調整歷程記錄 AzAutoscaleHistory
Get-AzAutoscaleHistory -ResourceId /subscriptions/<subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/$vmssName
排程和週期性設定檔
新增特殊事件的排程設定檔
設定自動調整設定檔,以針對特定事件以不同的方式調整。 例如,針對需求高於平常的一天,請建立增加上限和最小實例限制的設定檔。
下列範例會使用與上述預設設定檔相同的規則,但會針對特定日期設定新的實例限制。 您也可以設定要與新設定檔搭配使用的不同規則。
$highDemandDay=New-AzAutoscaleProfileObject `
-Name "High-demand-day" `
-CapacityDefault 7 `
-CapacityMaximum 30 `
-CapacityMinimum 5 `
-FixedDateEnd ([System.DateTime]::Parse("2023-12-31T14:00:00Z")) `
-FixedDateStart ([System.DateTime]::Parse("2023-12-31T13:00:00Z")) `
-FixedDateTimeZone "UTC" `
-Rule $rule1, $rule2
Update-AzAutoscaleSetting `
-Name vmss-autoscalesetting1 `
-ResourceGroupName $resourceGroupName `
-Profile $defaultProfile, $highDemandDay `
-Notification $notification1 `
-TargetResourceUri "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/$vmssName"
新增週期性排程設定檔
週期性設定檔可讓您排程每週重複的調整設定檔。 例如,在週末從星期五晚上調整為單一實例,直到星期一上午。
雖然排程的設定檔有開始和結束日期,但週期性設定檔沒有結束時間。 設定檔會保持作用中,直到下一個設定檔的開始時間為止。 因此,當您建立週期性設定檔時,必須建立週期性預設設定檔,以在您希望先前的週期性設定檔完成時開始。
例如,若要設定從星期五開始的週末設定檔,並在星期一上午結束,請建立從星期五晚上開始的設定檔,然後使用您在星期一上午開始的預設設定建立週期性設定檔。
下列腳本會建立週末設定檔,以及結束週末設定檔的新增預設設定檔。
$fridayProfile=New-AzAutoscaleProfileObject `
-Name "Weekend" `
-CapacityDefault 1 `
-CapacityMaximum 1 `
-CapacityMinimum 1 `
-RecurrenceFrequency week `
-ScheduleDay "Friday" `
-ScheduleHour 22 `
-ScheduleMinute 00 `
-ScheduleTimeZone "Pacific Standard Time" `
-Rule $rule1, $rule2
$defaultRecurringProfile=New-AzAutoscaleProfileObject `
-Name "default recurring profile" `
-CapacityDefault 2 `
-CapacityMaximum 10 `
-CapacityMinimum 2 `
-RecurrenceFrequency week `
-ScheduleDay "Monday" `
-ScheduleHour 00 `
-ScheduleMinute 00 `
-ScheduleTimeZone "Pacific Standard Time" `
-Rule $rule1, $rule2
New-AzAutoscaleSetting `
-Location eastus `
-Name vmss-autoscalesetting1 `
-ResourceGroupName $resourceGroupName `
-Profile $defaultRecurringProfile, $fridayProfile `
-Notification $notification1 `
-TargetResourceUri "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/$vmssName"
如需排程設定檔的詳細資訊,請參閱 使用多個設定檔自動調整
其他自動調整命令
如需自動調整的 PowerShell Cmdlet 完整清單,請參閱 PowerShell 模組瀏覽器
清除資源
若要清除您在本教學課程中建立的資源,請刪除您所建立的資源群組。 下列 Cmdlet 會刪除資源群組及其所有資源。
Remove-AzResourceGroup -Name $resourceGroupName