Pushgateway是Prometheus的一個組件,prometheus server默認是通過Exporter主動獲取數(shù)據(jù)(默認采取pull拉取數(shù)據(jù)),Pushgateway則是通過exporter主動方式推送數(shù)據(jù)到Pushgateway,再由Prometheus主動去拉取 Pushgateway數(shù)據(jù),用戶可以寫一些自定義的監(jiān)控腳本把需要監(jiān)控的數(shù)據(jù)發(fā)送給Pushgateway。
一、概述
Pushgateway是Prometheus的一個組件,prometheus server默認是通過Exporter主動獲取數(shù)據(jù)(默認采取pull拉取數(shù)據(jù)),Pushgateway則是通過exporter主動方式推送數(shù)據(jù)到Pushgateway,再由Prometheus主動去拉取 Pushgateway數(shù)據(jù),用戶可以寫一些自定義的監(jiān)控腳本把需要監(jiān)控的數(shù)據(jù)發(fā)送給Pushgateway。從prometheus server角度看,都是由prometheus server主動去拉取各個數(shù)據(jù)源(例:Exporter和Pushgateway)的數(shù)據(jù)。
1、Pushgateway優(yōu)點:
- Prometheus 默認采用定時pull 模式拉取targets數(shù)據(jù),但是如果不在一個子網(wǎng)或者防火墻,prometheus就拉取不到targets數(shù)據(jù),所以可以采用各個target往pushgateway上push數(shù)據(jù),然后prometheus去pushgateway上定時pull數(shù)據(jù)。
- 在監(jiān)控業(yè)務數(shù)據(jù)的時候,需要將不同數(shù)據(jù)匯總, 匯總之后的數(shù)據(jù)可以由pushgateway統(tǒng)一收集,然后由 Prometheus 統(tǒng)一拉取,起到給Prometheus 減壓的作用。
- 自定義采集指標簡單。
2、Pushgateway缺點:
- Prometheus拉取狀態(tài)只針對 pushgateway, 不能對每個節(jié)點都有效。
- Pushgateway出現(xiàn)問題,整個采集到的數(shù)據(jù)都會出現(xiàn)問題。
- Pushgateway 可以持久化推送給它的所有監(jiān)控數(shù)據(jù)。因此,即使你的監(jiān)控已經(jīng)下線,prometheus 還會拉取到舊的監(jiān)控數(shù)據(jù),需要手動清理 pushgateway 不要的數(shù)據(jù)。
- 官方文檔:https://prometheus.io/docs/prometheus/
- Prometheus GitHub地址:https://github.com/prometheus/prometheus/
- Pushgetway GitHub地址:https://github.com/prometheus/pushgateway/
關于Prometheus整體介紹

二、Pushgateway 架構

- Pushgateway就是個數(shù)據(jù)中轉站。提供API,支持數(shù)據(jù)生產(chǎn)者隨時將數(shù)據(jù)推送過來。
- Pushgateway提供exporter功能,在promethus server拉取數(shù)據(jù)時,將自己保存的數(shù)據(jù)反饋給promethus server端。
三、Prometheus server 安裝
Prometheus基于Golang編寫,編譯后的軟件包,不依賴于任何的第三方依賴。用戶只需要下載對應平臺的二進制包,解壓并且添加基本的配置即可正常啟Prometheus Server。
1)下載
下載地址:https://prometheus.io/download/
wget https://github.com/prometheus/prometheus/releases/download/v2.40.6/prometheus-2.40.6.linux-amd64.tar.gz
tar -xf prometheus-2.40.6.linux-amd64.tar.gz
2)配置
解壓后當前目錄會包含默認的Prometheus配置文件promethes.yml,下面配置文件做下簡略的解析:
# 全局配置
global:
scrape_interval: 15s # 設置抓取間隔,默認為1分鐘
evaluation_interval: 15s #估算規(guī)則的默認周期,每15秒計算一次規(guī)則。默認1分鐘
# scrape_timeout #默認抓取超時,默認為10s
# Alertmanager相關配置
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# 規(guī)則文件列表,使用'evaluation_interval' 參數(shù)去抓取
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# 抓取配置列表
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
3)啟動服務
# 查看幫助
./prometheus -h
# 直接啟動服務,但是不提倡這種,因為退出控制臺服務也就退出了,雖然可以加nohup啟動,但是也不是特別友好,下面將配置prometheus.server啟動
# 默認端口是:9090,如需要修改默認端口,可以使用--web.listen-address=:9099,還可以指定配置文件--config.file=prometheus.yml
./prometheus
配置prometheus.service 啟動腳本
cat >/usr/lib/systemd/system/prometheus.service<<EOF
[Unit]
Descriptinotallow=Prometheus
After=network.target
[Service]
Type=simple
ExecStart=/opt/prometheus/prometheus_server/prometheus-2.40.6.linux-amd64/prometheus --config.file=/opt/prometheus/prometheus_server/prometheus-2.40.6.linux-amd64/prometheus.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
啟動服務
# 執(zhí)行 systemctl daemon-reload 命令重新加載systemd
systemctl daemon-reload
# 啟動
systemctl start prometheus
# 檢查
systemctl status prometheus
netstat -tnlp|grep :9090
ps -ef|grep prometheus

