在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: 15shonor_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 | 接收外部推送的短期任務指標數據 |