Azure Terraform(十三)提升 Azure Web App Plan 的性能

一,引言  

  我们是否正在为部署在云主机上的应用程序性能缓慢和停机问题而苦恼?我们是否正在因为云主机上仅仅部署了应用程序,在流量平缓的时候而浪费大量的计算资源而心疼荷包。那么让我们来一起看看 Azure Web App Plan 吧!今天我们以 IAC 的方式来控制资源的创建,通过使用 Terraform 配置我们的 Azure Web App Plan,可以让我们可以轻松优化应用程序的性能和可扩展性以满足不断增长的用户群的需求。借助 Azure Web App Plan,我们可以轻松创建和管理在完全托管环境中运行的可缩放 Web 应用。我们可以使用 Terraform 对其进行配置,从而比以往更轻松地自动化部署我们的基础设施。

--------------------Azure Terraform 系列--------------------

1,Azure Terraform(一)入门简介

2,Azure Terraform(二)语法详解

3,Azure Terraform(三)部署 Web 应用程序

4,Azure Terraform(四)状态文件存储

5,Azure Terraform(五)利用Azure DevOps 实现自动化部署基础资源

6,Azure Terraform(六)Common Module

7,Azure Terraform(七)利用Azure DevOps 实现自动化部署基础资源(补充)

8,Azure Terraform(八)利用Azure DevOps 实现Infra资源和.NET CORE Web 应用程序的持续集成、持续部署

9,Azure Terraform(九)利用 Azure DevOps Pipeline 的审批来控制流程发布

10,Azure Terraform(十)利用 Azure DevOps 的条件语句选择发布环境

11,Azure Terraform(十一)Azure DevOps Pipeline 内的动态临时变量的使用

12,Azure Terraform(十二)利用 Terraform 将文件上传到 Azure Blob Storage

13,Azure Terraform(十三)提升 Azure  Web App Plan  的性能

二,正文

1,创建 Azure 应用服务计划资源

首先,您需要创建一个 Azure 应用服务计划资源。这是执行此操作的 Terraform 代码:

resource "azurerm_app_service_plan" "app_service_plan" {
  name                = "cnbateblogweb-app-service-plan"
  location            = "East Asia"
  resource_group_name = "Web_Test_TF_RG"
  kind                = "Linux"
  reserved = true
  
  sku {
    tier = "Standard"
    size = "S1"
  }
}

此代码在 "East Asia"(东亚)的 位置创建一个叫做 "cnbateblogweb-app-service-plan" Azure App Service Plan 资源 ,SKU 为 "Standard" 大小为“S1”。它还将 "reserved" 属性设置为 true。

2,创建 Azure Web 应用资源

上一步我们已经创建了 Azure 应用服务计划资源,接下来可以创建 Azure Web App。以下是执行此操作的 Terraform 代码:

resource "azurerm_app_service" "app_service" {
  name                = "cnbateblogweb-web-app"
  location            = "${azurerm_app_service_plan.app_service_plan.location}"
  resource_group_name = "${azurerm_app_service_plan.app_service_plan.resource_group_name}"
  app_service_plan_id  = "${azurerm_app_service_plan.app_service_plan.id}"
  
  site_config {
    linux_fx_version = "DOCKER|xxx:tag"
  }
}

此代码创建一个 Azure Web App 资源,该资源依赖与步骤 1 中创建的 Azure App Service Plan 资源。它还设置 “linux_fx_version” 属性以指定要用于 Azure Web App 的 Docker 映像和标记。

3,使用 azurerm_monitor_autoscale_setting 配置自动缩放

现在,让我们使用 azurerm_monitor_autoscale_setting,以下是执行此操作的 Terraform 代码:

resource "azurerm_monitor_autoscale_setting" "app_service_autoscale" {
  name                = "cnbateblogweb-autoscale"
  resource_group_name = "${azurerm_app_service_plan.app_service_plan.resource_group_name}"
  target_resource_id  = "${azurerm_app_service_plan.app_service_paln.id}"
  
  profile {
    default_capacity {
      minimum = 1
      maximum = 10
      default = 1
    }
    
    rule {
      metric_trigger {
        metric_name        = "CpuPercentage"
        metric_resource_id = "${azurerm_app_service_plan.example.id}"
        time_grain         = "PT1M"
        statistic          = "Average"
        time_window        = "PT5M"
        operator           = "GreaterThan"
        threshold          = 70
      }
      
      scale_action {
        direction         = "Increase"
        type              = "ChangeCount"
        value             = 1
        cooldown          = "PT5M"
      }

此 Terraform 代码创建一个 Azure Web App Plan 的自动缩放设置,用于监视 Azure Web App Plan 资源的 CPU 使用率,并在 CPU 使用率超过 70% 时自动扩展容量。同时还将设置最小和最大容量分别设置为 1 和 10,并将默认容量设置为 1。使用此自动缩放设置,可以确保我们的 Web 应用程序拥有处理高流量所需的资源,同时在流量减少时通过缩减来节省资金。

三,结尾

  通过执行这些简单的步骤,我们可以使用 Terraform 轻松配置 Azure App Service Plan 资源,并利用  azurerm_monitor_autoscale_setting。借助 Azure Web App Plan,可以确保 Azure Web App 的最佳性能,同时节省资源成本。对于企业/个人的云上资源的成本是个不错的选择!!!

 参考链接:https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/app_service

作者:Allen 

版权:转载请在文章明显位置注明作者及出处。如发现错误,欢迎批评指正。

posted @ 2023-04-04 10:59  Grant_Allen  阅读(176)  评论(0编辑  收藏  举报