In Kubernetes, a PodMonitor defines which pods Prometheus scrapes for metrics. By managing PodMonitor resources through Terraform, you can version-control your monitoring configuration and let Managed Service for Prometheus automatically discover pods and collect metrics. This improves system stability and reliability.
This topic describes how to create and delete a PodMonitor configuration for a Container Service for Kubernetes (ACK) environment instance using the alicloud_arms_env_pod_monitor Terraform resource.
Prerequisites
Before you begin, make sure that you have:
An ACK environment instance. For setup instructions, see Use Terraform to manage an environment instance
Limits
Only ACK environment instances are supported.
Create a PodMonitor configuration
Create a working directory and add a
main.tffile with the following configuration: Replace the following placeholders with your values:Placeholder Description Example <environment-id>The ID of your ACK environment instance env-xxxxx<your-access-key>Your Alibaba Cloud AccessKey ID LTAI5tXxx<your-secret-key>Your Alibaba Cloud AccessKey secret xXxXxXx<region-id>The region ID cn-beijingKey YAML fields
Field Description metadata.nameA unique name for the PodMonitor. metadata.namespaceThe namespace where the PodMonitor resides. Use arms-prom.arms.prometheus.io/discoverySet to 'true'to activate the PodMonitor, or'false'to deactivate it.selector.matchLabelsLabels that identify the target pods. namespaceSelector.anySet to trueto match pods in all namespaces.podMetricsEndpointsA list of scrape endpoints, each with interval,targetPort, andpath.provider "alicloud" { # Set credentials through environment variables: # export ALICLOUD_ACCESS_KEY="<your-access-key>" # export ALICLOUD_SECRET_KEY="<your-secret-key>" # export ALICLOUD_REGION="<region-id>" } resource "alicloud_arms_env_pod_monitor" "my-pod-monitor1" { environment_id = "<environment-id>" # Example: env-xxxxx config_yaml = <<-EOT apiVersion: monitoring.coreos.com/v1 kind: PodMonitor metadata: name: my-pod-monitor1 namespace: arms-prom annotations: arms.prometheus.io/discovery: 'true' # Set to 'false' to disable this PodMonitor spec: selector: matchLabels: app: xxx release: yyy namespaceSelector: any: true # Match pods across all namespaces podMetricsEndpoints: - interval: 30s targetPort: 9335 path: /metrics - interval: 10s targetPort: 9335 path: /metrics1 EOT }Initialize Terraform: Expected output:
terraform initInitializing 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.Preview the changes: Expected output:
terraform planTerraform will perform the following actions: # alicloud_arms_env_pod_monitor.my-pod-monitor1 will be created + resource "alicloud_arms_env_pod_monitor" "my-pod-monitor1" { environment_id = "env-xxxxx" config_yaml = ... } Plan: 1 to add, 0 to change, 0 to destroy.Apply the configuration: When prompted with
Enter a value, typeyesto confirm. Terraform creates the PodMonitor configuration for your environment instance.terraform applyVerify in the ARMS console:
Log on to the ARMS console.
In the left-side navigation pane, click Integration Management.
Click the name of your environment instance.
Click the Metric Scraping tab, then click the Pod Monitor tab.
Confirm that your PodMonitor configuration appears in the list.
Delete a PodMonitor configuration
Destroy the PodMonitor resource: Expected output: When prompted with
Enter a value, typeyesto confirm.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.Verify in the ARMS console:
Log on to the ARMS console.
In the left-side navigation pane, click Integration Management.
Click the name of your environment instance.
Click the Metric Scraping tab, then click the Pod Monitor tab.
Confirm that the PodMonitor configuration has been removed.