使用Azure Automation自动调整App Services Plan大小

最近在和客户聊天的过程中了解到,客户当前在Azure的环境内有很多App Services的网站资源,其中包含了生产、开发和测试环境。客户对于不同的环境,有着不同的SLA的需求并且对于当前环境的业务高峰和低谷都是可预测的。所以想通过一定的方式来节省一些费用,比如在非工作时间降低App Services Plan的规格等方式

使用Azure Automation自动调整App Services Plan大小_Azure

说到App Services Plan大家可能比较陌生,但是如果提到Azure App Services大家肯定都很熟悉,相信很多朋友都在其上托管了一些Web应用。其实我们所使用的App Services始终是在App Services Plan中运行的。App Services Plan的定价层确定了所提供的App Services功能和计划费用。这也就意味着,如果想要控制App Services所产生的费用,就需要从App Services Plan入手。

默认情况下,Azure中提供了两种对于App Services Plan的扩展方式,分别为:

  • 横向扩展:是添加应用程序的额外实例以适应不断增加的负载的过程
  • 纵向扩展:是在性能更高的硬件上运行应用程序的过程

其中横向扩展是可以通过App Service的CPU或内存使用情况自动化实现的,但是纵向扩展则不能。但是很显然,对于本次的需求,我们需要使用纵向扩展来实现这个需求。所以经过一番研究以后,决定通过Azure Automation的自动化Runbook的定时任务来实现上述需求。具体大家可以参考如下步骤:

创建Automation Account

在Azure Portal,点击创建 > 搜索 Automation:

使用Azure Automation自动调整App Services Plan大小_App Services Plan_02

点击创建:

使用Azure Automation自动调整App Services Plan大小_Azure_03

选择订阅并设置资源组,然后设置Automation Account的名称,选择对应的区域后,点击下一步:

使用Azure Automation自动调整App Services Plan大小_成本优化_04

勾选启用系统分配的托管标识,点击下一步:

使用Azure Automation自动调整App Services Plan大小_成本优化_05

网络位置保持默认,点击下一步:

使用Azure Automation自动调整App Services Plan大小_成本优化_06

确认无误,点击创建:

使用Azure Automation自动调整App Services Plan大小_App Services Plan_07

创建完成后,如下图所示:

使用Azure Automation自动调整App Services Plan大小_成本优化_08

配置Automation Account权限

自动化账户创建好以后,需要对其进行赋权,确保有权限操作App Services Plan的扩展动作。本次演示中,直接给Automation Account订阅的参与者权限,大家如果在实际的生产环境建议精细化控制权限的分配哦

导航到订阅,点击access control,然后点击Add role assignment:

使用Azure Automation自动调整App Services Plan大小_成本优化_09

选择参与者权限:

使用Azure Automation自动调整App Services Plan大小_Web应用_10

选择将权限分配给Automation Account的托管标识:

使用Azure Automation自动调整App Services Plan大小_App Services Plan_11

确认无误,点击分配:

使用Azure Automation自动调整App Services Plan大小_Web应用_12

部署Runbook

进入刚刚创建的Automation Account,点击Runbooks > Create Runbook:

使用Azure Automation自动调整App Services Plan大小_App Services Plan_13

输入Runbook名称,选择类型为Powershell,然后选择版本为7.2:

使用Azure Automation自动调整App Services Plan大小_Azure_14

确认无误,点击创建:

使用Azure Automation自动调整App Services Plan大小_Web应用_15

创建好以后,贴入我们的代码:

使用Azure Automation自动调整App Services Plan大小_App Services Plan_16

点击保存,然后点击发布:

使用Azure Automation自动调整App Services Plan大小_Web应用_17

发布好以后,我们可以点击 Start来测试Runbook的运行情况:

使用Azure Automation自动调整App Services Plan大小_App Services Plan_18

点击Start以后,配置对应的参数,这里主要指明需要操作那个App Services Plan以及需要调整到的Size:

使用Azure Automation自动调整App Services Plan大小_App Services Plan_19

Runbook运行的过程中,我们可以通过输出窗口查看相关Log,可以看到已经将App Services Plan从F1调整到了S1:

使用Azure Automation自动调整App Services Plan大小_Web应用_20

我们回到App Services Plan来检查可以看到,确实已经完成更改:

使用Azure Automation自动调整App Services Plan大小_App Services Plan_21

到这里整个的一个实现思路就给大家实现了,后续如果想要通过自动化的方式进行扩展的话,只需要结合Automation Account中的Schedules来实现就可以啦