y85.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶、pushgateway和prometheus存储(十六)
发布时间
阅读量:
阅读量
Prometheus 摘要
告警抑制与静默
- 基于百分位数阈值的告警规则:超过80%或60%时触发告警并减少通知数量。
- 静默策略:超过阈值后手动 silence 节点或组件(如磁盘利用率>80%)。
- 示例代码:
`yaml- alert: 磁盘容量
expr: 100-(nodefilesystemfreebytes{fstype="ext4|xfs"}/nodefilesystemsizebytes{fstype="ext4|xfs"}*100) > 80 #磁盘容量利用率大于80%
for: 2m
labels:
severity: critical
annotations:
summary: "磁盘容量利用率过高~"
description: "磁盘容量利用率大于80% (目前使用{{value}}%)"- name: alertmanager_pod/rules
class: alertmanager_pod.rules
type: gauge
expr: 100-(nodefilesystemfreebytes{fstype="ext4|xfs"}/nodefilesystemsizebytes{fstype="ext4|xfs"}*100) > 60 #磁盘容量利用率大于60%
`
高可用配置- 基于负载均衡(HA)实现集群高可用性。
- 使用Gossip机制实现多个节点之间的信息交换与负载均衡。
- 配置示例:
yaml cluster: listen-address: "prometheus{{ fall back to }}{{ cluster.listen-address | default '' }}" peer {{ if cluster.permanent | is-set }} address="{{ cluster.peer | default '' }}" node: name {{ if label.mgr_name | is-set }} resource {{ if label.mgr resource | is-set }}
Pushgateway部署与数据推送- Pushgateway作为被动推送工具:
bash cd /apps/pushgateway; ./pushgateway --help; ./pushgateway --port-5999 --web.config.file=""
输出示例:
starting pushgateway on port :5999 Web interface at http://{{ self.host }}/api/v2. Web interface URL {{ self.host }}/api/v2. API URL {{ self.host }}/api/v2. TLS is disabled. Metrics are only kept in memory. Minimum interval at which to write out the persistence file is set to '5m' (5 minutes).- 数据验证:
bash curl --data-binary @- http://{{ host }}/metrics/job/mytest_job/instance/172.31.2.185 #发送两次curl请求以确认数据是否成功上传 curl --data-binary @- http://{{ host }}/metrics/job/mytestjob/instance/{{ instancename }} #验证数据是否被正确捕获 curl --data-binary @- http://{{ host }}/metrics/job/mytestjob/instance/{{ instancename }}
自定义采集与数据收集- 使用自定义脚本实现监控和数据推送:
`bash
12.6 告警抑制与静默
12.6.1 告警抑制
基于告警规则,超过80%就不在发60%的告警,即由60%的表达式触发的告警被抑制了。
root@prometheus1:/apps/prometheus# cat roles/server_roles.yml
groups:
- name: alertmanager_pod.rules
rules:
- alert: 磁盘容量
expr: 100-(node_filesystem_free_bytes{fstype=~"ext4|xfs"}/node_filesystem_size_bytes{fstype=~"ext4|xfs"}*100) > 80 #磁盘容量利用率大于80%
for: 2m
labels:
severity: critical
annotations:
summary: "{{$labels.mountpoint}} 磁盘分区使用率过高~"
description: "{{$labels.mountpoint}} 磁盘分区使用率大于80%(目前使用{{$value}}%)"
- name: alertmanager_pod.rules
rules:
- alert: 磁盘容量
expr: 100-(node_filesystem_free_bytes{fstype=~"ext4|xfs"}/node_filesystem_size_bytes{fstype=~"ext4|xfs"}*100) > 60 #磁盘容量利用率大于60%
for: 2m
labels:
severity: warning
annotations:
summary: "{{$labels.mountpoint}} 磁盘分区使用率过高~"
description: "{{$labels.mountpoint}} 磁盘分区使用率大于60%(目前使用{{$value}}%)"
12.6.2 手动静默
先要找到要静默的告警事件,然后手动静默指定的事件

点击silence(静默):

填写信息并创建:

查看当前被静默的事件:

验证告警是否被静默:

12.7 alertermanager高可用
12.7.1 单机
大多数应用[组件]时倾向于采用单点架构,请参考以下链接获取具体架构细节:

12.7.2 基于负载均衡

12.7.3 基于Gossip机制
该书的第二部分详细介绍了Prometheus的高级功能及其应用实践。其中重点讲述了Alert Manager模块的设计与实现原理以及其实现细节,并详细阐述了其在高可用性环境下的应用实践。
alertermanager引入Gossip机制。Gossip机制为多个alertermanager之间提供了信息传递的机制。确保即使在多个alertermanager分别接收到相同告警信息的情况下,并且只有一个告警通知被发送给Receiver。
集群环境搭建:
为了能够让alertermanager节点之间进行通讯,需要在alertermanager启动时设置相应的参数。其中主要的参数包括:
--cluster.listen-address string: 当前实例集群服务监听地址
--cluster.peer value: 初始化时关联的其它实例的集群服务地址

13.pushgateway
https://github.com/prometheus/pushgateway
13.1 pushgateway简介
pushgateway是采用被动推送的方式,而不是类似于prometheus server主动连接exporter获取监控数据。
pushgateway可以单独运行在一个节点,然后需要自定义监控脚本把需要监控的主动推送给pushgateway的API接口,然后pushgateway再等待prometheus server抓取数据,即pushgateway本身密钥任何抓取监控数据的功能,目前pushgateway只是被动的等待数据从客户端推送过来。
--persistence.file="" #数据保存的文件,默认只保存在内存中
--persistence.interval=5m #数据持久化的间隔事件

13.2 部署pushgateway
root@prometheus2:/apps# wget https://github.com/prometheus/pushgateway/releases/download/v1.4.3/pushgateway-1.4.3.linux-amd64.tar.gz
root@prometheus2:/apps# tar xf pushgateway-1.4.3.linux-amd64.tar.gz
root@prometheus2:/apps# ln -sv /apps/pushgateway-1.4.3.linux-amd64 /apps/pushgateway
'/apps/pushgateway' -> '/apps/pushgateway-1.4.3.linux-amd64'
root@prometheus2:/apps/pushgateway# ./pushgateway --help
usage: pushgateway [<flags>]
The Pushgateway
Flags:
-h, --help Show context-sensitive help (also try --help-long and --help-man).
--web.config.file="" [EXPERIMENTAL] Path to configuration file that can enable TLS or authentication.
--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.
--push.disable-consistency-check
Do not check consistency of pushed metrics. DANGEROUS.
--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.
root@prometheus2:/apps/pushgateway# ./pushgateway
ts=2022-05-31T07:23:01.620Z caller=main.go:85 level=info msg="starting pushgateway" version="(version=1.4.3, branch=HEAD, revision=f9dc1c8664050edbc75916c3664be1df595a1958)"
ts=2022-05-31T07:23:01.620Z caller=main.go:86 level=info build_context="(go=go1.18.2, user=root@75e397dd33fe, date=20220530-19:02:00)"
ts=2022-05-31T07:23:01.622Z caller=main.go:139 level=info listen_address=:9091
ts=2022-05-31T07:23:01.624Z caller=tls_config.go:195 level=info msg="TLS is disabled." http2=false
13.3 prometheus到pushgateway采集数据
13.3.1 验证pushgateway

13.3.2 prometheus配置数据采集
root@prometheus1:/apps/prometheus# vim prometheus.yml
...
- job_name: 'pushgateway_monitor'
scrape_interval: 5s
static_configs:
- targets: ['172.31.2.102:9091']
honor_labels: true
#honor_labels控制prometheus如何处理已经存在于已抓取数据中的标签与prometheus将附加服务器端的标签之间的冲突,(”job“和"instance"标签,手动配置的目标标签以及服务发现实现生成的标签)。
#如果”honor_labels“设置为”true“,则通过保留已抓取数据的标签值并忽略冲突的服务端标签来解决标签冲突。
#如果”honor_labels“设置为”false“,则通过将已经抓取数据中的冲突标签重命名为”exporter_<original-label>“(例如“exporter_instance”."exporter_job")然后附加服务器端标签来解决标签冲突。
root@prometheus1:/apps/prometheus# systemctl restart prometheus
13.3.3 验证数据

13.4 测试从客户端推送单条数据
13.4.1 推送单条数据
要push数据到pushgateway中,可以通过其提供的API标准接口来添加,默认URL地址为:
http://<ip>:9091/metrics/job/<JOBNAME>{/<LABEL_NAME>/<LABEL_VALUE>}.
其中<JOBNAME>是必填项,为job标签值,后边可以跟任意数量的标签对,一般我们会添加一个instance/<INSTANCE_NAME>实例名称标签,来方便区分各个指标。
推送一个job名称为mytest_job,key为mytest_metrics值为2022
root@node3:~# echo "mytest_metrics 2022" | curl --data-binary @- http://172.31.2.102:9091/metrics/job/mytest_job
root@node3:~# echo "mytest_metrics 2026" | curl --data-binary @- http://172.31.2.102:9091/metrics/job/mytest_job
13.4.2 pushgateway验证数据

除了mytest_metrics外,同时还新增了push_time_seconds和purh_failurs_time_seconds两个指标,这两个是pushgateway自动生成的指标,分别用于记录数据的成功上传时间和失败上传时间。

13.4.3 prometheus验证数据

13.5 测试从客户端推送多条数据
13.5.1 推送多条数据
root@node3:~# cat <<EOF | curl --data-binary @- http://172.31.2.102:9091/metrics/job/test_job/instance/172.31.0.100
#TYPE node_memory_usage gauge
node_memory_usage 4311744512
# TYPE memory_total gauge
node_memory_total 103481868288
EOF
13.5.2 pushgateway验证数据

13.5.3 prometheus验证数据

13.6 自定义收集数据
基于自定义脚本实现数据的收集和推送
13.6.1 自定义脚本
root@node3:~# cat mem_monitor.sh
#!/bin/bash
#
#**********************************************************************************************
#Author: Raymond
#QQ: 88563128
#Date: 2022-05-31
#FileName: mem_monitor.sh
#URL: raymond.blog..net
#Description: The test script
#Copyright (C): 2022 All rights reserved
#*********************************************************************************************
total_memory=$(free |awk '/Mem/{print $2}')
used_memory=$(free |awk '/Mem/{print $3}')
job_name="custom_memory_monitor"
NET_NAME=`ip addr |awk -F"[: ]" '/^2: e.*/{print $3}'`
instance_name=`ip addr show ${NET_NAME}| awk -F" +|/" '/global/{print $3}'`
pushgateway_server="http://172.31.2.102:9091/metrics/job"
cat <<EOF | curl --data-binary @- ${pushgateway_server}/${job_name}/instance/${instance_name}
#TYPE custom_memory_usage gauge
custom_memory_usage ${total_memory}
# TYPE custom_memory_total gauge
custom_memory_total ${used_memory}
EOF
#分别在不同主机上执行脚本,验证指标数据收集和推送
root@node3:~# bash mem_monitor.sh
root@node3:~# scp mem_monitor.sh 172.31.2.182:
root@node3:~# scp mem_monitor.sh 172.31.2.181:
root@node2:/apps/alertmanager# bash /root/mem_monitor.sh
root@node1:~# bash mem_monitor.sh
13.6.2 pushgateway验证数据

13.6.3 prometheus验证数据

13.7 删除数据
先对一组写入多个instance的数据:
root@node1:~# cat <<EOF | curl --data-binary @- http://172.31.2.102:9091/metrics/job/test_job/instance/172.31.0.100
#TYPE node_memory_usage gauge
node_memory_usage 4311744512
# TYPE memory_total gauge
node_memory_total 103481868288
EOF
root@node1:~# cat <<EOF | curl --data-binary @- http://172.31.2.102:9091/metrics/job/test_job/instance/172.31.0.100
#TYPE node_memory_usage gauge
node_memory_usage 4311744512
# TYPE memory_total gauge
node_memory_total 103481868288
EOF
13.7.1 通过API删除指定组内指定实例的数据
root@node1:~# curl -X DELETE http://172.31.2.102:9091/metrics/job/test_job/instance/172.31.0.100

13.7.2 通过web界面删除



14.prometheus存储系统
Prometheus采用了极高的效率方案来存储时间序列数据。每个采样数据仅占用约3.5 byte的空间。上百万条时间序列在每30秒间隔下持续存储60天左右的时长,在这种设计下总空间需求约为200 G(具体细节可见官方PPT)。
14.1 prometheus本地存储简介

默认情况下,prometheus将采集到的数据存储在本地的TSDB数据库中,路径默认为prometheus安装目录的data目录,数据写入过程为先把数据写入wal日志并放在内存,然后2小时后将内存数据保存至一个新的block块,同时再把新采集的数据写入内存并在2小时后再保存至一个新的block块,以此类推。

14.1.1 block简介
每一个block为一个data目录中以01开头的存储目录,如下:
root@prometheus1:/apps/prometheus# ll /apps/prometheus/data/
total 32
drwxr-xr-x 7 root root 4096 May 31 09:00 ./
drwxr-xr-x 5 3434 3434 4096 May 30 10:41 ../
drwxr-xr-x 3 root root 4096 May 31 07:27 01G4CG7123C0EA05EQ2F18TN9P/
drwxr-xr-x 3 root root 4096 May 31 09:00 01G4CNFYAW75SRYDPMJVV4EKWA/
drwxr-xr-x 3 root root 4096 May 31 09:00 01G4CNFYDD72VQCR9TF0672VY9/
drwxr-xr-x 2 root root 4096 May 31 09:30 chunks_head/
-rw-r--r-- 1 root root 0 May 31 05:27 lock
-rw-r--r-- 1 root root 20001 May 31 05:27 queries.active
drwxr-xr-x 3 root root 4096 May 31 09:00 wal/

14.1.2 block特性
block会压缩、合并历史数据块,以及删除过期的块,随着压缩、合并,block的数量会减少,在压缩过程中会发生三件事:定期执行压缩、合并小的block到大的block、清理过期的块。
每个块有4个部分:
root@prometheus1:/apps/prometheus# tree /apps/prometheus/data/01G4CG7123C0EA05EQ2F18TN9P
/apps/prometheus/data/01G4CG7123C0EA05EQ2F18TN9P
├── chunks
│ └── 000001 #数据目录,每个大小为512MB,超过会被切分为多个
├── index #索引文件,记录存储数据的索引信息,通过文件内的几个表来查找时序数据
├── meta.json #block元数据信息,包含了样本数、采集数据的起始时间、压缩历史
└── tombstones #逻辑数据,主要记录删除记录和标记删除的内容,删除标记,可在查询块时排除样本。
1 directory, 4 files
12.1.3 本地存储参数
--config.file="prometheus.yml" #指定配置文件
--web.listen-address="0.0.0.0:9090" #指定监听地址
--storage.tsdb.path="data/" #指定数据存储目录
--storage.tsdb.retention.size=B, KB, MB, GB, TB,PB, EB. Ex #指定chunk大小,默认512MB
--storage.tsdb.retention.time= #数据保存时长,默认15天
--query.timeout=2m #最大查询超时时间
--query.max-concurrency=20 #最大查询并发数
--web.read-timeout=5m #最大空闲超时时间
--web.max-connections=512 #最大并发连接数
--web.enable-lifecycle #启用API动态加载配置功能
常见的默认参数:
默认保留15天历史数据,--storage.tsdb.retention.time=30d
默认每间隔2小时把内存数据持久化一次,保存到data目录中,作为一个clock
默认每个chunk大小512M
默认数据在./data
-query.max-concurrency=20 #最大查询并发数
--web.max-connections=512 #最大并发连接数
--web.read-timeout=5m #最大空闲超时时间
--query.timeout=2m #最大查询超时时间
全部评论 (0)
还没有任何评论哟~
