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

使用Bicep配置Azure Web App自动缩放时设置最小实例数

Azure Web App 配置自动缩放并保证至少2个实例的Bicep实现

要实现这个需求,核心是在Bicep中正确配置Web App的弹性缩放参数,同时确保App Service Plan使用支持「Always Ready Instances」的SKU。以下是具体实现方案:

关键前提

必须使用弹性Premium SKU的App Service Plan(如P1v3、P2v3等非隔离型弹性规格),只有这类计划支持固定最小实例数的弹性缩放功能。

完整Bicep代码示例

// 创建支持弹性缩放的App Service Plan
resource appServicePlan 'Microsoft.Web/serverfarms@2023-01-01' = {
  name: 'elastic-app-plan'
  location: resourceGroup().location
  sku: {
    name: 'P1v3' // 按需选择合适的弹性Premium规格
    tier: 'PremiumV3'
    size: 'P1v3'
    family: 'Pv3'
    capacity: 1 // 初始实例数不影响,minimumElasticInstanceCount会强制覆盖最小限制
  }
  properties: {
    elasticScaleEnabled: true // 开启计划的弹性缩放支持
  }
}

// 创建Web App并配置最小实例数
resource webApp 'Microsoft.Web/sites@2023-01-01' = {
  name: 'my-scalable-webapp'
  location: resourceGroup().location
  properties: {
    serverFarmId: appServicePlan.id
    siteConfig: {
      elasticScaleEnabled: true // 必须启用此选项
      minimumElasticInstanceCount: 2 // 强制维持至少2个运行实例
      // 可添加其他Web App配置(如运行时、日志设置等)
    }
  }
}

配置说明

  1. App Service Plan:

    • 选择弹性Premium SKU后,需显式设置elasticScaleEnabled: true,开启计划的弹性缩放支持。
    • 初始capacity设为1不影响最终实例数,minimumElasticInstanceCount会强制Web App维持最少2个实例。
  2. Web App核心配置:

    • 必须同时设置elasticScaleEnabled: true和minimumElasticInstanceCount: 2,这两个参数配合才能生效。部署完成后,Web App会自动保持2个实例运行,并根据负载自动弹性扩容。
  3. 自定义缩放规则(可选):
    如果需要基于CPU、内存等指标自定义缩放逻辑,可以添加自动缩放规则资源:

    resource autoscaleSetting 'Microsoft.Insights/autoscaleSettings@2022-10-01' = {
      name: 'webapp-autoscale-rule'
      location: resourceGroup().location
      properties: {
        profiles: [
          {
            name: 'Default'
            capacity: {
              minimum: '2' // 需与Web App的minimumElasticInstanceCount保持一致
              maximum: '10'
              default: '2'
            }
            rules: [
              // CPU超过70%时,5分钟内扩容1个实例
              {
                metricTrigger: {
                  metricName: 'CpuPercentage'
                  metricNamespace: 'Microsoft.Web/sites'
                  metricResourceUri: webApp.id
                  timeGrain: 'PT1M'
                  statistic: 'Average'
                  timeWindow: 'PT5M'
                  timeAggregation: 'Average'
                  operator: 'GreaterThan'
                  threshold: 70
                }
                scaleAction: {
                  direction: 'Increase'
                  type: 'ChangeCount'
                  value: '1'
                  cooldown: 'PT5M'
                }
              }
              // CPU低于30%时,10分钟内缩容1个实例
              {
                metricTrigger: {
                  metricName: 'CpuPercentage'
                  metricNamespace: 'Microsoft.Web/sites'
                  metricResourceUri: webApp.id
                  timeGrain: 'PT1M'
                  statistic: 'Average'
                  timeWindow: 'PT10M'
                  timeAggregation: 'Average'
                  operator: 'LessThan'
                  threshold: 30
                }
                scaleAction: {
                  direction: 'Decrease'
                  type: 'ChangeCount'
                  value: '1'
                  cooldown: 'PT5M'
                }
              }
            ]
          }
        ]
        enabled: true
        targetResourceUri: appServicePlan.id
      }
    }
    

部署上述代码后,Web App会自动维持至少2个运行实例,同时根据配置规则或默认弹性策略实现自动缩放。

内容的提问来源于stack exchange,提问作者Iain Brown

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 13:36:13