Use Terraform to create and deploy Prometheus scrape jobs to Container Service for Kubernetes (ACK) or Elastic Compute Service (ECS) environment instances. You can configure scrape targets, paths, and intervals to automate metric collection.
Prerequisites
A Container Service for Kubernetes (ACK) environment is created. For more information, see Use Terraform to manage an environment instance.
Limitations
Only Container Service for Kubernetes (ACK) and Elastic Compute Service (ECS) environment instances are supported.
Add a custom job for an environment instance
-
Create a file named
main.tfin a working directory to define the custom job resource.provider "alicloud" { # access_key = "************" # secret_key = "************" # region = "cn-beijing" } # Configure the custom job for the environment instance. resource "alicloud_arms_env_custom_job" "my-custom-job1" { env_custom_job_name = "my-custom-job1" environment_id = "env-xxxxx" # Replace with the ID of your ACK or ECS environment instance. 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 } -
Run the following command to initialize the Terraform working directory.
terraform initExpected output:
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. -
Run the following command to generate an execution plan.
terraform planExpected output:
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. -
Run the following command to create the custom job.
terraform applyExpected output:
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: yesAt the
Enter a valueprompt, typeyesand press Enter to create the custom job. -
Log on to the Cloud Monitor console.
-
In the navigation pane on the left, click Integration Management. On the Integration Management page, find your target environment instance and click its name.
-
On the environment details page, click the Metric Scraping tab, and then click Customize Collection. Verify that the custom job is listed.
Delete the custom job
-
Run the following command to delete the custom job.
terraform destroyExpected output:
... 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.At the
Enter a valueprompt, typeyesand press Enter. -
Log on to the Cloud Monitor console.
-
In the navigation pane on the left, click Integration Management. On the Integration Management page, find your target environment instance and click its name.
-
On the environment details page, click the Metric Scraping tab, and then click Customize Collection. Verify that the custom job is no longer listed.