tencent cloud

腾讯云可观测平台

动态与公告
产品动态
产品简介
产品概述
产品优势
基本功能
基本概念
应用场景
使用限制
购买指南
云产品监控
应用性能监控
终端性能监控
前端性能监控
云拨测
Prometheus 监控服务
Grafana 服务
事件总线
云压测
快速入门
监控概览
实例分组
云产品监控
应用性能监控
云拨测
云压测
Prometheus 监控服务
Grafana 服务
创建 Dashboard
事件总线
告警服务
云产品监控
云产品监控指标
控制台操作指南
云服务器监控组件
云产品监控对接 Grafana
故障处理
实践教程
应用性能监控
应用性能监控简介
接入指南
控制台操作指南
实践教程
参考信息
常见问题
终端性能监控
终端性能监控概述
控制台操作指南
接入指南
实践教程
前端性能监控
前端性能监控简介
控制台操作指南
接入指南
常见问题
云拨测
产品简介
控制台操作指南
常见问题
云压测
云压测概述
控制台操作指南
实践教程
JavaScript API 列表
常见问题
Prometheus 监控
Prometheus 监控简介
接入指南
控制台操作指南
实践教程
Terraform
常见问题
Grafana 服务
产品简介
控制台操作指南
Grafana 平台常用功能指引
常见问题
Dashboard
什么是 Dashboard
控制台操作指南
告警管理
控制台操作指南
故障处理
常见问题
事件总线
事件总线简介
控制台操作指南
实践教程
常见问题
报表管理
常见问题
腾讯云可观测平台常见问题
告警服务相关
一般性问题
监控图表相关
云服务器监控组件相关
动态阈值告警相关
云监控对接 Grafana 相关
文档阅读指南
相关协议
应用性能监控服务等级协议
APM 隐私协议
APM 数据处理和安全协议
前端性能监控服务等级协议
终端性能监控服务等级协议
云拨测服务等级协议
Prometheus 监控服务服务等级协议
Grafana 服务服务等级协议
云压测服务等级协议
云压测使用限制
Cloud Monitor Service Level Agreement
词汇表
文档腾讯云可观测平台Prometheus 监控Terraform使用 Terraform 管理 Prometheus 实例

使用 Terraform 管理 Prometheus 实例

PDF
聚焦模式
字号
最后更新时间: 2025-03-19 10:32:18

前提条件

安装 Terraform

关于安装 Terraform 的具体操作,请参见在 本地安装和配置 Terraform
说明:
Terraform 安装版本不得低于 v1.6.3,您可通过 terraform --version 命令查看安装的 Terraform 版本。

配置腾讯云账号信息

在首次使用 Terraform 之前,请前往 云 API 密钥 申请安全凭证 SecretId 和 SecretKey。如果已有可使用的安全凭证,则跳过该步骤。
1. 登录 访问管理控制台,在左侧导航栏,进入访问密钥 > API 密钥管理
2. 在 API 密钥管理页面,单击新建密钥,即可以创建一对 SecretId / SecretKey。

配置腾讯云账号信息,有以下两种方式:

静态凭证鉴权
在用户目录下创建 provider.tf 文件,输入如下内容。其中my-secret-idmy-secret-key 需替换为密钥 SecretId 和 SecretKey。
provider "tencentcloud" {
secret_id = "my-secret-id"
secret_key = "my-secret-key"
}
环境变量鉴权
配置电脑环境变量或云端环境变量,请执行以下命令。其中YOUR_SECRET_ID 和 YOUR_SECRET_KEY 需替换为密钥 SecretId 和 SecretKey。
export TENCENTCLOUD_SECRET_ID=YOUR_SECRET_ID
export TENCENTCLOUD_SECRET_KEY=YOUR_SECRET_KEY

创建 prometheus 实例

1. 创建 Terraform 的配置文件。在项目目录中创建一个名为 main.tf 的文件,并使用合适的编辑器打开,在 main.tf 文件或者 provider.tf 文件中添加腾讯云提供者配置。
provider "tencentcloud" {  
region     = "your_region"  
secret_id  = "your_secret_id"  
secret_key = "your_secret_key"
}
2. 定义腾讯云资源:在 main.tf 文件中使用 Terraform 腾讯云提供的资源来定义您想要创建和管理的资源。
# 指定 provider 配置信息
terraform { required_providers { tencentcloud = { source = "tencentcloudstack/tencentcloud" } } }

# 创建 prometheus 实例

resource "tencentcloud_monitor_tmp_instance" "foo" {
instance_name = "tf-tmp-instance-sjtest"
vpc_id = "vpc-0n42dxzs"
subnet_id = "subnet-es8rv1kx"
data_retention_time = 30
zone = "ap-guangzhou-3"
tags = {
"createdBy" = "terraform"
}
}

# 创建 grafana 实例(选填)
resource "tencentcloud_monitor_grafana_instance" "foo" {
instance_name = "tf-grfana-cstest"
vpc_id = "vpc-0n42dxzs"
subnet_ids = ["subnet-es8rv1kx"]
grafana_init_password = "1234567890"
enable_internet = false
is_destroy = true

tags = {
"createdBy" = "test"
}
}

