Prometheus监控实践

Prometheus简介

Prometheus是一个开源的系统监控和报警工具包,由SoundCloud公司开发,并使用Go语言编写。它起源于前Google工程师在2012年开始的项目,自那时起,许多公司和组织都采用了Prometheus作为他们的监控告警工具。在2016年,Prometheus加入了Cloud Native Computing Foundation(CNCF),成为继Kubernetes之后的第二个CNCF托管项目。

特点

  • 多维数据模型:Prometheus使用时间序列数据模型,可以对多种维度的数据进行监控和分析。

  • 灵活的查询语言:Prometheus提供了PromQL查询语言,允许用户灵活地对监控数据进行查询和分析。

  • 高效的数据存储:Prometheus采用本地存储方式,能够高效地存储监控数据,并支持数据压缩和数据刷写等功能。

  • 强大的告警系统:Prometheus内置了告警系统,可以根据监控数据设置告警规则,并及时发送告警通知。

缺点

  • Prometheus作为一个基于度量的系统,不适合存储事件或日志等。它更多地展示的是趋势性监控。
  • Prometheus本地存储的设计初衷只有一个月数据,不会正对大量的历史数据进行存储。如需历史数据建议使用Prometheus远程存储。

Prometheus架构

img

Job/Exporter

Job/Exporter属于Prometheus target,是Prometheus监控的对象;Job分为长时间执行和短时间执行两种,对于长时间执行的Job使用Prometheus Client集成进行监控;对于短时间执行的Job将监控数据推送到Pushgateway中缓存。

PushGateway

Prometheus是拉模式为主的监控系统,他的推模式是通过PushGateway组件实现。PushGateway是支持临时性Job主动推送指标的中间网关,它的本质上是一种用于监控Prometheus服务器无法抓取资源的解决方案。

PushGateway作为一个独立的服务,位于被采集监控指标的应用程序和Prometheus服务器之间。应用程序主动推送指标到PushGateway,PushGateway接受指标,然后PushGateway也作为target被Prometheus服务器抓取。

服务发现 Service Discovery

通过服务发现的方式,管理员可以在不重启Prometheus服务的情况下动态发现需要监控的target实例。服务发现中有一个高级操作就是Relabeling机制,从而对不同开发环境、不同业务团队、不同组织等按照某些规则从服务发现注册中心返回的target实例中选择性地采集某些Exporter实例的监控数据。

Prometheus Server

Prometheus服务器是Prometheus最核心的模块,主要负责抓取、存储和查询这三个功能。

  • 抓取:Prometheus Server通过服务发现组件周期性地从Job、Exporter、PushGateway这三个组件中通过HTTP轮询的形式拉去监控指标数据。
  • 存储:抓取到的监控数据通过一定的规则清理和数据整理,会把得到的结果存储到新的实践序列中进行持久化。储存分为:本地存储和远程存储,远程存储支持:OpenTSDB、InfluxDB、Elasticsearch、Graphite、CrateDB、Kakfa、PostgreSQL、Timescale DB等。
  • 查询:Prometheus持久化数据之后,客户端就可以通过Prom QL语句对数据进行查询了。

Dashboard

Web UI、Grafana、API client可以统一理解为Prometheus的Dashboard。Prometheus服务器除了内置查询语言Prom QL之外,还支持表达式浏览器以及表达式浏览器上的数据图像界面。实际工作中使用Grafana等作为前端展示界面,用户也可以直接使用Client向Prometheus Server发送请求获取数据。

Alertmanager

Alertmanager是独立于Prometheus的一个告警组件,需要单独安装部署。Prometheus可以将多个Alertmanager配置为一个集群,通过服务发现动态发现告警集群中节点的上下线从而避免单点问题。

Alertmanager接受Prometheus推送过来的告警,用于管理、整合和分发告警到不通的目的地。Alertmanager提供了多种内置的第三方告警通知方式,同时还提供了对Webhook通知的支持。通过Webhook用户可以完成对告警的更多个性化的扩展。

Prometheus部署

部署之前需要先生成认证需要的加密字符串,浏览器访问https://www.bejson.com/encrypt/bcrpyt_encode/ 生成bcrpyt密码

Docker

创建配置文件

mkdir -p /opt/prometheus/data
chmod 777 -R /opt/prometheus
cat > /opt/prometheus/prometheus.yml << 'EOF'
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
EOF

创建认证配置文件

