Azure中如何从手动缩放切换为基于规则的缩放?能否用Runbook实现?
问题:能否通过Runbook将手动缩放更新为基于规则的缩放?

用户问题:
- 是否可以通过Runbook将手动缩放更新为基于规则的缩放?
- 尝试获取JSON并运行但未成功,已为Automation Account授予存储账户访问权限,仍无法获取JSON,当前遇到JSON格式错误。

解决方案
1. 可行性确认
可以通过Azure Automation Runbook实现手动缩放转基于规则的缩放,核心是调用Azure API或Az PowerShell模块修改资源的自动缩放配置。
2. JSON格式问题修复
- 匹配Azure API要求的结构:基于规则的缩放配置JSON需包含正确的层级,示例结构如下:
{ "properties": { "targetResourceUri": "/subscriptions/{订阅ID}/resourceGroups/{资源组名}/providers/Microsoft.Compute/virtualMachineScaleSets/{VMSS名称}", "enabled": true, "profiles": [ { "name": "默认配置", "capacity": { "minimum": "1", "maximum": "5", "default": "1" }, "rules": [ { "metricTrigger": { "metricName": "Percentage CPU", "metricResourceUri": "/subscriptions/{订阅ID}/resourceGroups/{资源组名}/providers/Microsoft.Compute/virtualMachineScaleSets/{VMSS名称}", "timeGrain": "PT1M", "statistic": "Average", "timeWindow": "PT5M", "timeAggregation": "Average", "operator": "GreaterThan", "threshold": 70 }, "scaleAction": { "direction": "Increase", "type": "ChangeCount", "value": "1", "cooldown": "PT5M" } } ] } ] } } - 校验语法正确性:使用JSON校验工具检查是否存在引号不匹配、逗号缺失等基础语法错误。
- 补全权限配置:除存储账户权限外,需给Automation Account分配目标资源(如VMSS)的
Microsoft.Insights/autoscaleSettings/write权限,可通过IAM添加“监视参与者”角色实现。
3. PowerShell Runbook示例
# 以托管身份连接Azure Connect-AzAccount -Identity # 读取并转换JSON配置 $configJson = Get-Content "./autoscale-config.json" -Raw | ConvertFrom-Json # 更新自动缩放设置 New-AzAutoscaleSetting -ResourceGroupName "你的资源组名" ` -Name "自动缩放设置名称" ` -Location "资源区域" ` -AutoscaleSettingObject $configJson
内容的提问来源于stack exchange,提问作者Antonio Pop
相关产品推荐
相关产品推荐

