4

Prometheus监控之pushgateway安装配置

 1 year ago
source link: https://blog.51cto.com/u_13236892/5968944
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Prometheus监控之pushgateway安装配置

精选 原创

哭泣的馒头 2022-12-26 14:31:11 博主文章分类:prometheus随笔 ©著作权

文章标签 hg 数据 推送 文章分类 Linux 系统/运维 yyds干货盘点 阅读数186

1、pushgateway是什么
pushgateway是另一种数据采集的方式,采用被动推送来获取监控数据的prometheus插件,它可以单独运行在任何节点上,并不一定要运行在被监控的客户端。
首先通过用户自定义编写的脚本把需要监控的数据发送给pushgateway,
pushgateway再将数据推送给对应的Prometheus服务。
对于短时运行、不支持轮询的任务,可以引入 pushgateway,将指标数值以 push 的方式推送到 pushgateway暂存,然后 prometheus 从 pushgateway 中轮询
pushgateway是Prometheus下的一个组件,用来当做采集对象和Prometheus的代理,Prometheus会定时的从gateway上面pull数据。

2、使用pushgateway的原因
原因一:因为Prometheus 采用 pull 模式,可能由于不在一个子网或者防火墙,导致 Prometheus 无法直接拉取各个 target 数据。
Prometheus 在一些情况下无法直接拉取各个 target 数据
原因二:在监控业务数据的时候,需要将不同数据汇总, 由 Prometheus 统一收集。

3、弊端
a:将多个节点数据汇总到 pushgateway, 如果pushgateway挂了,受影响比多个target大。
通过单个 Pushgateway 监控多个实例时, Pushgateway 将会成为单点故障和潜在瓶颈
b:Prometheus 拉取状态 up 只针对 pushgateway, 无法做到对每个节点有效。
c:Pushgateway可以持久化推送给它的所有监控数据。 因此,即使你的监控已经下线,prometheus还会拉取到旧的监控数据,需要手动清理pushgateway不要的数据

2、流程图

pushgateway结构图

Prometheus监控之pushgateway安装配置_hg

与prometheus结合流程图

Prometheus监控之pushgateway安装配置_数据_02

二、安装配置

下载地址
https://github.com/prometheus/pushgateway/releases/

2、安装并加入systemd管理

1、解压
tar -zxvf pushgateway-1.2.0.linux-amd64.tar.gz
cp -r pushgateway-1.2.0.linux-amd64 /usr/local/pushgateway