cat > /opt/prometheus/basic_auth.yml << 'EOF'
basic_auth_users:
prometheus: $2a$10$cD2r0elgi4F2GlN/3oieEeUnuGW5/vnSBZEHAwWa0l2P5aK9IUbBm
EOF

启动容器

docker run -idt --name prometheus \
-p 9090:9090 \
-v /opt/prometheus:/prometheus \
--restart always \
prom/prometheus:v2.52.0 \
--web.config.file=/prometheus/basic_auth.yml --storage.tsdb.path=/prometheus/data --storage.tsdb.retention.time=30d

二进制

下载安装

wget https://github.com/prometheus/prometheus/releases/download/v2.51.2/prometheus-2.51.2.linux-amd64.tar.gz
tar xf prometheus-2.51.2.linux-amd64.tar.gz -C /usr/local
mv /usr/local/prometheus-2.51.2.linux-amd64 /usr/local/prometheus
mkdir -p /data/prometheus_data

创建Prometheus配置文件

# vi /usr/local/pometheus/prometheus.yml
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:

创建密码认证文件

cat > /usr/local/pometheus/basic_auth.yml << 'EOF'
basic_auth_users:
prometheus: $2a$10$cD2r0elgi4F2GlN/3oieEeUnuGW5/vnSBZEHAwWa0l2P5aK9IUbBm
EOF

创建systemd service配置文件

# vi /etc/systemd/system/prometheus-server.service
[Unit]
Description=prometheus
[Service]
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --web.config.file=/usr/local/prometheus/basic_auth.yml --storage.tsdb.path=/data/prometheus_data --storage.tsdb.retention.time=30d
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target

启动服务

systemctl daemon-reload
systemctl start prometheus-server
systemctl enable prometheus-server
systemctl status prometheus-server

测试访问

curl -u prometheus:femwGh75MGWm http://localhost:9090

Exporter部署

Node Exporter

Docker

创建目录

mkdir -p /opt/node_exporter

创建配置文件,将生成的bcrpy加密字符串替换密码部分。默认密码:femwGh75MGWm

cat >/opt/node_exporter/basic_auth.yml << 'EOF'
basic_auth_users:
prometheus: $2a$10$cD2r0elgi4F2GlN/3oieEeUnuGW5/vnSBZEHAwWa0l2P5aK9IUbBm
EOF

启动容器

docker run -idt --name node_exporter \
-p 9100:9100 \
--restart always \
-v /opt/node_exporter/basic_auth.yml:/usr/local/node_exporter/basic_auth.yml \
prom/node-exporter:v1.5.0 \
--web.config.file="/usr/local/node_exporter/basic_auth.yml"

二进制

下载安装

tar xf node_exporter-1.5.0.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/node_exporter-1.5.0.linux-amd64/ /usr/local/node_exporter

创建认证配置文件

cat >/usr/local/node_exporter/basic_auth.yml << 'EOF'
basic_auth_users:
prometheus: $2a$10$cD2r0elgi4F2GlN/3oieEeUnuGW5/vnSBZEHAwWa0l2P5aK9IUbBm
EOF

创建system service配置文件

cat > /usr/lib/systemd/system/node_exporter.service << EOF
[Unit]
Description=Node Exporter

[Service]
WorkingDirectory=/usr/local/node_exporter
ExecStart=/usr/local/node_exporter/node_exporter --web.config.file="/usr/local/node_exporter/basic_auth.yml"
[Install]
WantedBy=multi-user.target
EOF

启动服务

systemctl daemon-reload
systemctl enable node_exporter
systemctl restart node_exporter
systemctl status node_exporter

测试访问

curl -u prometheus:femwGh75MGWm http://localhost:9100

Prometheus配置文件添加

- job_name: node_exporter
# 添加监控主机的用户和密码
basic_auth:
username: prometheus
password: femwGh75MGWm
static_configs:
- targets:
- 192.168.1.1:9100
- 192.168.1.2:9100

MySQL Exporter

Docker

创建目录

mkdir -p /opt/mysqld_exporter

创建认证文件

cat >/opt/mysqld_exporter/basic_auth.yml << 'EOF'
basic_auth_users:
prometheus: $2a$10$cD2r0elgi4F2GlN/3oieEeUnuGW5/vnSBZEHAwWa0l2P5aK9IUbBm
EOF

启动容器

docker run -idt --name mysqld_exporter \
-p 9104:9104 \
--restart always \
-v /opt/mysqld_exporter/basic_auth.yml:/usr/local/mysqld_exporter/basic_auth.yml \
prom/mysqld-exporter:v0.15.1 \
--web.config.file="/usr/local/mysqld_exporter/basic_auth.yml"

