如何通过ARM模板或PowerShell将AutoScaleSetting添加到现有Azure AppServicePlan

rxztt3cl  于 2022-11-10  发布在  Shell
关注(0)|答案(1)|浏览(106)

各位,
我有一个没有自动缩放设置的Azure AppService计划。我想使用ScaleOut添加自动缩放设置,并根据CPU%和Memory%等指标扩展规则。我知道怎么用手来做。但我想通过PowerShell或ARM模板实现自动化。
我到处搜索,但我不知道如何在现有的应用程序服务计划上做到这一点。
如有任何帮助或指导,我们将不胜感激。

t9eec4r0

t9eec4r01#

通过使用ARM模板,我发现了一些可以做到这一点的好读物,您需要的是‘Microsoft.Insights/autoscalesetings’资源类型。
这段代码基于CPU规则设置应用程序服务、服务器场和伸缩指标:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "type": "string",
            "metadata": {
                "description": "Location of the resources deployed"
            },
            "defaultValue": "centralus"
        },
        "serverFarmName": {
            "type": "string",
            "metadata": {
                "description": "Name of the server farm being deployed"
            },
            "defaultValue": "testsf1337_unique"
        },
        "svcPlanName": {
            "type": "string",
            "defaultValue": "SampleAppServicePlan",
            "metadata": {
                "description": "The name of the App Service plan."
            }
        },
        "sku": {
            "type": "string",
            "defaultValue": "Standard",
            "allowedValues": [
                "Standard",
                "Premium"
            ],
            "metadata": {
                "description": "The pricing tier for the App Service plan."
            }
        },
        "svcPlanSize": {
            "defaultValue": "S1",
            "type": "string",
            "metadata": {
                "description": "The instance size of the app."
            }
        },
        "minimumCapacity": {
            "type": "Int",
            "defaultValue": 2,
            "metadata": {
                "description": "The minimum capacity.  Autoscale engine will ensure the instance count is at least this value."
            }
        },
        "maximumCapacity": {
            "type": "Int",
            "defaultValue": 5,
            "metadata": {
                "description": "The maximum capacity.  Autoscale engine will ensure the instance count is not greater than this value."
            }
        },
        "defaultCapacity": {
            "type": "Int",
            "defaultValue": 5,
            "metadata": {
                "description": "The default capacity.  Autoscale engine will preventively set the instance count to be this value if it can not find any metric data."
            }
        },
        "metricName": {
            "type": "string",
            "defaultValue": "CpuPercentage",
            "metadata": {
                "description": "The metric name."
            }
        },
        "metricThresholdToScaleOut": {
            "type": "Int",
            "defaultValue": 60,
            "metadata": {
                "description": "The metric upper threshold.  If the metric value is above this threshold then autoscale engine will initiate scale out action."
            }
        },
        "metricThresholdToScaleIn": {
            "type": "Int",
            "defaultValue": 20,
            "metadata": {
                "description": "The metric lower threshold.  If the metric value is below this threshold then autoscale engine will initiate scale in action."
            }
        },
        "changePercentScaleOut": {
            "type": "Int",
            "defaultValue": 20,
            "metadata": {
                "description": "The percentage to increase the instance count when autoscale engine is initiating scale out action."
            }
        },
        "changePercentScaleIn": {
            "type": "Int",
            "defaultValue": 10,
            "metadata": {
                "description": "The percentage to decrease the instance count when autoscale engine is initiating scale in action."
            }
        },
        "autoscaleEnabled": {
            "type": "Bool",
            "metadata": {
                "description": "A boolean to indicate whether the autoscale policy is enabled or disabled."
            },
            "defaultValue": true
        }

    },
    "functions": [],
    "variables": {},
    "resources": [
        {
            "type": "Microsoft.Web/serverfarms",
            "apiVersion": "2021-02-01",
            "name": "[parameters('serverFarmName')]",
            "location": "[parameters('location')]",
            "sku": {
                "tier": "Standard",
                "name": "S1"
            },
            "kind": "linux",
            "properties": {
                "reserved": true
            }
        },
        {
            "type": "Microsoft.Web/sites",
            "apiVersion": "2021-02-01",
            "name": "testaps1337",
            "location": "[parameters('location')]",
            "properties": {
                "httpsOnly": true,
                "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('serverFarmName'))]",
                "siteConfig": {
                    "linuxFxVersion": "Node|lts",
                    "minTlsVersion": "1.2",
                    "ftpsState": "FtpsOnly",
                    "vnetRouteAllEnabled": true,
                    "http20Enabled": true,
                    "cors": {
                        "allowedOrigins": [
                            "*"
                        ]
                    }
                }
            },

            "identity": {
                "type": "SystemAssigned"
            },
            "dependsOn": [
                "[resourceId('Microsoft.Web/serverfarms', parameters('serverFarmName'))]"
            ]
        },
        {
            "type": "Microsoft.Insights/autoscalesettings",
            "name": "ass1337",
            "dependsOn": [
                "[resourceId('Microsoft.Web/serverfarms', parameters('serverFarmName'))]"
            ],
            "apiVersion": "2014-04-01",
            "location": "[parameters('location')]",
            "properties": {
                "profiles": [
                    {
                        "name": "DefaultAutoscaleProfile",
                        "capacity": {
                            "minimum": 2,
                            "maximum": 4,
                            "default": 2
                        },
                        "rules": [
                            {
                                "metricTrigger": {
                                    "metricName": "[parameters('metricName')]",
                                    "metricResourceUri": "[resourceId('Microsoft.Web/serverFarms/', parameters('serverFarmName'))]",
                                    "timeGrain": "PT5M",
                                    "statistic": "Average",
                                    "timeWindow": "PT10M",
                                    "timeAggregation": "Average",
                                    "operator": "GreaterThan",
                                    "threshold": "[parameters('metricThresholdToScaleOut')]"
                                },
                                "scaleAction": {
                                    "direction": "Increase",
                                    "type": "PercentChangeCount",
                                    "value": "[parameters('changePercentScaleOut')]",
                                    "cooldown": "PT10M"
                                }
                            },
                            {
                                "metricTrigger": {
                                    "metricName": "[parameters('metricName')]",
                                    "metricResourceUri": "[resourceId('Microsoft.Web/serverFarms/', parameters('serverFarmName'))]",
                                    "timeGrain": "PT5M",
                                    "statistic": "Average",
                                    "timeWindow": "PT10M",
                                    "timeAggregation": "Average",
                                    "operator": "LessThan",
                                    "threshold": "[parameters('metricThresholdToScaleIn')]"
                                },
                                "scaleAction": {
                                    "direction": "Decrease",
                                    "type": "PercentChangeCount",
                                    "value": "[parameters('changePercentScaleIn')]",
                                    "cooldown": "PT10M"
                                }
                            }
                        ]
                    }
                ],
                "enabled": "[parameters('autoscaleEnabled')]",
                "targetResourceUri": "[resourceId('Microsoft.Web/serverFarms/', parameters('serverFarmName'))]"
            }
        }

    ],
    "outputs": {}
}

