使用Terraform管理Prometheus聚合视图

更新时间:
复制 MD 格式

通过 Terraform 创建 Prometheus 聚合视图,将多个 Prometheus 实例的监控数据统一汇聚展示,实现跨实例的集中监控管理。

前提条件

  • 已安装 Terraform(v1.5.0 及以上版本)。

  • 已配置阿里云账号信息。支持以下两种方式:

    说明

    建议创建 RAM 用户并授权,避免使用主账号 AccessKey。具体操作请参见创建RAM用户和管理RAM用户的权限。

    • 方式一(推荐):通过环境变量配置身份认证信息。

      export ALICLOUD_ACCESS_KEY="<YOUR-ACCESS-KEY>"
      export ALICLOUD_SECRET_KEY="<YOUR-SECRET-KEY>"
      export ALICLOUD_REGION="cn-beijing"
    • 方式二:在 Provider 代码块中指定身份认证信息。

      provider "alicloud" {
        access_key = "<YOUR-ACCESS-KEY>"
        secret_key = "<YOUR-SECRET-KEY>"
        region     = "cn-beijing"
        endpoints {
          cms = "metrics.cn-beijing.aliyuncs.com"
        }
      }
    重要

    必须显式指定 CMS endpoint,格式为 endpoints { cms = "metrics.<region-id>.aliyuncs.com" },其中 <region-id> 替换为实际的地域 ID。

  • 已创建至少一个 Prometheus 实例。如需通过 Terraform 创建,请参见管理Prometheus实例。

创建 Prometheus 聚合视图

创建单实例视图

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

    terraform {
      required_version = ">= 1.5.0"
    
      required_providers {
        alicloud = {
          source  = "aliyun/alicloud"
          version = "~> 1.277.0"
        }
      }
    }
    
    provider "alicloud" {
      access_key = "************"
      secret_key = "************"
      region = "cn-beijing"
      endpoints {
        cms = "metrics.cn-beijing.aliyuncs.com"
      }
    }
    
    data "alicloud_account" "default" {
    }
    
    resource "alicloud_cms_prometheus_instance" "this" {
      workspace               = "your-workspace-name"
      prometheus_instance_name = "my-prometheus-instance"
    }
    
    resource "alicloud_cms_prometheus_view" "default" {
      prometheus_view_name = "my-prometheus-view"
      version             = "V2"
      workspace           = alicloud_cms_prometheus_instance.this.workspace
    
      prometheus_instances {
        prometheus_instance_id = alicloud_cms_prometheus_instance.this.id
        region_id             = alicloud_cms_prometheus_instance.this.region_id
        user_id               = data.alicloud_account.default.id
      }
    }
    
    output "prometheus_view_id" {
      value = alicloud_cms_prometheus_view.default.id
    }

    参数说明如下:

    参数

    是否必填

    说明

    prometheus_view_name

    是

    聚合视图名称。

    version

    是

    视图版本,固定值为 V2。

    workspace

    是

    工作空间名称,需与关联实例的工作空间一致。

    prometheus_instances

    是

    关联的 Prometheus 实例配置块,可配置多个。包含 prometheus_instance_id(实例 ID)、region_id(地域 ID)、user_id(账号 ID)三个子参数。

  2. 初始化 Terraform 工作目录。

    terraform init
  3. 预览资源变更计划,确认将要创建的资源。

    terraform plan
  4. 执行创建。在交互提示中输入 yes 确认。

    terraform apply

    预期输出:

    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
    
    Outputs:
    prometheus_view_id = "************"

创建多实例聚合视图