二进制

下载安装

wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-amd64.tar.gz
tar xf mysqld_exporter-0.15.1.linux-amd64.tar.gz -C /usr/local/

创建export访问mysql的配置文件,vi /usr/local/mysqld_exporter-0.15.1.linux-amd64/my.cnf

[client]
user=root
password=xxxxxxxxx

创建认证配置文件

# vi /usr/local/node_exporter-1.5.0/basic_auth.yml
basic_auth_users:
prometheus: $2y$12$8Edp8VHNdxAXWa2nNFwabei93SyE8nLNjxdQp5vr9H1HFr8y/0Hbi

创建system service文件

# vim /usr/lib/systemd/system/mysqld_exporter.service
[Unit]
Description=mysqld_exporter
After=network.target

[Service]
User=root
Type=simple
ExecStart=/usr/local/mysqld_exporter-0.15.1/mysqld_exporter --config.my-cnf /usr/local/mysqld_exporter-0.15.1/my.cnf --collect.info_schema.processlist --web.config.file=/usr/local/mysqld_exporter-0.15.1/basic_auth.yml

Restart=on-failure

[Install]
WantedBy=multi-user.target

启动服务

systemctl daemon-reload
systemctl start mysqld_exporter.service
systemctl enable mysqld_exporter.service
systemctl status mysqld_exporter.service

测试访问

测试mysqld_exporter接口

curl -u prometheus:femwGh75MGWm http://localhost:9104

Prometheus配置文件中添加mysql job

- job_name: mysql-exporter
basic_auth:
username: prometheus
password: femwGh75MGWm
static_configs:
- targets:
- 10.217.13.40:9104
- 10.217.13.41:9104
- 10.217.13.42:9104
- 10.217.13.43:9104

重启Prometheus,请求Prometheus target检查服务是否正常

curl -u prometheus:femwGh75MGWm http://localhost:9090/api/v1/targets?endpoint=mysql

Grafana

GrafanaCNCF下可视化面板的Go语言项目,有图表和布局展 示,以及功能齐全的度量仪表盘和图形编辑器,主要用于大规模指标 数据的可视化展示,是网络架构和应用分析中最流行的时序数据展示 工具,目前支持绝大部分时序数据库,支持GraphiteElasticsearchInfluxDBPrometheusCloudWatchMySQLOpenTSDB等数据源。

安装

Docker

使用docker安装grafana,默认用户名和密码为admin

docker run -d --name grafana -p 3000:3000 --restart always grafana/grafana:10.4.2

配置

添加Prometheus数据源,配置完成之后保存。

image-20240418150528585

创建一个Dashboard

image-20240418150758484

Prom QL

获取当前主机可用内存空间大小,单位为MB

node_memory_free_bytes_total / (1024 * 1024)

基于2小时的样本数据,预测未来24小时内磁盘是否会满

IF predict_linear(node_filesystem_free[2h],24*3600)<0 

查询7天内每个节点CPU平均利用率

(avg(rate(node_cpu_seconds_total{instance="%s",mode!="idle"}[7d]))) * 100

查询7天内每个节点内存平均使用率

(1- avg_over_time(node_memory_MemAvailable_bytes{instance="%s"}[7d:1m]) / node_memory_MemTotal_bytes{instance="%s"}) * 100

查询7天内每个节点CPU最大利用率

max(rate(node_cpu_seconds_total{instance="%s",mode!="idle"}[7d])) * 100

查询7天内每个节点内存最大使用率

(1- (min_over_time(node_memory_MemAvailable_bytes{instance="%s"}[7d]) / node_memory_MemTotal_bytes{instance="%s"})) * 100

查询每个几点磁盘使用率

(node_filesystem_size_bytes{instance="%s"} - node_filesystem_free_bytes{instance="%s"}) / node_filesystem_size_bytes{instance="%s"} * 100

获取根分区磁盘利用率

100 * (sum(node_filesystem_size_bytes{mountpoint="/",fstype!="rootfs"} - node_filesystem_free_bytes{mountpoint="/",fstype!="rootfs"}) / sum(node_filesystem_size_bytes{mountpoint="/",fstype!="rootfs"}))
文章作者: 慕容峻才
文章链接: https://www.acaiblog.top/Prometheus监控实践/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 阿才的博客
微信打赏
支付宝打赏