在K8s中,PodMonitor資源用于指定Prometheus需要監控的Pod。您可以在Terraform中配置PodMonitor資源并添加至Prometheus,使Prometheus實例能夠自動發現目標Pod并抓取監控指標,從而提高系統的穩定性和可靠性。
前提條件
已創建容器服務類型的環境。具體操作,請參見使用Terraform管理環境實例。
使用限制
僅支持容器服務類型的環境。
為容器服務環境實例添加PodMonitor
創建一個工作目錄,并在工作目錄中創建名為
main.tf
的配置文件,用于配置PodMonitor資源。provider "alicloud" { # access_key = "************" # secret_key = "************" # region = "cn-beijing" } # 容器服務環境實例的PodMonitor配置。 resource "alicloud_arms_env_pod_monitor" "my-pod-monitor1" { environment_id = "容器服務環境的ID,如env-xxxxx" config_yaml = <<-EOT apiVersion: monitoring.coreos.com/v1 kind: PodMonitor metadata: name: my-pod-monitor1 #podMonitor名稱 namespace: arms-prom #podMonitor所在的命名空間 annotations: arms.prometheus.io/discovery: 'true' #arms-prometheus固定要求的annotation,true則表示當前podMonitor有效,false則表示無效。 spec: selector: matchLabels: app: xxx release: yyy namespaceSelector: any: true podMetricsEndpoints: - interval: 30s targetPort: 9335 path: /metrics - interval: 10s targetPort: 9335 path: /metrics1 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_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.
執行以下命令,創建PodMonitor。
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_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
提示
Enter a value
時,輸入yes
,當前環境實例的PodMonitor創建成功。登錄ARMS控制臺。
在左側導航欄,單擊接入管理。在接入管理頁面的環境列表中,單擊目標環境名稱進入詳情頁面。
在環境詳情頁面,單擊指標采集頁簽,在PodMonitor列表中查看是否已成功創建PodMonitor配置。
刪除容器服務環境實例的PodMonitor
執行以下命令,刪除通過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控制臺。
在左側導航欄,單擊接入管理。在接入管理頁面的環境列表中,單擊目標環境名稱進入詳情頁面。
在環境詳情頁面,單擊指標采集頁簽,在PodMonitor列表中查看是否已成功刪除PodMonitor配置。
文檔內容是否對您有幫助?