如何按计划扩大或缩小 Azure App Service 实例大小?

How to scale Azure App Service instance size up or down on a schedule?

关于 Azure App Service 的自动缩放,我只找到以下内容。

Scale a web app in Azure App Service

这只允许缩放到更多或更少的实例。它不允许扩展到更大和更小的实例。

我想按计划在小型、中型和大型之间安排应用服务实例大小。是否有 API 允许我这样做?

非常感谢。

我认为您正在寻找的是按计划自动缩放 Azure WebApp 或 CPU 类指标

1.Change 您的托管计划为标准,您不能在标准层以下设置自动缩放。

2.Use Azure 预览门户位于 portal.azure.com。

3.Azure 预览门户:正确的设置是使用“CPU 百分比”

4.Azure 门户,您可以将按指标缩放从 None 设置为 CPU

5.Set 实例数介于 1 和 4 或 8 之间,您可以稍后更改它,最多可以达到 10。

有关详细信息,您可以参考这篇文章 http://blogs.msdn.com/b/devschool/archive/2015/05/24/azure-webjobs.aspx本文没有讨论的是按计划缩放,我想您一旦到达那里就可以弄清楚。 确保使用 portal.azure.com

遗憾的是,目前无法按计划扩展 Azure 应用服务实例大小(即 应用服务计划定价等级)。

截至目前,Azure App Service 仅支持按计划进行水平缩放(即实例计数缩放),但不支持垂直缩放(即实例大小缩放)。

希望对您有所帮助!

没有简单的方法可以做到这一点。

但是,如果您愿意编写一些代码,您可以使用 PowerShell api 和 Azure 自动化来为自己创建此功能。

您将使用 api 每隔 X 分钟检查一次指标(如 CPU),如果 CPU 高于 Y,则扩大到下一个更大的实例。如果它低于您的阈值,则缩小。

使用powershell,你可以像这样切换web应用的应用服务计划

PS C:\> $Resource = Get-AzureRmResource -ResourceType "microsoft.web/sites" -ResourceGroupName "ResourceGroup11" -ResourceName "ContosoSite" 
PS C:\> $Resource.Properties.ServerFarmId = "/subscriptions/{subscr_id}/resourceGroups/FriendsRGrp/provider
s/Microsoft.Web/serverfarms/FriendsPlan"
PS C:\> $Resource | Set-AzureRmResource -Force

此处的服务器场 ID 只是服务计划的资源 ID,您可以通过查看计划的属性从新门户获取该资源 ID。

您可以有两个服务计划,一个是基本服务,另一个是标准服务。然后,您可以使用 Azure 自动化在工作日升级到标准并在周末降级到基本。

我了解到您的要求是更改现有计划本身,而不是在计划之间切换。我认为这应该是可能的,尽管我自己还没有尝试过。但是如果你像上面那样浏览返回的 Azure web 应用程序资源的 Resource.Properties 中返回的属性,你应该能够弄明白。

实际上您可以自动扩展(纵向,即更改服务计划)和缩减(实例计数)。

横向扩展选项一直存在并允许您设置规则(例如 CPU 超过 %、内存超过阈值等)

纵向扩展选项需要使用 Azure 自动化。文档齐全 here

希望对您有所帮助!

由于缺少简单的解决方案,我创建了一个 one-click 部署来完成您的要求。

https://github.com/jraps20/jrap-AzureVerticalScaling

概览

我的方法使用 Azure Automation Runbook。通过 one-click 部署方法,您可以在几分钟内完全启动并 运行。两个互补的运行手册(ScaleAppServicePlansUp 和 ScaleAppServicePlansDown)协同工作以存储、读取和修改您选择的任何应用服务计划。这些运行手册的主要目标是 non-prod 环境。

不幸的是,代码太长无法包含在这个答案中(所以是的,这主要是一个 link-only 答案)。

伪代码

缩小规模

Iterate across all Resource Groups (or pass in specific one)
Iterate across all App Service Plans (or pass in specific one)
Iterate across all App Services (identify Tier-specific settings)

During iteration, the current App Service Plan Service Tier is stored in Azure Automation Variables (3 distinct variables for each App Service Plan)

Within each App Service Plan, each App Service is iterated to identify tier-specific settings. Some of these settings include: AlwaysOn, Use32BitWorkerProcess, and ClientCertEnabled. All current settings are stored in Azure Automation Variables.

All App Service Plans are then scaled down to the FREE tier.

扩大规模

Iterate across all Resource Groups (or pass in specific one)
Iterate across all App Service Plans (or pass in specific one)
Iterate across all App Services (identify Tier-specific settings)

During iteration, the original App Service Plan Service Tier is retrieved from Azure Automation Variables (3 distinct variables for each App Service Plan)

Within each App Service Plan, each App Service is iterated and any previously stored tier-specific settings are retrieved.

All App Service Plans are then scaled up to their original tier.
All App Services with tier-specific settings are reapplied to their original values.

其他资源

免责声明

Sam Spoerles technique完成我的工作后才意识到。我的方法比他的方法的好处如下: