All Products
Search
Document Center

Application Real-Time Monitoring Service:Use Terraform to manage the PodMonitor configurations of an environment instance

Last Updated:Jul 15, 2024

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

  1. Create a working directory, and create a configuration file named main.tf in 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
    }
    
  2. Run the following command to initialize the runtime environment for Terraform:

    terraform init

    Expected 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.
  3. Run the following command to create an execution plan:

    terraform plan

    Expected 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.
  4. Run the following command to create a PodMonitor configuration:

    terraform apply

    Expected 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: yes

    If Enter a value is returned, enter yes. The PodMonitor configuration of the current environment instance is created.

  5. Log on to the ARMS console.

  6. In the left-side navigation pane, click Integration Management. On the Integration Management page, click the name of the environment instance.

  7. 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

  1. Run the following command to delete the PodMonitor configuration that is created by using Terraform:

    terraform destroy

    Expected 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 value is returned, enter yes.

  2. Log on to the ARMS console. On the Integration Management page, click the name of the environment instance.

  3. Log on to the ARMS console.

  4. In the left-side navigation pane, click Integration Management. On the Integration Management page, click the name of the environment instance.

  5. 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.