You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过ARM模板或PowerShell为现有Azure App Service Plan添加自动缩放设置

为现有Azure App Service Plan配置自动缩放规则(PowerShell/ARM模板)

PowerShell 实现

先登录Azure并指定目标订阅:

Connect-AzAccount
Set-AzContext -SubscriptionId "你的订阅ID"

替换脚本中的参数后执行,即可为现有App Service Plan添加基于CPU、内存的扩缩容规则:

# 替换为你的实际参数
$resourceGroupName = "你的资源组名称"
$appServicePlanName = "你的App Service Plan名称"
$autoscaleSettingName = "ASPAutoScaleSetting"

# 获取目标App Service Plan的资源ID
$aspResourceId = (Get-AzAppServicePlan -ResourceGroupName $resourceGroupName -Name $appServicePlanName).Id

# 定义自动缩放配置
$autoscaleProfile = @{
    Name = "DefaultProfile"
    Capacity = @{
        Minimum = 1   # 最小实例数
        Maximum = 5   # 最大实例数
        Default = 2   # 默认实例数
    }
    Rules = @(
        # CPU 扩容:平均CPU>70%持续5分钟,增加1个实例,冷却5分钟
        @{
            MetricTrigger = @{
                MetricName = "CpuPercentage"
                MetricNamespace = "Microsoft.Web/serverfarms"
                ResourceUri = $aspResourceId
                TimeGrain = "PT1M"
                Statistic = "Average"
                TimeWindow = "PT5M"
                TimeAggregation = "Average"
                Operator = "GreaterThan"
                Threshold = 70
                Dimensions = @()
            }
            ScaleAction = @{
                Direction = "Increase"
                Type = "ChangeCount"
                Value = 1
                Cooldown = "PT5M"
            }
        },
        # CPU 缩容:平均CPU<30%持续10分钟,减少1个实例,冷却10分钟
        @{
            MetricTrigger = @{
                MetricName = "CpuPercentage"
                MetricNamespace = "Microsoft.Web/serverfarms"
                ResourceUri = $aspResourceId
                TimeGrain = "PT1M"
                Statistic = "Average"
                TimeWindow = "PT10M"
                TimeAggregation = "Average"
                Operator = "LessThan"
                Threshold = 30
                Dimensions = @()
            }
            ScaleAction = @{
                Direction = "Decrease"
                Type = "ChangeCount"
                Value = 1
                Cooldown = "PT10M"
            }
        },
        # 内存扩容:平均内存>75%持续5分钟,增加1个实例,冷却5分钟
        @{
            MetricTrigger = @{
                MetricName = "MemoryPercentage"
                MetricNamespace = "Microsoft.Web/serverfarms"
                ResourceUri = $aspResourceId
                TimeGrain = "PT1M"
                Statistic = "Average"
                TimeWindow = "PT5M"
                TimeAggregation = "Average"
                Operator = "GreaterThan"
                Threshold = 75
                Dimensions = @()
            }
            ScaleAction = @{
                Direction = "Increase"
                Type = "ChangeCount"
                Value = 1
                Cooldown = "PT5M"
            }
        },
        # 内存缩容:平均内存<25%持续10分钟,减少1个实例,冷却10分钟
        @{
            MetricTrigger = @{
                MetricName = "MemoryPercentage"
                MetricNamespace = "Microsoft.Web/serverfarms"
                ResourceUri = $aspResourceId
                TimeGrain = "PT1M"
                Statistic = "Average"
                TimeWindow = "PT10M"
                TimeAggregation = "Average"
                Operator = "LessThan"
                Threshold = 25
                Dimensions = @()
            }
            ScaleAction = @{
                Direction = "Decrease"
                Type = "ChangeCount"
                Value = 1
                Cooldown = "PT10M"
            }
        }
    )
}

# 创建自动缩放配置
New-AzAutoscaleSetting -ResourceGroupName $resourceGroupName -Name $autoscaleSettingName -TargetResourceUri $aspResourceId -AutoscaleProfile $autoscaleProfile

关键参数说明

  • Minimum/Maximum/Default:控制实例数范围,避免过度扩容或缩容至0
  • TimeWindow:评估指标的时间窗口,过滤瞬时波动导致的误操作
  • Cooldown:扩缩容后的冷却时间,防止短时间内频繁调整实例数

ARM 模板实现

