Terraform支持管理Prometheus集成中心組件和Remote Write資源。本文介紹如何通過Terraform管理Remote Write配置。
前提條件
已創建Prometheus for容器服務或for ECS實例。具體操作,請參見使用Terraform管理Prometheus實例。
安裝Terraform。
Cloud Shell默認安裝配置了Terraform和阿里云賬號信息,您無需執行任何額外配置。
如果您不使用Cloud Shell,關于安裝Terraform的具體操作,請參見在本地安裝和配置Terraform。
說明請確認Terraform版本不低于v0.12.28,可通過terraform --version命令查看Terraform版本。
資源編排服務為Terraform提供了托管的能力,您可以創建Terraform類型的模板,定義阿里云、AWS或Azure資源,配置資源參數和資源間的依賴關系。更多信息,請參見創建Terraform類型模板、創建Terraform類型資源棧。
配置阿里云賬號信息。有以下兩種方式:
方式一:創建環境變量,用于存放身份認證信息。
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實例的Remote Write
創建一個工作目錄,并在工作目錄中創建名為main.tf的配置文件。
provider "alicloud" { }
執行以下命令,初始化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.
導入Remote Write資源。
將Remote Write資源添加到main.tf文件中。
# Prometheus實例的Remote Write配置。 resource "alicloud_arms_remote_write" "myRemoteWrite1" { cluster_id = "c77e1106f429e4b46b0ee1720c*****" #Prometheus實例Id remote_write_yaml = <<-EOT remote_write: - name: ArmsRemoteWrite1 url: http://47.96.XX.XX:8080/prometheus/xxx/yyy/cn-hangzhou/api/v3/write basic_auth: {username: 666, password: '******'} write_relabel_configs: - source_labels: [instance_id] separator: ; regex: si-6e2ca86444db4e5***** replacement: $1 action: keep EOT
執行以下命令,生成資源規劃。
terraform plan
預期輸出:
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: # alicloud_arms_remote_write.ack-rw1 will be created + resource "alicloud_arms_remote_write" "ack-rw1" { + cluster_id = "c77e1106f429e4b46b0ee1720c*****" + id = (known after apply) + remote_write_name = (known after apply) + remote_write_yaml = <<-EOT remote_write: - name: ArmsRemoteWrite1 url: http://xx.xx.xx.xx:8080/prometheus/xxx/yyy/cn-hangzhou/api/v3/write basic_auth: {username: 666, password: '******'} write_relabel_configs: - source_labels: [instance_id] separator: ; regex: si-6e2ca86444db4e5***** replacement: $1 action: keep EOT } Plan: 1 to add, 0 to change, 0 to destroy.
執行以下命令,創建Remote Write。
terraform apply
預期輸出:
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: # alicloud_arms_remote_write.ack-rw1 will be created + resource "alicloud_arms_remote_write" "ack-rw1" { + cluster_id = "c77e1106f429e4b46b0ee1720c9*****" + id = (known after apply) + remote_write_name = (known after apply) + remote_write_yaml = <<-EOT remote_write: - name: ArmsRemoteWrite1 url: http://xx.xx.xx.xx:8080/prometheus/xxx/yyy/cn-hangzhou/api/v3/write basic_auth: {username: 666, password: '******'} write_relabel_configs: - source_labels: [instance_id] separator: ; regex: si-6e2ca86444db4e5***** replacement: $1 action: keep EOT } Plan: 1 to add, 0 to change, 0 to destroy. 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
若輸出結果出現
yes
,表示當前Prometheus實例的Remote Write配置創建成功。
結果驗證
您可以登錄可觀測監控 Prometheus 版控制臺,然后在Prometheus實例的集成中心頁面,查看已成功創建的Remote Write配置。具體操作如下:
登錄ARMS控制臺。
在左側導航欄選擇 ,進入可觀測監控 Prometheus 版的實例列表頁面。
- 單擊目標Prometheus實例名稱,進入集成中心頁面。
單擊已安裝區域的自定義組件卡片,然后在彈出的面板中單擊Remote Write頁簽,查看已成功創建的RemoteWrite配置。
刪除Prometheus實例Remote Write
操作步驟
您可以執行以下命令刪除通過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.
結果驗證
您可以登錄可觀測監控 Prometheus 版控制臺,然后在Prometheus實例的集成中心頁面,查看Remote Write配置已被成功刪除。
登錄ARMS控制臺。
在左側導航欄選擇 ,進入可觀測監控 Prometheus 版的實例列表頁面。
- 單擊目標Prometheus實例名稱,進入集成中心頁面。
單擊已安裝區域的目標組件卡片,然后在彈出的面板中單擊Remote Write頁簽,您可以看到已不存在目標Remote Write配置信息,表示該Remote Write配置已被成功刪除。