全部產品
Search
文件中心

Application Real-Time Monitoring Service:使用Terraform管理Prometheus執行個體

更新時間:Mar 13, 2025

Terraform是HashiCorp公司提供的一種開源工具,用於安全高效地預覽,配置和管理雲基礎架構和資源,協助開發人員自動化地建立、更新阿里雲基礎設施資源,並進行版本管理。本文介紹如何使用Terraform管理Prometheus執行個體。

前提條件

  • 安裝Terraform。

    • Cloud Shell預設安裝配置了Terraform和阿里雲帳號資訊,您無需執行任何額外配置。

    • 如果您不使用Cloud Shell,關於安裝Terraform的具體操作,請參見在本地安裝和配置Terraform

    說明
    • 請確認Terraform版本不低於v0.12.28,可通過terraform --version命令查看Terraform版本。

    • Resource Orchestration Service服務為Terraform提供了託管的能力,您可以建立Terraform類型的模板,定義阿里雲、AWS或Azure資源,配置資源參數和資源間的依賴關係。更多資訊,請參見建立Terraform類型模板建立Terraform類型資源棧

  • 配置阿里雲帳號資訊。有以下兩種方式:

    說明

    為提高許可權管理的靈活性和安全性,建議您建立名為Terraform的RAM使用者,並為該RAM使用者建立AccessKey和授權。具體操作,請參見建立RAM使用者為RAM使用者授權

    • 方式一:建立環境變數,用於存放身份認證資訊。

      export ALICLOUD_ACCESS_KEY="************"
      export ALICLOUD_SECRET_KEY="************"
      export ALICLOUD_REGION="cn-beijing"
      說明

      其中export ALICLOUD_REGION參數的值需要您根據實際情況進行替換。

    • 方式二:通過在設定檔的Provider代碼塊中指定身份認證資訊。

      provider "alicloud" {
        access_key = "************"
        secret_key = "************"
        region     = "cn-beijing"
      }
      說明

      其中region參數的值需要您根據實際情況進行替換。

建立Prometheus執行個體

建立Container Service(ACK)類型的Prometheus執行個體

  1. 建立一個工作目錄,並且在工作目錄中建立名為main.tf的設定檔,檔案內容如下:

    #provider, use alicloud
    provider "alicloud" {
      #access_key = "************"
      #secret_key = "************"
      #region     = "cn-beijing"
    }
    
    # PrometheusContainer Service執行個體。
    resource "alicloud_arms_prometheus" "my_ack1" {
      cluster_id = "阿里雲Container ServiceACK叢集ID"
      cluster_type = "aliyun-cs"
      grafana_instance_id = "free 或 Grafana工作區ID"
    }
  2. 執行以下命令,初始化Terraform運行環境。

    terraform init

    預期輸出:

    Initializing the backend...
    
    Initializing provider plugins...
    - Checking for available provider plugins...
    - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1...
    ...
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. 執行以下命令,產生資源規劃。

    terraform plan

    預期輸出:

    Refreshing Terraform state in-memory prior to plan...
    The refreshed state will be used to calculate this plan, but will not be
    persisted to local or remote state storage.
    ...
    Plan: 5 to add, 0 to change, 0 to destroy.
    ...
  4. 執行以下命令,建立執行個體。

    terraform apply

    預期輸出:

    ...
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    ...
    alicloud_arms_prometheus: Creation complete after 8m26s [id=************]
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

    若輸出結果出現yes,表示當前Prometheus執行個體建立成功。同時您可以登入可觀測監控 Prometheus 版控制台,在執行個體列表頁面查看已成功建立的Prometheus for Container Service執行個體。具體操作如下:

    1. 登入ARMS控制台

    2. 在左側導覽列選擇Prometheus監控 > 執行個體列表,進入可觀測監控 Prometheus 版的執行個體列表頁面。

    3. 在執行個體列表頁面查看已成功建立的Prometheus for Container Service執行個體。

