自動調整可協助您確保執行中的資源數量正確,能夠處理變動的應用程式負載。 你可以透過使用 Azure 入口網站、Azure CLI、Azure PowerShell,或 ARM 或 Bicep 範本來設定自動擴展。
本文說明如何使用 PowerShell 為虛擬機器擴展集設定自動調整。 使用下列步驟來設定:
- 建立可自動調整的擴展集。
- 建立用於縮減和擴增的規則。
- 建立使用您規則的設定檔。
- 套用自動縮放設定。
- 使用通知更新您的自動調整設定。
必要條件
若要使用 PowerShell 設定自動調整,您需要具有有效訂用帳戶的 Azure 帳戶。 您可以免費建立帳戶。
設定您的環境
在 Azure Cloud Shell 中使用 PowerShell。
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,而非將 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" 表示每隔 1 分鐘計算平均值來彙總計量。 |
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 |
指定週期性排程時,設定檔生效的天數集合。 可能的值為 Sunday 到 Saturday。 如需週期性排程的詳細資訊,請參閱使用 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
用 HTTPS 端點來做 webhook。
# 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
以下範例使用與先前預設配置檔相同的規則,但為特定日期設定新的實例限制。 你也可以設定不同的規則來搭配新設定檔使用。 起始與結束值使用 ISO 8601 UTC 時間戳記。
# 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
例如,在週末 (星期五晚上到星期一早上) 調整為單一執行個體。 若要設定從週五晚上開始、週一早上結束的週末設定檔,請建立一個從週五晚上開始的設定檔,然後使用您的預設設定建立從週一早上開始的週期性設定檔。
它會建立週末設定檔和週期性預設設定檔來結束週末設定檔,同時保留先前步驟中的預設和固定日期設定檔。
# 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 指令集清單,請參閱 PowerShell 模組瀏覽器。
清除資源
要清理你在這個教學中建立的資源,請刪除你建立的專用資源群組。 以下 Cmdlet 會刪除資源群組及其所有資源。
以下 Azure PowerShell 範例使用 Remove-AzResourceGroup cmdlet。
# Delete the example resource group
Remove-AzResourceGroup -Name $resourceGroupName