文档

使用Terraform管理Prometheus实例

更新时间:

Terraform是HashiCorp公司提供的一种开源工具,用于安全高效地预览,配置和管理云基础架构和资源,帮助开发者自动化地创建、更新阿里云基础设施资源,并进行版本管理。本文介绍如何使用Terraform管理Prometheus实例。

前提条件

  • 安装Terraform。

    说明

    请确认Terraform版本不低于v0.12.28,可通过terraform --version命令查看Terraform版本。

    • Cloud Shell默认安装配置了Terraform和阿里云账号信息,您无需执行任何额外配置。

    • 如果您不使用Cloud Shell,关于安装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"
      }
      说明

      其中export ALICLOUD_REGION参数的值需要您根据实际情况进行替换。

创建Prometheus实例

创建容器服务(ACK)类型的Prometheus实例

  1. 创建一个工作目录,并且在工作目录中创建名为main.tf的配置文件,文件内容如下:

    #provider, use alicloud
    provider "alicloud" {
      #access_key = "************"
      #secret_key = "************"
      #region     = "cn-beijing"
    }
    
    # Prometheus容器服务实例。
    resource "alicloud_arms_prometheus" "my_ack1" {
      cluster_id = "阿里云容器服务ACK集群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 容器服务实例。具体操作如下:

    1. 登录Prometheus控制台

    2. 在左侧导航栏单击实例列表,进入可观测监控 Prometheus 版的实例列表页面。

    3. 在实例列表页面查看已成功创建的Prometheus for 容器服务实例。

创建容器服务(ASK)类型的Prometheus实例

  1. 创建一个工作目录,并且在工作目录中创建名为main.tf的配置文件,文件内容如下:

    #provider, use alicloud
    provider "alicloud" {
      #access_key = "************"
      #secret_key = "************"
      #region     = "cn-beijing"
    }
    
    # Prometheus容器服务实例。
    resource "alicloud_arms_prometheus" "my_ask1" {  cluster_id = "阿里云容器服务ID"
      cluster_type = "aliyun-cs"
      grafana_instance_id = "free 或 Grafana工作区ID"
      vpc_id = "容器服务ASK所在的vpc-id"
      vswitch_id = "容器服务ASK所绑定的vsw-id"
      security_group_id = "容器服务ASK所绑定的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 容器服务实例。具体操作如下:

    1. 登录Prometheus控制台

    2. 在左侧导航栏单击实例列表,进入可观测监控 Prometheus 版的实例列表页面。

    3. 在实例列表页面查看已成功创建的Prometheus for 容器服务实例。

创建ECS类型的Prometheus实例

  1. 创建一个工作目录,并且在工作目录中创建名为main.tf的配置文件,文件内容如下:

    #provider, use alicloud
    provider "alicloud" {
      #access_key = "************"
      #secret_key = "************"
      #region     = "cn-beijing"
    }
    
    # Prometheus容器服务实例。
    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. 登录Prometheus控制台

    2. 在左侧导航栏单击实例列表,进入可观测监控 Prometheus 版的实例列表页面。

    3. 在实例列表页面查看已成功创建的Prometheus for ECS实例。

创建通用的Prometheus实例

  1. 创建一个工作目录,并且在工作目录中创建名为main.tf的配置文件,文件内容如下:

    #provider, use alicloud
    provider "alicloud" {
      #access_key = "************"
      #secret_key = "************"
      #region     = "cn-beijing"
    }
    
    # Prometheus容器服务实例。
    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. 登录Prometheus控制台

    2. 在左侧导航栏单击实例列表,进入可观测监控 Prometheus 版的实例列表页面。

    3. 在实例列表页面查看已成功创建的通用实例。

创建全局聚合实例

  1. 创建一个工作目录,并且在工作目录中创建名为main.tf的配置文件,文件内容如下:

    #provider, use alicloud
    provider "alicloud" {
      #access_key = "************"
      #secret_key = "************"
      #region     = "cn-beijing"
    }
    
    # Prometheus容器服务实例。
    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. 登录Prometheus控制台

    2. 在左侧导航栏单击实例列表,进入可观测监控 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. 登录Prometheus控制台

  2. 在左侧导航栏单击实例列表,进入可观测监控 Prometheus 版的实例列表页面。

  3. 在实例列表页面您可以看到已不存在目标Prometheus实例信息,表示该Prometheus实例已被成功删除。