门户网站上的结果是:

This github repository offers a good example
And this MS learn document provides a good overview on the available options
也可以将自动缩放设置添加到现有的应用程序服务/服务器场。要做到这一点,只需获取服务器场的资源ID:

Connect-AzAccount
$RESOURCE = Get-AzResource -ResourceGroupName "resourcegroupname" -Name "serverfarmname"
$SERVERFARMRESOURCEID = $RESOURCE.ResourceId

确保您获得的是服务器场资源,而不是“站点”资源。然后将其作为参数传递:

New-AzResourceGroupDeployment -ResourceGroupName demo-portal-azure -Name "deployment-demo-portal-azure-metricscaling" -TemplateFile "TemplateExistingAppService.json"  -serverFarmResourceId  $SERVERFARMRESOURCEID

和本例中使用的ARM模板:

"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "type": "string",
            "metadata": {
                "description": "Location of the resources deployed"
            },
            "defaultValue": "centralus"
        },

        "metricName": {
            "type": "string",
            "defaultValue": "CpuPercentage",
            "metadata": {
                "description": "The metric name."
            }
        },
        "metricThresholdToScaleOut": {
            "type": "Int",
            "defaultValue": 60,
            "metadata": {
                "description": "The metric upper threshold.  If the metric value is above this threshold then autoscale engine will initiate scale out action."
            }
        },
        "metricThresholdToScaleIn": {
            "type": "Int",
            "defaultValue": 20,
            "metadata": {
                "description": "The metric lower threshold.  If the metric value is below this threshold then autoscale engine will initiate scale in action."
            }
        },
        "changePercentScaleOut": {
            "type": "Int",
            "defaultValue": 20,
            "metadata": {
                "description": "The percentage to increase the instance count when autoscale engine is initiating scale out action."
            }
        },
        "changePercentScaleIn": {
            "type": "Int",
            "defaultValue": 10,
            "metadata": {
                "description": "The percentage to decrease the instance count when autoscale engine is initiating scale in action."
            }
        },
        "autoscaleEnabled": {
            "type": "Bool",
            "metadata": {
                "description": "A boolean to indicate whether the autoscale policy is enabled or disabled."
            },
            "defaultValue": true
        },
        "serverFarmResourceId": {
            "type": "string",
            "metadata": {
                "description": "Resource id of the App service"
            }
        }

    },
    "functions": [],
    "variables": {},
    "resources": [
        {
            "type": "Microsoft.Insights/autoscalesettings",
            "name": "ass1337",
            "apiVersion": "2014-04-01",
            "location": "[parameters('location')]",
            "properties": {
                "profiles": [
                    {
                        "name": "DefaultAutoscaleProfile",
                        "capacity": {
                            "minimum": 2,
                            "maximum": 4,
                            "default": 2
                        },
                        "rules": [
                            {
                                "metricTrigger": {
                                    "metricName": "[parameters('metricName')]",
                                    "metricResourceUri": "[parameters('serverFarmResourceId')]",
                                    "timeGrain": "PT5M",
                                    "statistic": "Average",
                                    "timeWindow": "PT10M",
                                    "timeAggregation": "Average",
                                    "operator": "GreaterThan",
                                    "threshold": "[parameters('metricThresholdToScaleOut')]"
                                },
                                "scaleAction": {
                                    "direction": "Increase",
                                    "type": "PercentChangeCount",
                                    "value": "[parameters('changePercentScaleOut')]",
                                    "cooldown": "PT10M"
                                }
                            },
                            {
                                "metricTrigger": {
                                    "metricName": "[parameters('metricName')]",
                                    "metricResourceUri": "[parameters('serverFarmResourceId')]",
                                    "timeGrain": "PT5M",
                                    "statistic": "Average",
                                    "timeWindow": "PT10M",
                                    "timeAggregation": "Average",
                                    "operator": "LessThan",
                                    "threshold": "[parameters('metricThresholdToScaleIn')]"
                                },
                                "scaleAction": {
                                    "direction": "Decrease",
                                    "type": "PercentChangeCount",
                                    "value": "[parameters('changePercentScaleIn')]",
                                    "cooldown": "PT10M"
                                }
                            }
                        ]
                    }
                ],
                "enabled": "[parameters('autoscaleEnabled')]",
                "targetResourceUri": "[parameters('serverFarmResourceId')]"
            }
        }

    ],
    "outputs": {}
}

希望这回答了您关于如何使用ARM模板添加自动缩放设置的问题。
亲切的问候

相关问题