# grafana 与 Prometheus 实例做绑定(选填)
resource "tencentcloud_monitor_tmp_manage_grafana_attachment" "foo" {
grafana_id = tencentcloud_monitor_grafana_instance.foo.id
instance_id = tencentcloud_monitor_tmp_instance.foo.id
}
3. 初始化 Terraform 运行环境,执行命令如下:
terraform init
预期输出信息:
Initializing the backend...

Initializing provider plugins...
- Reusing previous version of tencentcloudstack/tencentcloud from the dependency lock file
- Using previously-installed tencentcloudstack/tencentcloud v1.81.32

Terraform has been successfully initialized!

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.
4. 生成资源规划,执行命令如下:
terraform plan
预期输出信息:
tencentcloud_vpc.vpc: Refreshing state... [id=vpc-3csjp7k8]
tencentcloud_monitor_tmp_instance.foo: Refreshing state... [id=prom-4wcvt7p1]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
following symbols:
+ create

Terraform will perform the following actions:

# tencentcloud_monitor_tmp_instance.foo will be created
+ resource "tencentcloud_monitor_tmp_instance" "foo" {
+ api_root_path = (known after apply)
+ data_retention_time = 30
+ id = (known after apply)
+ instance_name = "tf-tmp-instance-sjtest"
+ ipv4_address = (known after apply)
+ proxy_address = (known after apply)
+ remote_write = (known after apply)
+ subnet_id = "subnet-es8rv1kx"
+ tags = {
+ "createdBy" = "terraform"
}
+ vpc_id = "vpc-0n42dxzs"
+ zone = "ap-guangzhou-3"
}

Plan: 1 to add, 0 to change, 0 to destroy.
5. 创建实例,执行命令如下:
terraform apply
预期输出信息:
tencentcloud_vpc.vpc: Refreshing state... [id=vpc-3csjp7k8]
tencentcloud_monitor_tmp_instance.foo: Refreshing state... [id=prom-4wcvt7p1]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
following symbols:
+ create

Terraform will perform the following actions:

# tencentcloud_monitor_tmp_instance.foo will be created
+ resource "tencentcloud_monitor_tmp_instance" "foo" {
+ api_root_path = (known after apply)
+ data_retention_time = 30
+ id = (known after apply)
+ instance_name = "tf-tmp-instance-sjtest"
+ ipv4_address = (known after apply)
+ proxy_address = (known after apply)
+ remote_write = (known after apply)
+ subnet_id = "subnet-es8rv1kx"
+ tags = {
+ "createdBy" = "terraform"
}
+ vpc_id = "vpc-0n42dxzs"
+ zone = "ap-guangzhou-3"
}

Plan: 1 to add, 0 to change, 0 to destroy.
以下信息填入 “yes” 继续操作:
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.

Enter a value:
若出现以下信息,表示您已创建成功:
tencentcloud_monitor_tmp_instance.foo: Creating...
tencentcloud_monitor_tmp_instance.foo: Still creating... [10s elapsed]
tencentcloud_monitor_tmp_instance.foo: Creation complete after 12s [id=prom-8dyb6iny]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

查看 Prometheus 实例状态

登录 腾讯云可观测平台,在左侧导航栏选择 prometheus 监控,可在 prometheus 实例列表中看到存在的实例。

删除 Prometheus 实例

删除 Prometheus 实例,执行命令如下:
terraform destroy
出现以下信息填入提示您填入 “yes” 确认:
tencentcloud_monitor_tmp_instance.foo: Refreshing state... [id=prom-8dyb6iny]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
following symbols:
- destroy

Terraform will perform the following actions:

# tencentcloud_monitor_tmp_instance.foo will be destroyed
- resource "tencentcloud_monitor_tmp_instance" "foo" {
- api_root_path = "http://10.0.0.34:9090/api/v1" -> null
- data_retention_time = 30 -> null
- id = "prom-8dyb6iny" -> null
- instance_name = "tf-tmp-instance-sjtest" -> null
- ipv4_address = "10.0.0.34" -> null
- proxy_address = "10.0.0.34:9090" -> null
- remote_write = "http://10.0.0.34:9090/api/v1/prom/write" -> null
- subnet_id = "subnet-es8rv1kx" -> null
- tags = {
- "createdBy" = "terraform"
} -> null
- vpc_id = "vpc-0n42dxzs" -> null
- zone = "ap-guangzhou-3" -> null
}

Plan: 0 to add, 0 to change, 1 to 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

tencentcloud_monitor_tmp_instance.foo: Destroying... [id=prom-8dyb6iny]
tencentcloud_monitor_tmp_instance.foo: Destruction complete after 6s

Destroy complete! Resources: 1 destroyed.

说明:
当出现 Destroy complete! Resources: 1 destroyed. 表示您已删除该实例。


帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