可以在自动缩放中使用多个配置文件在不同时间以不同的方式进行缩放。 例如,如果企业在周末未处于活动状态,请创建定期配置文件,以在星期六和星期日缩减资源。 如果黑色星期五是忙碌的一天,请创建一个配置文件,以在这一天自动横向扩展资源。
在Azure门户中,结束时间字段将成为默认配置文件的下一个开始时间。 你不能为一个配置文件的结束和下一个配置文件的开始指定相同的时间。 门户将结束时间强制调整为比下一个简介的开始时间早一分钟。 这一分钟内,默认配置会变为活动状态。 如果不希望默认配置文件在重复配置文件之间被激活,请将结束时间字段留空。
以下Azure CLI示例使用az monitor autoscale update命令。
# User input variables - update values in <AngleBrackets>
resourceGroupName="<ResourceGroupName>"
vmssName="<VirtualMachineScaleSetName>"
autoscaleName="<AutoscaleSettingName>"
# Get the subscription ID from the current Azure CLI context
subscriptionId=$(az account show --query id --output tsv)
# Build virtual machine scale set resource ID
vmssPath="/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName"
vmssProvider="Microsoft.Compute/virtualMachineScaleSets/$vmssName"
vmssResourceId="$vmssPath/providers/$vmssProvider"
# Build the profiles as JSON
profiles=$(jq -n --arg vmssResourceId "$vmssResourceId" '
def scaleRule($operator; $threshold; $direction): {
scaleAction: {
direction: $direction,
type: "ChangeCount",
value: "1",
cooldown: "PT5M"
},
metricTrigger: {
metricName: "Inbound Flows",
metricNamespace: "microsoft.compute/virtualmachinescalesets",
metricResourceUri: $vmssResourceId,
operator: $operator,
statistic: "Average",
threshold: $threshold,
timeAggregation: "Average",
timeGrain: "PT1M",
timeWindow: "PT10M",
dimensions: [],
dividePerInstance: true
}
};
[
{
name: "Weekday profile",
capacity: {minimum: "3", maximum: "20", default: "3"},
rules: [
scaleRule("GreaterThan"; 100; "Increase"),
scaleRule("LessThan"; 60; "Decrease")
],
recurrence: {
frequency: "Week",
schedule: {
timeZone: "E. Europe Standard Time",
days: ["Monday"],
hours: [4],
minutes: [0]
}
}
},
{
name: "Weekend profile",
capacity: {minimum: "1", maximum: "3", default: "1"},
rules: [],
recurrence: {
frequency: "Week",
schedule: {
timeZone: "E. Europe Standard Time",
days: ["Saturday"],
hours: [0],
minutes: [1]
}
}
}
]
')
profileSet="profiles=$profiles"
# Update the autoscale profiles
az monitor autoscale update \
--name "$autoscaleName" \
--resource-group "$resourceGroupName" \
--set "$profileSet"
注意
Azure CLI命令使用当前 CLI 上下文中的Azure 资源管理器终结点,因此无需在命令语法中指定 management.chinacloudapi.cn。
以下 Azure PowerShell 示例使用 New-AzAutoscaleScaleRuleObject、New-AzAutoscaleProfileObject 和 Update-AzAutoscaleSetting cmdlet。
# Set variables
$resourceGroupName = "<ResourceGroupName>"
$vmssName = "<VirtualMachineScaleSetName>"
$autoscaleName = "<AutoscaleSettingName>"
# 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"
$targetResourceId = "$vmssPath/providers/$vmssProvider"
# Define parameters for New-AzAutoscaleScaleRuleObject
$newAzAutoscaleScaleRuleObjectParams = @{
MetricTriggerMetricName = "Inbound Flows"
MetricTriggerMetricNamespace = "microsoft.compute/virtualmachinescalesets"
MetricTriggerMetricResourceUri = $targetResourceId
MetricTriggerTimeGrain = (New-TimeSpan -Minutes 1)
MetricTriggerStatistic = "Average"
MetricTriggerTimeWindow = (New-TimeSpan -Minutes 10)
MetricTriggerTimeAggregation = "Average"
MetricTriggerOperator = "GreaterThan"
MetricTriggerThreshold = 100
MetricTriggerDividePerInstance = $true
ScaleActionDirection = "Increase"
ScaleActionType = "ChangeCount"
ScaleActionValue = 1
ScaleActionCooldown = (New-TimeSpan -Minutes 5)
}
$scaleOutRule = New-AzAutoscaleScaleRuleObject @newAzAutoscaleScaleRuleObjectParams
# Define parameters for New-AzAutoscaleScaleRuleObject
$newAzAutoscaleScaleRuleObjectParams = @{
MetricTriggerMetricName = "Inbound Flows"
MetricTriggerMetricNamespace = "microsoft.compute/virtualmachinescalesets"
MetricTriggerMetricResourceUri = $targetResourceId
MetricTriggerTimeGrain = (New-TimeSpan -Minutes 1)
MetricTriggerStatistic = "Average"
MetricTriggerTimeWindow = (New-TimeSpan -Minutes 10)
MetricTriggerTimeAggregation = "Average"
MetricTriggerOperator = "LessThan"
MetricTriggerThreshold = 60
MetricTriggerDividePerInstance = $true
ScaleActionDirection = "Decrease"
ScaleActionType = "ChangeCount"
ScaleActionValue = 1
ScaleActionCooldown = (New-TimeSpan -Minutes 5)
}
$scaleInRule = New-AzAutoscaleScaleRuleObject @newAzAutoscaleScaleRuleObjectParams
# Define parameters for New-AzAutoscaleProfileObject
$newAzAutoscaleProfileObjectParams = @{
Name = "Weekday profile"
CapacityDefault = "3"
CapacityMaximum = "20"
CapacityMinimum = "3"
RecurrenceFrequency = "week"
ScheduleDay = @("Monday")
ScheduleHour = @(4)
ScheduleMinute = @(0)
ScheduleTimeZone = "E. Europe Standard Time"
Rule = @($scaleOutRule, $scaleInRule)
}
$weekdayProfile = New-AzAutoscaleProfileObject @newAzAutoscaleProfileObjectParams
# Define parameters for New-AzAutoscaleProfileObject
$newAzAutoscaleProfileObjectParams = @{
Name = "Weekend profile"
CapacityDefault = "1"
CapacityMaximum = "3"
CapacityMinimum = "1"
RecurrenceFrequency = "week"
ScheduleDay = @("Saturday")
ScheduleHour = @(0)
ScheduleMinute = @(1)
ScheduleTimeZone = "E. Europe Standard Time"
Rule = @()
}
$weekendProfile = New-AzAutoscaleProfileObject @newAzAutoscaleProfileObjectParams
# Define parameters for Update-AzAutoscaleSetting
$updateAzAutoscaleSettingParams = @{
Name = $autoscaleName
ResourceGroupName = $resourceGroupName
Enabled = $true
TargetResourceUri = $targetResourceId
Profile = @($weekdayProfile, $weekendProfile)
}
Update-AzAutoscaleSetting @updateAzAutoscaleSettingParams
注意
Azure PowerShell cmdlet 使用当前 Az 上下文中的Azure 资源管理器终结点,因此无需在 cmdlet 语法中指定management.chinacloudapi.cn。
以下 REST 示例使用 自动缩放设置 REST API 操作。
PUT https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Insights/autoscalesettings/{autoscaleSettingName}?api-version={apiVersion}
Authorization: Bearer {accessToken}
Content-Type: application/json
{
"location": "<AzureRegion>",
"properties": {
"name": "<AutoscaleSettingName>",
"enabled": true,
"targetResourceUri": "/subscriptions/<SubscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.Compute/virtualMachineScaleSets/<VirtualMachineScaleSetName>",
"profiles": [
{
"name": "Weekday profile",
"capacity": { "minimum": "3", "maximum": "20", "default": "3" },
"rules": [
{
"scaleAction": { "direction": "Increase", "type": "ChangeCount", "value": "1", "cooldown": "PT5M" },
"metricTrigger": {
"metricName": "Inbound Flows",
"metricNamespace": "microsoft.compute/virtualmachinescalesets",
"metricResourceUri": "/subscriptions/<SubscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.Compute/virtualMachineScaleSets/<VirtualMachineScaleSetName>",
"operator": "GreaterThan", "statistic": "Average", "threshold": 100,
"timeAggregation": "Average", "timeGrain": "PT1M", "timeWindow": "PT10M",
"dimensions": [], "dividePerInstance": true
}
},
{
"scaleAction": { "direction": "Decrease", "type": "ChangeCount", "value": "1", "cooldown": "PT5M" },
"metricTrigger": {
"metricName": "Inbound Flows",
"metricNamespace": "microsoft.compute/virtualmachinescalesets",
"metricResourceUri": "/subscriptions/<SubscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.Compute/virtualMachineScaleSets/<VirtualMachineScaleSetName>",
"operator": "LessThan", "statistic": "Average", "threshold": 60,
"timeAggregation": "Average", "timeGrain": "PT1M", "timeWindow": "PT10M",
"dimensions": [], "dividePerInstance": true
}
}
],
"recurrence": {
"frequency": "Week",
"schedule": { "timeZone": "E. Europe Standard Time", "days": ["Monday"], "hours": [4], "minutes": [0] }
}
},
{
"name": "Weekend profile",
"capacity": { "minimum": "1", "maximum": "3", "default": "1" },
"rules": [],
"recurrence": {
"frequency": "Week",
"schedule": { "timeZone": "E. Europe Standard Time", "days": ["Saturday"], "hours": [0], "minutes": [1] }
}
}
],
"notifications": [],
"targetResourceLocation": "<AzureRegion>"
}
}
注意
模板部署使用创建或更新操作,而不是部分更新。
以下Bicep示例使用Microsoft.Insights/autoscaleSettings资源类型。
param autoscaleName string = '<AutoscaleSettingName>'
param azureRegion string = '<AzureRegion>'
param vmssName string = '<VirtualMachineScaleSetName>'
var vmssResourceId = resourceId(
'Microsoft.Compute/virtualMachineScaleSets',
vmssName
)
resource autoscaleSetting 'Microsoft.Insights/autoscaleSettings@<ApiVersion>' = {
name: autoscaleName
location: azureRegion
properties: {
name: autoscaleName
enabled: true
targetResourceUri: vmssResourceId
profiles: [
{
name: 'Weekday profile'
capacity: {
minimum: '3'
maximum: '20'
default: '3'
}
rules: [
{
scaleAction: {
direction: 'Increase'
type: 'ChangeCount'
value: '1'
cooldown: 'PT5M'
}
metricTrigger: {
metricName: 'Inbound Flows'
metricNamespace: 'microsoft.compute/virtualmachinescalesets'
metricResourceUri: vmssResourceId
operator: 'GreaterThan'
statistic: 'Average'
threshold: 100
timeAggregation: 'Average'
timeGrain: 'PT1M'
timeWindow: 'PT10M'
dimensions: []
dividePerInstance: true
}
}
{
scaleAction: {
direction: 'Decrease'
type: 'ChangeCount'
value: '1'
cooldown: 'PT5M'
}
metricTrigger: {
metricName: 'Inbound Flows'
metricNamespace: 'microsoft.compute/virtualmachinescalesets'
metricResourceUri: vmssResourceId
operator: 'LessThan'
statistic: 'Average'
threshold: 60
timeAggregation: 'Average'
timeGrain: 'PT1M'
timeWindow: 'PT10M'
dimensions: []
dividePerInstance: true
}
}
]
recurrence: {
frequency: 'Week'
schedule: {
timeZone: 'E. Europe Standard Time'
days: [
'Monday'
]
hours: [
4
]
minutes: [
0
]
}
}
}
{
name: 'Weekend profile'
capacity: {
minimum: '1'
maximum: '3'
default: '1'
}
rules: []
recurrence: {
frequency: 'Week'
schedule: {
timeZone: 'E. Europe Standard Time'
days: [
'Saturday'
]
hours: [
0
]
minutes: [
1
]
}
}
}
]
notifications: []
targetResourceLocation: azureRegion
}
}
将此示例改编为配置后,将其另存为 .bicep 文件。 有关部署说明,请参阅使用Azure CLI部署Bicep文件,或使用Azure PowerShell部署Bicep文件。
注意
模板部署使用创建或更新操作,而不是部分更新。
以下 ARM 模板示例使用 Microsoft.Insights/autoscaleSettings 资源类型。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"autoscaleName": {
"type": "string",
"defaultValue": "<AutoscaleSettingName>"
},
"azureRegion": {
"type": "string",
"defaultValue": "<AzureRegion>"
},
"vmssName": {
"type": "string",
"defaultValue": "<VirtualMachineScaleSetName>"
}
},
"variables": {
"vmssResourceId": "[resourceId('Microsoft.Compute/virtualMachineScaleSets', parameters('vmssName'))]"
},
"resources": [
{
"type": "Microsoft.Insights/autoscaleSettings",
"apiVersion": "<ApiVersion>",
"name": "[parameters('autoscaleName')]",
"location": "[parameters('azureRegion')]",
"properties": {
"name": "[parameters('autoscaleName')]",
"enabled": true,
"targetResourceUri": "[variables('vmssResourceId')]",
"profiles": [
{
"name": "Weekday profile",
"capacity": {
"minimum": "3",
"maximum": "20",
"default": "3"
},
"rules": [
{
"scaleAction": {
"direction": "Increase",
"type": "ChangeCount",
"value": "1",
"cooldown": "PT5M"
},
"metricTrigger": {
"metricName": "Inbound Flows",
"metricNamespace": "microsoft.compute/virtualmachinescalesets",
"metricResourceUri": "[variables('vmssResourceId')]",
"operator": "GreaterThan",
"statistic": "Average",
"threshold": 100,
"timeAggregation": "Average",
"timeGrain": "PT1M",
"timeWindow": "PT10M",
"dimensions": [],
"dividePerInstance": true
}
},
{
"scaleAction": {
"direction": "Decrease",
"type": "ChangeCount",
"value": "1",
"cooldown": "PT5M"
},
"metricTrigger": {
"metricName": "Inbound Flows",
"metricNamespace": "microsoft.compute/virtualmachinescalesets",
"metricResourceUri": "[variables('vmssResourceId')]",
"operator": "LessThan",
"statistic": "Average",
"threshold": 60,
"timeAggregation": "Average",
"timeGrain": "PT1M",
"timeWindow": "PT10M",
"dimensions": [],
"dividePerInstance": true
}
}
],
"recurrence": {
"frequency": "Week",
"schedule": {
"timeZone": "E. Europe Standard Time",
"days": [
"Monday"
],
"hours": [
4
],
"minutes": [
0
]
}
}
},
{
"name": "Weekend profile",
"capacity": {
"minimum": "1",
"maximum": "3",
"default": "1"
},
"rules": [],
"recurrence": {
"frequency": "Week",
"schedule": {
"timeZone": "E. Europe Standard Time",
"days": [
"Saturday"
],
"hours": [
0
],
"minutes": [
1
]
}
}
}
],
"notifications": [],
"targetResourceLocation": "[parameters('azureRegion')]"
}
}
]
}
将此示例改编为配置后,将其另存为 .json 文件。 有关部署说明,请参阅使用Azure CLI部署 ARM 模板,或使用 Azure PowerShell部署 ARM 模板。