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

Grant_Allen發表於2023-04-04

一,引言  

  我們是否正在為部署在雲主機上的應用程式效能緩慢和停機問題而苦惱?我們是否正在因為雲主機上僅僅部署了應用程式,在流量平緩的時候而浪費大量的計算資源而心疼荷包。那麼讓我們來一起看看 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 

版權:轉載請在文章明顯位置註明作者及出處。如發現錯誤,歡迎批評指正。

相關文章