聚合多个 Prometheus 实例的监控数据时,在 alicloud_cms_prometheus_view 中配置多个 prometheus_instances 块:

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

    terraform {
      required_version = ">= 1.5.0"
    
      required_providers {
        alicloud = {
          source  = "aliyun/alicloud"
          version = "~> 1.277.0"
        }
      }
    }
    
    provider "alicloud" {
      access_key = "************"
      secret_key = "************"
      region = "cn-beijing"
      endpoints {
        cms = "metrics.cn-beijing.aliyuncs.com"
      }
    }
    
    data "alicloud_account" "default" {
    }
    
    # 创建第一个 Prometheus 实例
    resource "alicloud_cms_prometheus_instance" "instance1" {
      prometheus_instance_name = "prometheus-instance-1"
      workspace               = "your-workspace-name"
    }
    
    # 创建第二个 Prometheus 实例
    resource "alicloud_cms_prometheus_instance" "instance2" {
      prometheus_instance_name = "prometheus-instance-2"
      workspace               = "your-workspace-name"
    }
    
    # 创建聚合视图,关联多个 Prometheus 实例
    resource "alicloud_cms_prometheus_view" "default" {
      prometheus_view_name = "prometheus-aggregation-view"
      version             = "V2"
      workspace           = alicloud_cms_prometheus_instance.instance1.workspace
    
      prometheus_instances {
        prometheus_instance_id = alicloud_cms_prometheus_instance.instance1.id
        region_id             = alicloud_cms_prometheus_instance.instance1.region_id
        user_id               = data.alicloud_account.default.id
      }
    
      prometheus_instances {
        prometheus_instance_id = alicloud_cms_prometheus_instance.instance2.id
        region_id             = alicloud_cms_prometheus_instance.instance2.region_id
        user_id               = data.alicloud_account.default.id
      }
    }
    
    output "prometheus_view_id" {
      value = alicloud_cms_prometheus_view.default.id
    }
    说明

    使用限制:

    • 所有关联实例必须属于同一个工作空间。

    • 聚合视图会自动汇总所有指定实例的监控数据。

  2. 初始化 Terraform 工作目录。

    terraform init
  3. 预览资源变更计划。

    terraform plan
  4. 执行创建。在交互提示中输入 yes 确认。

    terraform apply

    预期输出:

    Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
    
    Outputs:
    prometheus_view_id = "************"

导入已有视图

将控制台已创建的聚合视图纳入 Terraform 管理:

使用 import 块(Terraform v1.5+)

import {
  to = alicloud_cms_prometheus_view.default
  id = "<prometheus-view-id>"
}

使用 terraform import 命令

terraform import alicloud_cms_prometheus_view.default <prometheus-view-id>

删除 Prometheus 聚合视图

执行以下命令,删除通过 Terraform 管理的聚合视图及其关联资源:

terraform destroy

在交互提示中输入 yes 确认执行。预期输出:

alicloud_cms_prometheus_view.default: Destroying... [id=************]
alicloud_cms_prometheus_view.default: Destruction complete after XXs

Destroy complete! Resources: 1 destroyed.
说明

terraform destroy 会删除当前配置文件中定义的所有资源。如果仅需删除视图而保留实例,请先从配置文件中移除视图 Resource 定义,再执行 terraform apply。

超时时间

alicloud_cms_prometheus_view 资源支持以下超时配置:

操作

默认超时

create

5 分钟

update

5 分钟

delete

5 分钟

常见错误

常见错误及解决方法:

现象原因解决方法
Error: InvalidParameter.workspace 或工作空间不匹配错误prometheus_instances 块中指定的实例与视图不属于同一工作空间,或工作空间名称不正确。确保所有实例和视图引用同一工作空间。在云监控控制台的 工作空间管理 中核对工作空间名称。
Error: Forbidden.RAMRAM 用户没有管理 Prometheus 视图所需的权限。为 RAM 用户授予 AliyunCMSFullAccess 策略,或创建自定义策略,授予 cms:CreatePrometheusView 和 cms:DeletePrometheusView 权限。
Error: InvalidInstanceIdprometheus_instance_id 不存在,或实例已被删除。在可观测监控 Prometheus 版控制台的 实例列表 中检查引用的 Prometheus 实例是否存在。如果实例已被删除,请移除对应的 prometheus_instances 块。
在控制台手动修改后,Terraform 计划显示意外变更通过控制台修改了聚合视图,导致 Terraform 状态与实际资源之间发生漂移。运行 terraform plan 检查漂移。使用 terraform apply 使状态一致,或更新 main.tf 以匹配期望配置。