

# 连接 VPC 的托管收集器
<a name="managed-prometheus-collectors-vpc-setup"></a>

连接 VPC 的 Amazon CloudWatch 托管 Prometheus 收集器会从 VPC 内可访问的任何资源（Amazon EC2 实例和 Amazon ECS 任务）中抓取与 Prometheus 兼容的指标。您提供子网和安全组；收集器会根据您的配置创建弹性网络接口（ENI）并抓取 Prometheus `/metrics` 端点。

有关特定工作负载的详细操作指南，请参阅用于 Prometheus 指标收集的 CloudWatch 可观测性解决方案：[Amazon CloudWatch 解决方案：Amazon EC2 上的 Prometheus 指标收集](Solution-Prometheus-On-EC2.md) 和 [Amazon CloudWatch 解决方案：Amazon ECS 上的 Prometheus 指标收集](Solution-Prometheus-On-ECS.md)。

## 先决条件
<a name="managed-prometheus-collectors-vpc-prerequisites"></a>

此过程假设您已熟悉 Amazon VPC 子网、安全组和 Prometheus 导出程序端点。
+ 已启用 DNS 的 Amazon VPC
+ 至少两个子网位于不同的可用区
+ 允许收集器访问目标导出程序端口的安全组
+ 公开与 Prometheus 兼容的 `/metrics` 端点的目标

收集器通过 Amazon 网络将抓取的指标传输至 CloudWatch，无需经过公有互联网。您指定的子网和安全组用于提供与抓取目标之间的连接；不提供 CloudWatch 传输路径。无需在 VPC 中配置互联网访问、NAT 网关或 CloudWatch 接口 VPC 端点即可进行指标传输。

## 创建抓取程序
<a name="managed-prometheus-collectors-vpc-create-scraper"></a>

您可以使用 [GetDefaultScraperConfiguration](https://docs.amazonaws.cn/prometheus/latest/APIReference/API_GetDefaultScraperConfiguration.html) 以检索通用抓取程序配置，也可以提供自己的抓取程序配置。

------
#### [ Amazon API ]

使用 `CreateScraper` API 操作，以便创建具有 CloudWatch 目标的抓取程序。将子网、安全组及数据集信息替换为您自己的值。

```
POST /scrapers HTTP/1.1

{
  "alias": "vpc-metrics-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}}"
  }
}
```

------
#### [ Amazon CLI ]

使用 `create-scraper` 命令，以便创建具有 CloudWatch 目标的抓取程序。将子网、安全组及数据集信息替换为您自己的值。

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

------

## 从 Amazon EC2 收集指标
<a name="managed-prometheus-collectors-vpc-ec2"></a>

要从运行 Prometheus 导出程序（例如 DCGM Exporter 或 Node Exporter）的 Amazon EC2 实例中抓取指标，请使用带有实例私有 IP 地址的 `static_configs`：

```
global:
  scrape_interval: 60s

scrape_configs:
  - job_name: 'ec2-node-exporter'
    static_configs:
      - targets:
          - '{{10.0.1.10}}:9100'
          - '{{10.0.1.11}}:9100'
    relabel_configs:
      - source_labels: [__address__]
        target_label: instance
      - target_label: compute_platform
        replacement: 'ec2'

  - job_name: 'ec2-dcgm-exporter'
    static_configs:
      - targets:
          - '{{10.0.1.10}}:9400'
          - '{{10.0.1.11}}:9400'
    relabel_configs:
      - source_labels: [__address__]
        target_label: instance
      - target_label: compute_platform
        replacement: 'ec2'
```

## 从 Amazon ECS 收集指标
<a name="managed-prometheus-collectors-vpc-ecs"></a>

对于在 Amazon Cloud Map 中注册的 Amazon ECS 任务，请使用 DNS 服务发现功能自动查找并抓取容器：

```
global:
  scrape_interval: 60s

scrape_configs:
  - job_name: 'ecs-services'
    dns_sd_configs:
      - names:
          - '{{my-service.my-namespace.local}}'
        type: A
        port: 9090
    relabel_configs:
      - source_labels: [__meta_dns_name]
        target_label: service_name
      - source_labels: [__address__]
        target_label: instance
      - target_label: compute_platform
        replacement: 'ecs'
```

## 跨账户可观测性
<a name="managed-prometheus-collectors-vpc-cross-account"></a>

对于跨账户 VPC 监控，建议使用 Amazon CloudWatch 指标集中化。有关更多信息，请参阅 [CloudWatch 指标集中化](https://docs.amazonaws.cn/AmazonCloudWatch/latest/monitoring/CloudWatch-Unified-Cross-Account.html)。

有关其他跨账户抓取程序配置，请参阅《Amazon Managed Service for Prometheus 用户指南》**中的[跨账户抓取程序](https://docs.amazonaws.cn/prometheus/latest/userguide/AMP-collector-cross-account.html)。

## 安全最佳实践
<a name="managed-prometheus-collectors-vpc-security"></a>
+ 为收集器 ENI 指定未直接连接互联网的私有子网。
+ 将安全组入口限制为仅在特定导出程序端口上的抓取程序安全组。
+ 在可行的情况下，为所有导出程序端点启用传输中 TLS 加密。