建立Container Service(ASK)類型的Prometheus執行個體

  1. 建立一個工作目錄,並且在工作目錄中建立名為main.tf的設定檔,檔案內容如下:

    #provider, use alicloud
    provider "alicloud" {
      #access_key = "************"
      #secret_key = "************"
      #region     = "cn-beijing"
    }
    
    # PrometheusContainer Service執行個體。
    resource "alicloud_arms_prometheus" "my_ask1" {  cluster_id = "阿里雲Container ServiceID"
      cluster_type = "aliyun-cs"
      grafana_instance_id = "free 或 Grafana工作區ID"
      vpc_id = "Container ServiceASK所在的vpc-id"
      vswitch_id = "Container ServiceASK所綁定的vsw-id"
      security_group_id = "Container ServiceASK所綁定的sg-id"
    }
  2. 執行以下命令,初始化Terraform運行環境。

    terraform init

    預期輸出:

    Initializing the backend...
    
    Initializing provider plugins...
    - Checking for available provider plugins...
    - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1...
    ...
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. 執行以下命令,產生資源規劃。

    terraform plan

    預期輸出:

    Refreshing Terraform state in-memory prior to plan...
    The refreshed state will be used to calculate this plan, but will not be
    persisted to local or remote state storage.
    ...
    Plan: 5 to add, 0 to change, 0 to destroy.
    ...
  4. 執行以下命令,建立執行個體。

    terraform apply

    預期輸出:

    ...
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    ...
    alicloud_arms_prometheus: Creation complete after 8m26s [id=************]
    
    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

    若輸出結果出現yes,表示當前Prometheus執行個體建立成功。同時您可以登入可觀測監控 Prometheus 版控制台,在執行個體列表頁面查看已成功建立的Prometheus for Container Service執行個體。具體操作如下:

    1. 登入ARMS控制台

    2. 在左側導覽列選擇Prometheus監控 > 執行個體列表,進入可觀測監控 Prometheus 版的執行個體列表頁面。

    3. 在執行個體列表頁面查看已成功建立的Prometheus for Container Service執行個體。

建立ECS類型的Prometheus執行個體

  1. 建立一個工作目錄,並且在工作目錄中建立名為main.tf的設定檔,檔案內容如下:

    #provider, use alicloud
    provider "alicloud" {
      #access_key = "************"
      #secret_key = "************"
      #region     = "cn-beijing"
    }
    
    # PrometheusContainer Service執行個體。
    resource "alicloud_arms_prometheus" "my_vpc1" {    cluster_type = "ecs"
        cluster_name = "Prometheus執行個體名" # 命名規則: name-vpcId,如 my_vpc1-vpc-xxxx
        grafana_instance_id = "free 或 Grafana工作區ID"
        vpc_id = "ECS歸屬的vpc-id"
        vswitch_id = "ECS歸屬的vsw-id"
        security_group_id = "ECS歸屬的sg-id"
    }
  2. 執行以下命令,初始化Terraform運行環境。

    terraform init

    預期輸出:

    Initializing the backend...
    
    Initializing provider plugins...
    - Checking for available provider plugins...
    - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1...
    ...
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. 執行以下命令,產生資源規劃。

    terraform plan

    預期輸出:

    Refreshing Terraform state in-memory prior to plan...
    The refreshed state will be used to calculate this plan, but will not be
    persisted to local or remote state storage.
    ...
    Plan: 5 to add, 0 to change, 0 to destroy.
    ...
  4. 執行以下命令,建立執行個體。

    terraform apply

    預期輸出:

    ...
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    ...
    alicloud_arms_prometheus: Creation complete after 8m26s [id=************]
    
    Apply complete! Resources: 3 added, 0 changed, 0 destroyed.

    若輸出結果出現yes,表示當前Prometheus執行個體建立成功。同時您可以登入可觀測監控 Prometheus 版控制台,在執行個體列表頁面查看已成功建立的Prometheus for ECS執行個體。具體操作如下:

    1. 登入ARMS控制台

    2. 在左側導覽列選擇Prometheus監控 > 執行個體列表,進入可觀測監控 Prometheus 版的執行個體列表頁面。

    3. 在執行個體列表頁面查看已成功建立的Prometheus for ECS執行個體。

