在ECS(Elastic Compute Service)机器上通过tar包部署 Prometheus 和 Pushgateway,并配置 Prometheus 采集 Pushgateway 的数据,是一个常见的监控部署任务。以下是详细的步骤说明:
🧩 环境准备
- 操作系统:Linux(如 CentOS、Ubuntu)
- 已安装 tar 命名
- 已开通 ECS 实例的相应端口(9090 for Prometheus, 9091 for Pushgateway)
✅ 第一步:下载 Prometheus 和 Pushgateway 的 tar 包
前往官网下载对应操作系统的二进制文件:
1. 下载 Prometheus
cd /opt
sudo wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz
sudo tar -zxvf prometheus-2.45.0.linux-amd64.tar.gz
sudo mv prometheus-2.45.0.linux-amd64 prometheus
2. 下载 Pushgateway
sudo wget https://github.com/prometheus/pushgateway/releases/download/v1.4.3/pushgateway-1.4.3.linux-amd64.tar.gz
sudo tar -zxvf pushgateway-1.4.3.linux-amd64.tar.gz
sudo mv pushgateway-1.4.3.linux-amd64 pushgateway
✅ 第二步:启动 Pushgateway
cd /opt/pushgateway
./pushgateway --web.listen-address=":9091"
默认监听端口为
9091
,你可以通过浏览器访问http://<ECS_IP>:9091
查看 Pushgateway 页面。
✅ 第三步:配置 Prometheus 以抓取 Pushgateway 数据
编辑 Prometheus 配置文件:
cd /opt/prometheus
nano prometheus.yml
在 scrape_configs
中添加如下内容:
scrape_configs:
- job_name: 'pushgateway'
static_configs:
- targets: ['localhost:9091']
honor_labels: true # 可选:保留推送时指定的标签
如果你希望 Prometheus 不自动添加时间戳和实例标签,可以加上:
scrape_interval: 15s
honor_labels: true
✅ 第四步:启动 Prometheus
cd /opt/prometheus
./prometheus --config.file=prometheus.yml --web.listen-address=:9090
访问
http://<ECS_IP>:9090
打开 Prometheus Web UI。
✅ 第五步:向 Pushgateway 推送测试数据
你可以使用 curl
向 Pushgateway 提交指标数据:
echo "some_metric 3.14" | curl --data-binary @- http://localhost:9091/metrics/job/some_job
你也可以带标签推送:
echo "some_metric{instance=\"my-instance\"} 3.14" | \
curl --data-binary @- http://localhost:9091/metrics/job/some_job
✅ 第六步:验证 Prometheus 是否采集成功
打开 Prometheus 的 Web UI(默认地址是 http://<ECS_IP>:9090
),输入查询语句:
some_metric
你应该能看到从 Pushgateway 推送上来的数据。
✅ (可选)配置守护进程运行
为了使 Prometheus 和 Pushgateway 在后台持续运行,建议使用 systemd 或 nohup。
示例:使用 nohup
启动守护进程
nohup /opt/pushgateway/pushgateway --web.listen-address=":9091" > /var/log/pushgateway.log 2>&1 &
nohup /opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --web.listen-address=:9090 > /var/log/prometheus.log 2>&1 &
✅ (可选)开放防火墙端口(适用于 ECS 安全组)
确保以下端口在阿里云安全组中已放行:
- 9090 (Prometheus)
- 9091 (Pushgateway)
✅ 总结
组件 | 地址 | 用途 |
---|---|---|
Prometheus | http://ECS_IP:9090 |
查询和展示监控数据 |
Pushgateway | http://ECS_IP:9091 |
接收外部推送的短期任务指标数据 |