web訪問:http://ip:9090

四、Pushgateway 安裝
1)下載
下載地址:https://prometheus.io/download/#pushgateway
wget https://github.com/prometheus/pushgateway/releases/download/v1.5.1/pushgateway-1.5.1.linux-amd64.tar.gz
2)啟動服務
# 查看幫助
./pushgateway -h
# 啟動服務,這里也不使用直接啟動的方式,配置pushgateway.service啟動
./pushgateway
默認監(jiān)聽的是9091端口。可以通過以下配置進行更改:
usage: pushgateway [<flags>]
Flags:
--web.listen-address=":9091" 監(jiān)聽Web界面,API和遙測的地址。
--web.telemetry-path="/metrics" 公開metrics的路徑。
--web.external-url= 可從外部訪問Pushgateway的URL.
--web.route-prefix="" Web端點內部路由的前綴。 默認為--web.external-url的路徑.
--persistence.file="" 歸檔以保留metrics。 如果為空,則metrics僅保留在內存中.
--persistence.interval=5m 寫入持久性文件的最小間隔。
--log.level="info" 僅記錄具有給定嚴重性或更高嚴重性的消息。 有效級別:[debug, info, warn, error, fatal]
--log.format="logger:stderr" 設置日志目標和格式。 示例:“ logger:syslog?appname = bob&local = 7”或“ logger:stdout?json = true”
--version 顯示應用程序版本。
配置pushgateway.service 啟動腳本
cat >/usr/lib/systemd/system/pushgateway.service<<EOF
[Unit]
Descriptinotallow=Pushgetway
After=network.target
[Service]
Type=simple
ExecStart=/opt/prometheus/pushgateway/pushgateway-1.5.1.linux-amd64/pushgateway
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
啟動服務
# 執(zhí)行 systemctl daemon-reload 命令重新加載systemd
systemctl daemon-reload
# 啟動
systemctl start pushgateway
# 檢查
systemctl status pushgateway
netstat -tnlp|grep :9091
ps -ef|grep pushgateway

web訪問:ip:9091/metrics

3)接入Prometheus
更改prometheus配置文件,增加如下內容:
- job_name: 'pushgateway_name'
scrape_interval: 30s
honor_labels: true #加上此配置,exporter節(jié)點上傳數(shù)據(jù)中的一些標簽將不會被pushgateway節(jié)點的相同標簽覆蓋
static_configs:
- targets: ["192.168.182.110:9091"]
labels:
instance: pushgateway_instance
# pushgateway 中的數(shù)據(jù)我們通常按照 job 和 instance 分組分類,所以這兩個參數(shù)不可缺少。
重啟Prometheus服務,或進行熱加載
# curl -X POST http://192.168.182.110:9090/-/reload
systemctl restatus prometheus
再查看prometheus web界面:http://ip:9090/targets