建立通用的Prometheus執行個體

  1. 建立一個工作目錄,並且在工作目錄中建立名為main.tf的設定檔,檔案內容如下:

    #provider, use alicloud
    provider "alicloud" {
      #access_key = "************"
      #secret_key = "************"
      #region     = "cn-beijing"
    }
    
    # PrometheusContainer Service執行個體。
    resource "alicloud_arms_prometheus" "my_rw1" { cluster_name = "Prometheus執行個體名"
      cluster_type = "remote-write"
      grafana_instance_id = "free 或 Grafana工作區ID"
    }
  2. 執行以下命令,初始化Terraform運行環境。

    terraform init

    預期輸出:

    Initializing the backend...
    
    Initializing provider plugins...
    - Checking for available provider plugins...
    - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1...
    ...
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. 執行以下命令,產生資源規劃。

    terraform plan

    預期輸出:

    Refreshing Terraform state in-memory prior to plan...
    The refreshed state will be used to calculate this plan, but will not be
    persisted to local or remote state storage.
    ...
    Plan: 5 to add, 0 to change, 0 to destroy.
    ...
  4. 執行以下命令,建立執行個體。

    terraform apply

    預期輸出:

    ...
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    ...
    alicloud_arms_prometheus: Creation complete after 8m26s [id=************]
    
    Apply complete! Resources: 4 added, 0 changed, 0 destroyed.

    若輸出結果出現yes,表示當前Prometheus執行個體建立成功。同時您可以登入可觀測監控 Prometheus 版控制台,在執行個體列表頁面查看已成功建立的通用Prometheus執行個體。具體操作如下:

    1. 登入ARMS控制台

    2. 在左側導覽列選擇Prometheus監控 > 執行個體列表,進入可觀測監控 Prometheus 版的執行個體列表頁面。

    3. 在執行個體列表頁面查看已成功建立的通用執行個體。

建立全域彙總執行個體

  1. 建立一個工作目錄,並且在工作目錄中建立名為main.tf的設定檔,檔案內容如下:

    #provider, use alicloud
    provider "alicloud" {
      #access_key = "************"
      #secret_key = "************"
      #region     = "cn-beijing"
    }
    
    # PrometheusContainer Service執行個體。
    resource "alicloud_arms_prometheus" "my_global1" 
    {  cluster_name = "Prometheus執行個體名"
      cluster_type = "global-view"
      grafana_instance_id = "free 或 Grafana工作區ID"
      subClustersJson = jsonencode( #"子執行個體列表json串"
       [
         {
           "headers": {},
           "regionId": "地區Id,如cn-hangzhou",
           "sourceType": "AlibabaPrometheus",
           "extras": {},
           "clusterId": "prometheus執行個體id",
           "sourceName": "prometheus執行個體名",
           "dataSource": "",
           "userId": "使用者id"
         },
         {
           "headers": {},
           "regionId": "地區Id,如cn-hangzhou",
           "sourceType": "AlibabaPrometheus",
           "extras": {},
           "clusterId": "prometheus執行個體id",
           "sourceName": "prometheus執行個體名",
           "dataSource": "",
           "userId": "使用者id"
         }
       ]
      )
    }
  2. 執行以下命令,初始化Terraform運行環境。

    terraform init

    預期輸出:

    Initializing the backend...
    
    Initializing provider plugins...
    - Checking for available provider plugins...
    - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1...
    ...
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. 執行以下命令,產生資源規劃。

    terraform plan

    預期輸出:

    Refreshing Terraform state in-memory prior to plan...
    The refreshed state will be used to calculate this plan, but will not be
    persisted to local or remote state storage.
    ...
    Plan: 5 to add, 0 to change, 0 to destroy.
    ...
  4. 執行以下命令,建立執行個體。

    terraform apply

    預期輸出:

    ...
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    ...
    alicloud_arms_prometheus: Creation complete after 8m26s [id=************]
    
    Apply complete! Resources: 5 added, 0 changed, 0 destroyed.

    若輸出結果出現yes,表示當前Prometheus執行個體建立成功。同時您可以登入可觀測監控 Prometheus 版控制台,在執行個體列表頁面查看已成功建立的Prometheus for Globalview執行個體。具體操作如下:

    1. 登入ARMS控制台

    2. 在左側導覽列選擇Prometheus監控 > 執行個體列表,進入可觀測監控 Prometheus 版的執行個體列表頁面。

    3. 在執行個體列表頁面查看已成功建立的Prometheus for Globalview執行個體。

刪除Prometheus執行個體

操作步驟

您可以執行以下命令刪除通過Terraform建立的叢集。

terraform destroy

預期輸出

...
Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes
...
Destroy complete! Resources: 1 destroyed.

結果驗證

  1. 登入ARMS控制台

  2. 在左側導覽列選擇Prometheus監控 > 執行個體列表,進入可觀測監控 Prometheus 版的執行個體列表頁面。

  3. 在執行個體列表頁面您可以看到已不存在目標Prometheus執行個體資訊,表示該Prometheus執行個體已被成功刪除。