您可以使用Terraform建立Prometheus監控作業,並配置監控對象、抓取路徑和抓取間隔等參數,將監控作業部署到Container Service環境執行個體或ECS環境執行個體中,實現Prometheus自動監控,使監控更加高效且易於管理。
前提條件
已建立Container Service類型的環境。具體操作,請參見使用Terraform管理環境執行個體。
使用限制
僅支援Container Service和ECS類型的環境。
添加環境執行個體的自訂Job
建立一個工作目錄,並在工作目錄中建立名為
main.tf的設定檔,用於配置自訂Job資源。provider "alicloud" { # access_key = "************" # secret_key = "************" # region = "cn-beijing" } # 環境執行個體的自訂Job配置。 resource "alicloud_arms_env_custom_job" "my-custom-job1" { env_custom_job_name = "自訂Job名稱,如my-custom-job1" environment_id = "Container Service或ECS環境的ID,如env-xxxxx" config_yaml = <<-EOT scrape_configs: - job_name: job-demo1 honor_timestamps: false honor_labels: false scrape_interval: 30s scheme: http metrics_path: /metric static_configs: - targets: - 127.0.0.1:9090 - job_name: job-demo2 honor_timestamps: false honor_labels: false scrape_interval: 30s scheme: http metrics_path: /metric static_configs: - targets: - 127.0.0.1:9090 http_sd_configs: - url: 127.0.0.1:9090 refresh_interval: 30s EOT }執行以下命令,初始化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.執行以下命令,產生資源規劃。
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_env_custom_job.myCustomJob1 will be created + resource "alicloud_arms_env_custom_job" "myCustomJob1" { env_custom_job_name = "myCustomJob1" environment_id = "xxx" config_yaml = .... } Plan: 1 to add, 0 to change, 0 to destroy.執行以下命令,建立自訂Job。
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_env_custom_job.myCustomJob1 will be created + resource "alicloud_arms_env_custom_job" "myCustomJob1" { env_custom_job_name = "myCustomJob1" environment_id = "xxx" config_yaml = .... } 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提示
Enter a value時,輸入yes,當前環境執行個體的自訂Job建立成功。登入ARMS控制台。
在左側導覽列,單擊接入管理。在接入管理頁面的環境列表中,單擊目標環境名稱進入詳情頁面。
在環境詳情頁面,單擊指標採集頁簽,然後單擊自訂採集,查看是否已成功建立自訂Job。
刪除Container Service環境執行個體的自訂Job
執行以下命令,刪除通過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.提示
Enter a value時,輸入yes。登入ARMS控制台。
在左側導覽列,單擊接入管理。在接入管理頁面的環境列表中,單擊目標環境名稱進入詳情頁面。
在環境詳情頁面,單擊指標採集頁簽,然後單擊自訂採集,查看是否已成功刪除自訂Job。