2、编写systemd管理文件
vi /usr/lib/systemd/system/pushgateway.service
[Unit]
Descriptinotallow=Prometheus Pushgateway daemon
After=network.target
[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/pushgateway/pushgateway \
--persistence.file=/usr/local/pushgateway/pushgateway_persist_file \
--persistence.interval=5m \
--web.listen-address=:9091
Restart=on-failure
[Install]
WantedBy=multi-user.target

3、配置说明
# --persistence.file=/usr/local/pushgateway/pushgateway_persist_file,指定持久化文件路径或名称。如果没有指定存储,则监
控指标仅保存在内存中,若出现pushgateway重启或意外故障,便会导致数据丢失。默认情况下,持久化文件每5分钟写一次,可以使用
“--persistence.interval”重新设置写入文件的时间间隔。
# --web.listen-address=:9091,进行端口设置。

4、重新加载system并启动pushgateway
systemctl daemon-reload
systemctl restart pushgateway
systemctl status pushgateway

5、访问
http://192.168.10.131:9091

3、启动参数

[root@root pushgateway-1.0.0]# ./pushgateway -h
usage: pushgateway [<flags>]

The Pushgateway

Flags:
-h, --help Show context-sensitive help (also try --help-long and --help-man).
#指定服务端口
--web.listen-address=":9091" Address to listen on for the web interface, API, and telemetry.
#指定暴露出去的接口
--web.telemetry-path="/metrics" Path under which to expose metrics.
--web.external-url= The URL under which the Pushgateway is externally reachable.
--web.route-prefix="" Prefix for the internal routes of web endpoints. Defaults to the path of --web.external-url.
--web.enable-lifecycle Enable shutdown via HTTP request.
--web.enable-admin-api Enable API endpoints for admin control actions.
#持久化存储的地址,否则重启后采集的指标就没有了
--persistence.file="" File to persist metrics. If empty, metrics are only kept in memory.
#持久化存储的间隔时间
--persistence.interval=5m The minimum interval at which to write out the persistence file.
--log.level=info Only log messages with the given severity or above. One of: [debug, info, warn, error]
--log.format=logfmt Output format of log messages. One of: [logfmt, json]
--version Show application version.

4、prometheus配置

因为Prometheus配置pushgateway的时候,指定job和instance,但是它只表示pushgateway实例,不能真正表达收集数据的含义。所以在
prometheus中配置pushgateway的时候,需要添加honor_labels: true参数,从而避免收集数据被push本身的job和instance被覆盖。不加
honor_labels: true,会取gateway的job和instance,设置了的话会取push过来的数据,job必填,instance没有就为""空字符串
- job_name: pushgetway
honor_labels: true
honor_timestamps: true
scrape_interval: 15s
scrape_timeout: 10s
metrics_path: /metrics
scheme: http
static_configs:
- targets:
- 127.0.0.1:9091
labels:
instance: pushgateway

5、数据测试

1、提交一条数据到 {job=‘some_job’}
echo "some_metric 3.14" | curl --data-binary @- http://pushgateway.example.org:9091/metrics/job/some_job

2、下面我们加上instance的值
echo "some_metrics 3.14" | curl --data-binary @- http://pushgateway.example.org:9091/metrics/job/some_job/instance/some_instance
可以看到pushgateway页面上产生了两个group,pgw是以job和instance分组的。用来更细力度的区分。

3、可以添加更多的标签,但是只会以job和instance区分
官方的例子如下:
cat <<EOF | curl --data-binary @- http://pushgateway.example.org:9091/metrics/job/some_job/instance/some_instance
# TYPE some_metric counter
some_metric{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
EOF
看起来太复杂,可以这么简单写
echo "some_metrics{tag=\"test\"} 3.14" | curl --data-binary @- http://127.0.0.1:9091/metrics/job/some_job/instance/some_instance
可以看到,这次并没有新增一个group,而且在同一个group下也没用多出来,而是把上一个覆盖了。

4、删除某个组下某实例的所有数据
curl -XDELETE http://pushgateway.example.org:9091/metrics/job/some_job/instance/some_instance

5、删除某个组下的所有数据
curl -X DELETE http://pushgateway.example.org:9091/metrics/job/some_job

6、删除所有,需要开启 --web.enable-admin-api
curl -X PUT http://pushgateway.example.org:9091/api/v1/admin/wipe

三、数据推送

1、数据推送格式

Pushgateway 监听的默认端口是 9091。路径看起来像:
/metrics/job/<JOB_NAME>{/<LABEL_NAME>/<LABEL_VALUE>}
要 Push 数据到 PushGateway 中,可以通过其提供的 API 标准接口来添加,
默认URL地址为:
http://<ip>:9091/metrics/job/<JOBNAME>{/<LABEL_NAME>/<LABEL_VALUE>},
<JOB_NAME>用作job标签的值,后跟任意数量的其他标签对(可能包含也可能不包含 instance标签)
一般我们会添加一个 instance/<INSTANCE_NAME>实例名称标签,来方便区分各个指标

2、简单命令行数据推送

echo "test_metric 123456" | curl --data-binary @- http://pushgateway.example.org:9091/metrics/job/test_job
刷新pushgateway,点击Metrics
除了test_metric外,同时还新增了push_time_seconds和push_failure_time_seconds两个指标,这两个是PushGateway系统自动生成的相关指标。
同时,在Prometheus UI页面上Graph页面可以查询test_metric的指标了。
test_metric{job="test_job"}

3、较为复杂数据的推送(命令行数据)

Push 一个复杂一些的,一次写入多个指标,而且每个指标添加 TYPE 及 HELP 说明。
推送命令:
cat <<EOF | curl --data-binary @- http://pushgateway.example.org:9091/metrics/job/test_job/instance/test_instance
# TYPE test_metrics counter
test_metrics{label="app1",name="demo"} 100.00
# TYPE another_test_metrics gauge
# HELP another_test_metrics Just an example.
another_test_metrics 123.45
EOF
指令说明:
/metrics/job/test_job和metrics/job/test_job/instance/test_instance,它们都属于test_job,但是它们属于两个指标值,因为instance对二者做了区分
在Prometheus UI页面上Graph页面可以查询test_metrics的指标
test_metrics{instance="test_instance",job="test_job",label="app1",name="demo"}

4、大量数据提交(通过写入文件将文件提交)

cat pgdata.txt
# TYPE http_request_total counter
# HELP http_request_total get interface request count with different code.
http_request_total{code="200",interface="/v1/save"} 276
http_request_total{code="404",interface="/v1/delete"} 0
http_request_total{code="500",interface="/v1/save"} 1
# TYPE http_request_time gauge
# HELP http_request_time get core interface http request time.
http_request_time{code="200",interface="/v1/core"} 0.122

curl -XPOST --data-binary @pgdata.txt http://pushgateway.example.org:9091/metrics/job/app/instance/app-172.30.0.0

四、删除指标

1、ui界面删除

2、命令行删除

删除 job=“test_job” 组下的所有指标值,可以执行如下命令:
curl -X DELETE http://pushgateway.example.org:9091/metrics/job/test_job

删除job="test_job"组下的所有指标值,不包括{job="test_job", instance="test_instance"}中的指标值,虽然它们的job名称都为test_job。
如果想删除该指标值,那么需要执行如下命令:
并且前提(requires to enable the admin API via the command line flag --web.enable-admin-api)
curl -X DELETE http://pushgateway.example.org:9091/metrics/job/test_job/instance/test_instance

五、使用注意事项

1、指标值只能是数字类型,非数字类型报错
echo "test_metric 12.34.56ff" | curl --data-binary @- http://pushgateway.example.org:9091/metrics/job/test_job_1
"text format parsing error in line 1: expected float as value, got "12.34.56ff""

2、指标值支持最大长度为 16 位,超过16 位后默认置为 0
echo "test_metric 1234567898765432123456789" | curl --data-binary @- http://pushgateway.example.org:9091/metrics/job/test_job_2
实际获取值 test_metric{job=“test_job_2”} 123456789876543200000000

3、PushGateway 数据持久化操作
默认PushGateway不做数据持久化操作,当PushGateway重启或者异常挂掉,导致数据的丢失,可以通过启动时添加-persistence.file和-persistence.interval参数来持久化数据。
-persistence.file 表示本地持久化的文件,将 Push 的指标数据持久化保存到指定文件,
-persistence.interval 表示本地持久化的指标数据保留时间,若设置为 5m,则表示 5 分钟后将删除存储的指标数据。

4、PushGateway推送及Prometheus拉取时间
设置Prometheus每次从PushGateway拉取的数据,并不是拉取周期内用户推送上来的所有数据,而是最后一次Push到PushGateway上的数据,
所以推荐设置推送时间小于或等于Prometheus拉取的时间,这样保证每次拉取的数据是最新Push上来的。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK