In Kubernetes clusters, PodMonitor resources are used to specify the pods to be monitored by Prometheus. You can configure PodMonitor resources in Terraform and add them to Managed Service for Prometheus. Then, Prometheus instances automatically discover pods and capture metrics. This improves system stability and reliability.
Prerequisites
An Container Service for Kubernetes (ACK) environment instance is created. For more information, see Use Terraform to manage an environment instance.
Limits
Only ACK environment instances are supported.
Create a PodMonitor configuration for the ACK environment instance
Create a working directory, and create a configuration file named
main.tfin the working directory to configure PodMonitor resources.provider "alicloud" { # access_key = "************" # secret_key = "************" # region = "cn-beijing" } # PodMonitor settings of the ACK environment instance. resource "alicloud_arms_env_pod_monitor" "my-pod-monitor1" { environment_id = "The ID of the environment instance. Example: env-xxxxx." config_yaml = <<-EOT apiVersion: monitoring.coreos.com/v1 kind: PodMonitor metadata: name: my-pod-monitor1 # The name of the PodMonitor configuration. namespace: arms-prom # The namespace where the PodMonitor configuration resides. annotations: arms.prometheus.io/discovery: 'true' # The specified annotation of arms-prometheus. The value "true" indicates that the current PodMonitor configuration is valid, and the value "false" indicates that the current PodMonitor configuration is invalid. spec: selector: matchLabels: app: xxx release: yyy namespaceSelector: any: true podMetricsEndpoints: - interval: 30s targetPort: 9335 path: /metrics - interval: 10s targetPort: 9335 path: /metrics1 EOT }Run the following command to initialize the runtime environment for Terraform:
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 create 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_pod_monitor.myPodMonitor1 will be created + resource "alicloud_arms_env_pod_monitor" "myPodMonitor1" { environment_id = "xxx" config_yaml = .... } Plan: 1 to add, 0 to change, 0 to destroy.Run the following command to create a PodMonitor configuration:
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_pod_monitor.myPodMonitor1 will be created + resource "alicloud_arms_env_pod_monitor" "myPodMonitor1" { 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: yesIf
Enter a valueis returned, enteryes. The PodMonitor configuration of the current environment instance is created.Log on to the ARMS console.
In the left-side navigation pane, click Integration Management. On the Integration Management page, click the name of the environment instance.
On the environment instance details page, click the Metric Scraping tab, and then click the Pod Monitor tab to check whether the PodMonitor configuration is created.
Delete the PodMonitor configuration from the ACK environment instance
Run the following command to delete the PodMonitor configuration that is created by using Terraform:
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.If
Enter a valueis returned, enteryes.Log on to the ARMS console. On the Integration Management page, click the name of the environment instance.
Log on to the ARMS console.
In the left-side navigation pane, click Integration Management. On the Integration Management page, click the name of the environment instance.
On the environment instance details page, click the Metric Scraping tab, and then click the Pod Monitor tab to check whether the PodMonitor configuration is deleted.