View a markdown version of this page

Amazon CloudWatch Prometheus 解決方案:Amazon EC2 上的指標集合 - Amazon CloudWatch

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

Amazon CloudWatch Prometheus 解決方案:Amazon EC2 上的指標集合

此解決方案可協助您從 Amazon EC2 執行個體收集 Prometheus相容的指標,而無需執行或擴展您自己的集合基礎設施。您在執行個體上執行一或多個Prometheus匯出程式,Amazon CloudWatch 受管 Prometheus 收集器會探索 VPC 中的目標、抓取其/metrics端點,並將指標交付至 CloudWatch。如需所有 CloudWatch 可觀測性解決方案的一般資訊,請參閱CloudWatch 可觀測性解決方案。如需受管收集器的詳細資訊,請參閱 Amazon CloudWatch 受管 Prometheus 收集器

要求

此解決方案適用於下列情況:

  • 運算:執行一或多個Prometheus匯出程式的 Amazon EC2 執行個體。

  • 啟用 DNS 的 Amazon VPC,以及收集器不同可用區域中至少兩個子網路。

  • 安全群組,可讓收集器連線到執行個體上的匯出程式連接埠。

啟用Prometheus匯出工具

Prometheus 匯出工具是一種在 HTTP /metrics端點上以Prometheus表達格式公開指標的程序。您執行的匯出工具符合您要監控的工作負載。下列匯出工具常用於 Amazon EC2:

  • Node Exporter — 主機層級基礎設施指標,例如 CPU、記憶體、磁碟和網路 (預設連接埠 9100)。如需詳細資訊,請參閱 GitHub 上的 Node Exporter 儲存庫。

  • JMX Exporter — Java 工作負載的 JVM 和 Java 應用程式指標。如需詳細資訊,請參閱 GitHub 上的 JMX Exporter 儲存庫。

  • NGINX Prometheus Exporter — NGINX Web 伺服器和反向代理指標。如需詳細資訊,請參閱 GitHub 上的 NGINX Prometheus Exporter 儲存庫。

  • DCGM Exporter:適用於機器學習訓練和推論 (預設連接埠 9400) 等 GPU 工作負載的 NVIDIA GPU 指標。如需詳細資訊,請參閱 GitHub 上的 DCGM Exporter 儲存庫。

  • HAProxy Exporter — HAProxy 負載平衡器指標。如需詳細資訊,請參閱 GitHub 上的 HAProxy Exporter 儲存庫。

  • Apache Exporter — Apache HTTP Server 指標。如需詳細資訊,請參閱 GitHub 上的 Apache Exporter 儲存庫。

遵循執行個體的官方文件,在執行個體上安裝和執行每個匯出程式。請注意每個匯出工具接聽的連接埠,因為您在湊集組態中參考這些連接埠。如需可用匯出程式的完整目錄,請參閱 Prometheus 文件中的匯出程式和整合

啟動匯出工具後,透過從執行個體查詢其端點來確認它公開指標:

curl http://localhost:port-number/metrics

步驟 1:設定湊集組態

下列範例使用 static_configs搭配執行個體私有 DNS 名稱,在 Amazon EC2 執行個體上抓取範例應用程式 (連接埠 8080) 和 Node Exporter (連接埠 9100)。relabel_configs 區段會為您的指標新增一致的標籤,讓您可以跨 服務進行查詢和篩選。如需完整的支援組態選項,請參閱 湊集器組態

global: scrape_interval: 30s scrape_timeout: 10s scrape_configs: - job_name: 'ec2-metrics' static_configs: - targets: - ip-ip-address.us-west-2.compute.internal:8080 metrics_path: '/metrics' relabel_configs: - source_labels: [__address__] target_label: instance replacement: 'ec2-metrics-instance' - target_label: service replacement: 'ec2-metrics' - target_label: environment replacement: 'dev' metric_relabel_configs: - source_labels: [__name__] regex: '.*' action: keep - job_name: ec2-node-exporter static_configs: - targets: - ip-ip-address.us-west-2.compute.internal:9100

步驟 2:設定抓取器

建立 VPC 連線的受管收集器來抓取執行個體,並將指標交付至 CloudWatch 資料集。提供允許收集器連接匯出者連接埠的子網路和安全群組:

您可以使用 GetDefaultScraperConfiguration來擷取一般用途的抓取器組態,或提供您自己的組態。

AWS API

使用 CreateScraper API 操作建立具有 CloudWatch 目的地的抓取器。將子網路、安全群組和資料集資訊取代為您自己的值。

POST /scrapers HTTP/1.1 { "alias": "ec2-payment-service-scraper", "source": { "vpcConfiguration": { "subnetIds": ["subnet-subnet-id-1", "subnet-subnet-id-2"], "securityGroupIds": ["sg-security-group-id"] } }, "destination": { "cloudWatchConfiguration": { "datasetArn": "arn:aws:cloudwatch:us-west-2:123456789012:dataset/default" } }, "scrapeConfiguration": { "configurationBlob": "base64-encoded-blob" } }
AWS CLI

使用 create-scraper命令建立具有 CloudWatch 目的地的抓取器。將子網路、安全群組和資料集資訊取代為您自己的值。

aws amp create-scraper \ --alias "ec2-payment-service-scraper" \ --source '{ "vpcConfiguration": { "subnetIds": ["subnet-subnet-id-1", "subnet-subnet-id-2"], "securityGroupIds": ["sg-security-group-id"] } }' \ --scrape-configuration configurationBlob=$(cat ec2-scraper.yml | base64 -w 0) \ --destination '{ "cloudWatchConfiguration": { "datasetArn": "arn:aws:cloudwatch:us-west-2:123456789012:dataset/default" } }'

當您建立受管收集器時,Amazon Managed Service for Prometheus 會自動建立服務連結角色,授予收集器存取 VPC 資源和寫入 CloudWatch 資料集的許可。

驗證指標集合

在幾分鐘內,指標就會開始流入 CloudWatch 資料集。若要確認您的指標到達,請使用 Query Studio 在 CloudWatch 中執行臨機操作查詢。 https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-PromQL-QueryStudio.html例如,下列查詢會傳回ec2-metrics任務的指標:

{job="ec2-metrics"}

建置自訂儀表板

收集器開始將 Amazon EC2 指標交付至 CloudWatch 之後,您可以建置自訂 CloudWatch 儀表板來視覺化它們。使用自訂儀表板,您可以將Prometheus匯出工具的指標合併為小工具、新增以 PromQL 為基礎的查詢,以及組織小工具以符合您的監控需求。如需詳細資訊,請參閱使用 CloudWatch 儀表板

成本

我們會按小時向Prometheus收集器收費,並套用 CloudWatch OpenTelemetry 指標擷取定價。如需 CloudWatch 定價的詳細資訊,請參閱 Amazon CloudWatch 定價