创建名为autoscale-asp.json的模板文件,替换参数后部署:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "resourceGroupName": {
            "type": "string",
            "defaultValue": "你的资源组名称"
        },
        "appServicePlanName": {
            "type": "string",
            "defaultValue": "你的App Service Plan名称"
        },
        "autoscaleSettingName": {
            "type": "string",
            "defaultValue": "ASPAutoScaleSetting"
        },
        "minInstanceCount": {
            "type": "int",
            "defaultValue": 1
        },
        "maxInstanceCount": {
            "type": "int",
            "defaultValue": 5
        },
        "defaultInstanceCount": {
            "type": "int",
            "defaultValue": 2
        }
    },
    "variables": {
        "aspResourceId": "[resourceId(parameters('resourceGroupName'), 'Microsoft.Web/serverfarms', parameters('appServicePlanName'))]"
    },
    "resources": [
        {
            "type": "Microsoft.Insights/autoscaleSettings",
            "apiVersion": "2022-10-01",
            "name": "[parameters('autoscaleSettingName')]",
            "location": "[resourceGroup().location]",
            "properties": {
                "targetResourceUri": "[variables('aspResourceId')]",
                "enabled": true,
                "profiles": [
                    {
                        "name": "DefaultProfile",
                        "capacity": {
                            "minimum": "[parameters('minInstanceCount')]",
                            "maximum": "[parameters('maxInstanceCount')]",
                            "default": "[parameters('defaultInstanceCount')]"
                        },
                        "rules": [
                            // CPU扩容规则
                            {
                                "metricTrigger": {
                                    "metricName": "CpuPercentage",
                                    "metricNamespace": "Microsoft.Web/serverfarms",
                                    "resourceUri": "[variables('aspResourceId')]",
                                    "timeGrain": "PT1M",
                                    "statistic": "Average",
                                    "timeWindow": "PT5M",
                                    "timeAggregation": "Average",
                                    "operator": "GreaterThan",
                                    "threshold": 70,
                                    "dimensions": []
                                },
                                "scaleAction": {
                                    "direction": "Increase",
                                    "type": "ChangeCount",
                                    "value": "1",
                                    "cooldown": "PT5M"
                                }
                            },
                            // CPU缩容规则
                            {
                                "metricTrigger": {
                                    "metricName": "CpuPercentage",
                                    "metricNamespace": "Microsoft.Web/serverfarms",
                                    "resourceUri": "[variables('aspResourceId')]",
                                    "timeGrain": "PT1M",
                                    "statistic": "Average",
                                    "timeWindow": "PT10M",
                                    "timeAggregation": "Average",
                                    "operator": "LessThan",
                                    "threshold": 30,
                                    "dimensions": []
                                },
                                "scaleAction": {
                                    "direction": "Decrease",
                                    "type": "ChangeCount",
                                    "value": "1",
                                    "cooldown": "PT10M"
                                }
                            },
                            // 内存扩容规则
                            {
                                "metricTrigger": {
                                    "metricName": "MemoryPercentage",
                                    "metricNamespace": "Microsoft.Web/serverfarms",
                                    "resourceUri": "[variables('aspResourceId')]",
                                    "timeGrain": "PT1M",
                                    "statistic": "Average",
                                    "timeWindow": "PT5M",
                                    "timeAggregation": "Average",
                                    "operator": "GreaterThan",
                                    "threshold": 75,
                                    "dimensions": []
                                },
                                "scaleAction": {
                                    "direction": "Increase",
                                    "type": "ChangeCount",
                                    "value": "1",
                                    "cooldown": "PT5M"
                                }
                            },
                            // 内存缩容规则
                            {
                                "metricTrigger": {
                                    "metricName": "MemoryPercentage",
                                    "metricNamespace": "Microsoft.Web/serverfarms",
                                    "resourceUri": "[variables('aspResourceId')]",
                                    "timeGrain": "PT1M",
                                    "statistic": "Average",
                                    "timeWindow": "PT10M",
                                    "timeAggregation": "Average",
                                    "operator": "LessThan",
                                    "threshold": 25,
                                    "dimensions": []
                                },
                                "scaleAction": {
                                    "direction": "Decrease",
                                    "type": "ChangeCount",
                                    "value": "1",
                                    "cooldown": "PT10M"
                                }
                            }
                        ]
                    }
                ]
            }
        }
    ]
}

部署模板

执行以下PowerShell命令完成部署:

New-AzResourceGroupDeployment -ResourceGroupName "你的资源组名称" -TemplateFile ./autoscale-asp.json

内容的提问来源于stack exchange,提问作者Ramdas Metla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 17:25:25