五、實戰(zhàn)操作演示
1)推送數(shù)據(jù)定義
/metrics/job/<JOB_NAME>{/<LABEL_NAME>/<LABEL_VALUE>}
其中job是必須參數(shù),label_name部分是可選的,URL中的job和label組合唯一標識pushgateway中的Group。
- 在推送的數(shù)據(jù)部分,格式定義如下:
## TYPE metric_name type
metric_name{lable_name="label_value",...} value
1)推送數(shù)據(jù)
推送一個group定義為{job=“some_job”}的數(shù)據(jù)
echo "some_metric 3.14" | curl --data-binary @- http://192.168.182.110:9091/metrics/job/some_job
推送一個group定義為{job=“some_job”,instance=“some_instance”}的數(shù)據(jù)
# --data-binary 表示發(fā)送二進制數(shù)據(jù),注意:它是使用POST方式發(fā)送的!
cat <<EOF | curl --data-binary @- http://192.168.182.110:9091/metrics/job/some_job/instance/some_instance
# TYPE some_metric counter
some_metric2{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
EOF
2)刪除數(shù)據(jù)
刪除group定義為{job=“some_job”}下的所有數(shù)據(jù)
curl -X DELETE http://192.168.182.110:9091/metrics/job/some_job/instance/some_instance
刪除所有group下的所有metrics(啟動pushgateway時需加上命令行參數(shù)--web.enable-admin-api)
curl -X PUT http://192.168.182.110:9091/api/v1/admin/wipe
說明:
- 刪除數(shù)據(jù)是以Group為單位的,Group由job name和URL中的label唯一標識。
- 舉例中刪除{job=“some_job”}數(shù)據(jù)的語句并不會刪除{job=“some_job”,instance=“some_instance”}的數(shù)據(jù)。因為屬于不同的Group。如需要刪除{job=“some_job”,instance=“some_instance”}下的數(shù)據(jù),需要使用。
- 這里刪除數(shù)據(jù)是指刪除pushgateway中的數(shù)據(jù),跟promethues沒有關系。
上面的演示示例是官方提供:https://github.com/prometheus/pushgateway/
3)?定義編寫腳本的?法 發(fā)送pushgateway 采集
模板
cat <<EOF | curl --data-binary @- http://192.168.182.110:9091/metrics/job/some_job/instance/some_instance
# A histogram, which has a pretty complex representation in the text format:
# HELP http_request_duration_seconds A histogram of the request duration.
# TYPE http_request_duration_seconds histogram
disk_usage{instance="local-168-182-110",job="disk",disk_name="/run/user/0"} 11
disk_usage{instance="local-168-182-110",job="disk",disk_name="/run/user/1"} 22
disk_usage{instance="local-168-182-110",job="disk",disk_name="/run/user/2"} 33
disk_usage{instance="local-168-182-110",job="disk",disk_name="/run/user/3"} 44
disk_usage{instance="local-168-182-110",job="disk",disk_name="/run/user/4"} 55
EOF
編寫采集腳本推送數(shù)據(jù)到Pushgateway
cat >disk_usage_metris.sh<<EOF
#!/bin/bash
hostname=`hostname -f | cut -d '.' -f1`
metrics=""
for line in `df |awk 'NR>1{print $NF "=" int($(NF-1))}'`
do
disk_name=`echo $line|awk -F'=' '{print $1}'`
disk_usage=`echo $line|awk -F'=' '{print $2}'`
metrics="$metrics\ndisk_usage{instance=\"$hostname\",job=\"disk\",disk_name=\"$disk_name\"} $disk_usage"
done
echo -e "# A histogram, which has a pretty complex representation in the text format:\n# HELP http_request_duration_seconds A histogram of the request duration.\n# TYPE http_request_duration_seconds histogram\n$metrics" | curl --data-binary @- http://192.168.182.110:9091/metrics/job/pushgateway/instance/disk_usage
EOF
查看Pushgetway web

查看Prometheus web
