

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

# Amazon CloudWatch Prometheus 解決方案：Amazon ECS 上的指標集合
<a name="Solution-Prometheus-On-ECS"></a>

此解決方案可協助您從 Amazon ECS 任務收集 Prometheus相容的指標，而無需執行或擴展您自己的集合基礎設施。您的任務會透過Prometheus匯出工具公開指標，而 Amazon CloudWatch 受管 Prometheus 收集器會透過 DNS 型服務探索來探索 VPC 中的任務、抓取其`/metrics`端點，並將指標交付至 CloudWatch。由於 Amazon ECS 任務是暫時性的，且其 IP 地址會變更，因此此解決方案會使用 AWS Cloud Map 服務探索，以便收集器一律抓取目前的任務集。如需所有 CloudWatch 可觀測性解決方案的一般資訊，請參閱[CloudWatch 可觀測性解決方案](Monitoring-Solutions.md)。如需受管收集器的詳細資訊，請參閱 [Amazon CloudWatch 受管 Prometheus 收集器](managed-prometheus-collectors.md)。

**Topics**
+ [要求](#Solution-Prometheus-On-ECS-Requirements)
+ [啟用Prometheus匯出工具](#Solution-Prometheus-On-ECS-Exporters)
+ [步驟 1：設定安全群組](#Solution-Prometheus-On-ECS-Security-Groups)
+ [步驟 2：設定抓取組態](#Solution-Prometheus-On-ECS-Scrape-Config)
+ [步驟 3：設定抓取器](#Solution-Prometheus-On-ECS-Create-Scraper)
+ [驗證指標集合](#Solution-Prometheus-On-ECS-Validate)
+ [建置自訂儀表板](#Solution-Prometheus-On-ECS-Dashboards)
+ [成本](#Solution-Prometheus-On-ECS-Costs)

## 要求
<a name="Solution-Prometheus-On-ECS-Requirements"></a>

此解決方案適用於下列情況：
+ 運算：公開Prometheus相容 指標的 Amazon ECS 任務 （在 Amazon EC2 或 AWS Fargate 上）。
+ 您向 AWS Cloud Map 註冊的 Amazon ECS 服務，用於 DNS 型服務探索。
+ 啟用 DNS 的 Amazon VPC，以及收集器不同可用區域中至少兩個子網路。
+ 安全群組，允許收集器連線到任務上的指標連接埠。如果您的任務和收集器共用安全群組，請新增指標連接埠的自我參考輸入規則。

## 啟用Prometheus匯出工具
<a name="Solution-Prometheus-On-ECS-Exporters"></a>

Prometheus 匯出工具是一種在 HTTP `/metrics`端點上以Prometheus表達格式公開指標的程序。在 Amazon ECS 上，您通常會在與應用程式相同的任務定義中，以附屬容器的形式執行匯出工具，或檢測應用程式以直接公開指標。常用的匯出工具如下：
+ **Node Exporter** — Amazon EC2 啟動類型 （預設連接埠 9100) 上任務的主機層級基礎設施指標。如需詳細資訊，請參閱 GitHub 上的 [Node Exporter](https://github.com/prometheus/node_exporter) 儲存庫。
+ **JMX Exporter** — Java 工作負載的 JVM 和 Java 應用程式指標。如需詳細資訊，請參閱 GitHub 上的 [JMX Exporter](https://github.com/prometheus/jmx_exporter) 儲存庫。
+ **NGINX Prometheus Exporter** — NGINX Web 伺服器和反向代理指標。如需詳細資訊，請參閱 GitHub 上的 [NGINX Prometheus Exporter](https://github.com/nginx/nginx-prometheus-exporter) 儲存庫。
+ **DCGM Exporter**：GPU 工作負載的 NVIDIA GPU 指標 （預設連接埠 9400)。如需詳細資訊，請參閱 GitHub 上的 [DCGM Exporter](https://github.com/NVIDIA/dcgm-exporter) 儲存庫。
+ **HAProxy Exporter** — HAProxy 負載平衡器指標。如需詳細資訊，請參閱 GitHub 上的 [HAProxy Exporter](https://github.com/prometheus/haproxy_exporter) 儲存庫。
+ **Apache Exporter** — Apache HTTP Server 指標。如需詳細資訊，請參閱 GitHub 上的 [Apache Exporter](https://github.com/Lusitaniae/apache_exporter) 儲存庫。

將匯出程式新增至您的任務定義，並遵循匯出程式的官方文件公開其指標連接埠。如需可用匯出程式的完整目錄，請參閱 Prometheus 文件中的[匯出程式和整合](https://prometheus.io/docs/instrumenting/exporters/)。

## 步驟 1：設定安全群組
<a name="Solution-Prometheus-On-ECS-Security-Groups"></a>

確認 Amazon ECS 任務的安全群組允許來自指標連接埠上收集器的傳入流量。當您的任務和收集器共用安全群組時，請新增自我參考的輸入規則，以便相同安全群組中的資源可以通訊。下列命令允許來自相同安全群組之連接埠 80 上的傳入流量：

```
aws ec2 authorize-security-group-ingress \
  --group-id {{sg-security-group-id}} \
  --ip-permissions IpProtocol=tcp,FromPort=80,ToPort=80,UserIdGroupPairs='[{GroupId={{sg-security-group-id}}}]'
```

## 步驟 2：設定抓取組態
<a name="Solution-Prometheus-On-ECS-Scrape-Config"></a>

對於您向 AWS Cloud Map 註冊的 Amazon ECS 任務，請使用 DNS 型服務探索 (`dns_sd_configs`) 在任務開始和停止時自動尋找和抓取任務。`dns_sd_configs` 區段指示收集器查詢 AWS Cloud Map DNS 名稱並抓取所有傳回的 IP 地址。當 Amazon ECS 取代任務時，收集器會接收下一個 DNS 查詢的變更。`relabel_configs` 區段會為您的指標新增一致的標籤，以便您可以跨 服務進行查詢和篩選。如需完整的支援組態選項，請參閱 [湊集器組態](managed-prometheus-collectors-scraper-configuration.md)。

```
global:
  scrape_interval: 30s
  scrape_timeout: 10s

scrape_configs:
  - job_name: 'ecs-payforadoption'
    dns_sd_configs:
      - names: ['{{payforadoption-go.Workshop-space}}']
        type: A
        port: 80
    metrics_path: '/metrics'
    relabel_configs:
      - target_label: service_name
        replacement: '{{payforadoption-go}}'
      - target_label: cloudmap_namespace
        replacement: '{{Workshop-space}}'
      - target_label: environment
        replacement: 'production'
      - target_label: compute_platform
        replacement: 'ecs-fargate'
```

## 步驟 3：設定抓取器
<a name="Solution-Prometheus-On-ECS-Create-Scraper"></a>

建立 VPC 連線的受管收集器來抓取 Amazon ECS 任務，並將指標交付到您的 CloudWatch 資料集：

您可以使用 [GetDefaultScraperConfiguration](https://docs.aws.amazon.com/prometheus/latest/APIReference/API_GetDefaultScraperConfiguration.html)來擷取一般用途的抓取器組態，或提供您自己的組態。

------
#### [ AWS API ]

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

```
POST /scrapers HTTP/1.1

{
  "alias": "ecs-payforadoption-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 "ecs-payforadoption-scraper" \
  --source '{
    "vpcConfiguration": {
      "subnetIds": ["{{subnet-subnet-id-1}}", "{{subnet-subnet-id-2}}"],
      "securityGroupIds": ["{{sg-security-group-id}}"]
    }
  }' \
  --scrape-configuration configurationBlob=$(cat {{ecs-scraper.yml}} | base64 -w 0) \
  --destination '{
    "cloudWatchConfiguration": {
      "datasetArn": "arn:aws:cloudwatch:{{us-west-2}}:{{123456789012}}:dataset/default"
    }
  }'
```

------

## 驗證指標集合
<a name="Solution-Prometheus-On-ECS-Validate"></a>

收集器開始交付指標後，請使用 Query Studio 在 CloudWatch 中執行臨機操作查詢，以確認您的指標到達。 [https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-PromQL-QueryStudio.html](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-PromQL-QueryStudio.html)例如，下列查詢會傳回過去五分鐘內，Amazon ECS 任務每秒的平均請求數：

```
rate(payforadoption_requests_total[5m])
```

## 建置自訂儀表板
<a name="Solution-Prometheus-On-ECS-Dashboards"></a>

收集器開始將 Amazon ECS 指標交付至 CloudWatch 之後，您可以建置自訂 CloudWatch 儀表板來視覺化它們。使用自訂儀表板，您可以將Prometheus匯出工具的指標合併為小工具、新增以 PromQL 為基礎的查詢，以及組織小工具以符合您的監控需求。如需詳細資訊，請參閱[使用 CloudWatch 儀表板](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)。

## 成本
<a name="Solution-Prometheus-On-ECS-Costs"></a>

我們會按小時向Prometheus收集器收費，並套用 CloudWatch OpenTelemetry 指標擷取定價。如需 CloudWatch 定價的詳細資訊，請參閱 [Amazon CloudWatch 定價](https://aws.amazon.com/cloudwatch/pricing/)。