本文主题:
- prometheus 介绍
- prometheus安装部署
- prometheus 监控本地服务器
- prometheus监控远程服务器
- prometheus监控mariadb
Prometheus 是什么?
Prometheus是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的。随着发展,越来越多公司和组织接受采用Prometheus,社区也十分活跃,他们便将它独立成开源项目,并且有公司来运作。google SRE的书内也曾提到跟他们BorgMon监控系统相似的实现是Prometheus。现在最常见的Kubernetes容器管理系统中,通常会搭配Prometheus进行监控。
Prometheus 的优点
1. 配置灵活
使用过zabbix的同学应该都体验过安装时候的安装依赖包的痛苦,而premetheus核心部分只有一个单独的二进制文件,下载解压安装即可,没有依赖的第三方库。
2. 监控更多多样性
prometheus支持多种语言的的客户端,我们通过客户端方便对核心业务进行埋点,比如下单,添加购物车等流程。 而且prometheus已经有非常多的系统集成:比如
应用层面的监控:nginx,haproxy,mysql ,docker等
系统层面如: SNMP协议监控,主机监控,进程监控
容器云监控: docker,k8s openstack私有云
3. 非常高效的存储
平均一个采样数据占 ~3.5 bytes左右,320万的时间序列,每30秒采样,保持60天,消耗磁盘大概228G。
prometheus的架构
对照图中的每一部分进行介绍:
- prometheus server 主要负责数据的采集和存储,提供PromQL查询语言支持 Retrieval:采样模块,prometheus的服务器在哪里拉取数据,检索拉取到的数据分发给 TSDB进行存储 TSDB:存储模块默认本地存储为TSDB HTTP server : 提供http接口查询和面板,默认端口为9090
- short-lived jobs: 存在时间不足以被删除的短暂或批量业务,无法通过pull的方式拉取,需要使用push的方式,与pushgeteway结合使用
- Service Discovery: 服务发现,prometheus支持多种服务发现机制: 文件,DNS,k8s,openstack,等,基于服务发现的过程,通过第三方接口,prometheus查询到需要监控的target列表,然后轮询这些target获取监控数据
- 客户端SDK 官方提供的客户端类库有go,java,python,ruby
- pushgateway 支持临时性的job主动推送指标的中间网关,prometheus默认通过pull方式从exporters拉取,但有些情况我们是不允许promethes与exporters直接进行通信的,这时候我们可以使用pushgateway由客户端主动push数据到pushgateway,在由prometheus拉取。很多时候我们需要自定义一些组件来采集
- proDash 使用rails开发的dashboard,用于可视化指标数据
- exporters 支持其他数据源的指标导入到prometheus,支持数据库,硬件,消息中间件,存储系统,http服务器,jmx等 负责收集目标对象的性能数据,并通过http接口供prometheus server获取 只要符合接口格式,就可以被采集
- alertmanager 实验性组件,用来进行报警
- prometheus_cli 命令行工具
prometheus的工作流程
- prometheus server 定期从配置好的jobs或者exporters中拉metrics.或者接受来自pushgateway发过来的metrics,或者从其他的prometheus server中拉取metrics
- prometheus server 在本地存储收集到的metrics,并运行已经定义好的arlt.rules,记录新的时间序列或者向alertmanager推送报警
- Alertmanager根据配置文件,对接受的警报进行处理,发出告警
- 在图形界面中,可视化采集数据,可以使用别人写好的grafana模板。
版本(官网提供版本)

实验环境的检查:
[root@laoxin-06 ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
[root@laoxin-06 ~]# free -m
total used free shared buff/cache available
Mem: 3770 887 1610 47 1273 2591
Swap: 2047 0 2047
[root@laoxin-06 ~]# ifconfig |grep inet |awk 'NR==1{print $2}'
192.168.3.71
prometheus 部署
下载地址:https://github.com/prometheus/prometheus/releases/
[root@zmedu-17 ~]# tar xf prometheus-2.7.1.linux-amd64.tar.gz
[root@zmedu-17 ~]# mv prometheus-2.7.2.linux-amd64 /usr/local/prometheus
[root@zmedu-17 ~]# cd /usr/local/prometheus/
[root@zmedu-17 prometheus]# pwd
[root@zmedu-17 prometheus]# ls
console_libraries NOTICE promtool
consoles prometheus
LICENSE prometheus.yml
[root@zmedu-17 prometheus]# ./prometheus --version
prometheus, version 2.7.2 (branch: HEAD, revision: 82f98c825a1437c0a24056eb4dc986d9a3fc7fae)
build user: root@ab86c672c33a
build date: 20190302-14:57:04
go version: go1.11.5
[root@zmedu-17 prometheus]#
后台启动普罗米修斯:
[root@zmedu-17 prometheus]# ./prometheus & [1] 66474 如果出现: ts=2022-05-17T06:54:41.844Z caller=main.go:897 level=info msg="Server is ready to receive web requests." 证明启动成功了
通过网址访问
通过后台执行http://192.168.1.17:9090
web端的基本使用及介绍
http:.//ip:9090
查看监控目标:
http://192.168.1.17:9090/metrics

可以看到系统默认监控了自己的主机信息,监控接口通过http://服务器IP:9090/metrics 可以直接看到监控数据,如下图:
第一张图中的localhost:9090/metrics中的localhost要改成自己的ip
这里面是prometheus开放的接口。
prometheus默认监控图像
process_cpu_seconds_total

在图1的位置输入process_cpu_seconds_total ,可以查看到cpu的总量。
[root@itlaoxin-17 ~]# curl localhost:9100/metrics |grep node_memory_MemFree
##查看第一个cpu的空闲状态
node_cpu_seconds_total{cpu=”0″,mode=”idle”}
## 计算第一个cpu过去5分钟内的CPU使用速率(查询的数据为双精度浮点性)
irate(node_cpu_seconds_total{cpu=”0″,mode=”idle”}[5m])
##第一个cpu的平均使用率
1- avg(irate(node_cpu_seconds_total{cpu=”0″,mode=”idle”}[5m]))
案例: 监控远程linux主机
首先在远程linux上安装node_export组件,帮助prometheus来收集数据
从官网下载 node_exporter-1.3.1.linux-amd64.tar.gz

1. 上传并解压安装
[root@itlaoxin72 ~]# ls \ 公共 文档 anaconda-ks.cfg 模板 下载 initial-setup-ks.cfg 视频 音乐 node_exporter-1.3.1.linux-amd64.tar.gz 图片 桌面 [root@itlaoxin72 ~]# tar xf node_exporter-1.3.1.linux-amd64.tar.gz [root@itlaoxin72 ~]# mv node_exporter-1.3.1.linux-amd64 node_exporter [root@itlaoxin72 ~]# pwd /root [root@itlaoxin72 ~]# cd node_exporter/ [root@itlaoxin72 node_exporter]# ls LICENSE node_exporter NOTICE [root@itlaoxin72 node_exporter]#
2. 启动node_exporter来收集数据
[
[root@itlaoxin72 node_exporter]# ll -h 总用量 18M -rw-r--r--. 1 3434 3434 12K 12月 5 19:15 LICENSE -rwxr-xr-x. 1 3434 3434 18M 12月 5 19:10 node_exporter -rw-r--r--. 1 3434 3434 463 12月 5 19:15 NOTICE [root@itlaoxin72 node_exporter]# chmod +x node_exporter [root@itlaoxin72 node_exporter]# nohup ./node_exporter &
此处用nohup让程序一直运行下去,且在后台运行 备注: 关于nohup
nohup 是 Linux 的一个常用命令,当你想要在退出账户或者关闭终端后进程仍在运行时,就可以使用 nohup 命令。nohup 就是不挂断的意思(no hang up)。
3. 查看9100端口的占用情况
[root@xinsz08-20 ~]# lsof -i:9100 [root@itlaoxin72 ~]# lsof -i:9100 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME node_expo 6231 root 3u IPv6 61353 0t0 TCP *:jetdirect (LISTEN)
4. 查看监控的信息
http://192.168.1.18:9100/metrics
5. 配置prometheus server端可以拉取node信息
[root@itlaoxin-17 prometheus-2.26.0.linux-amd64]# vim prometheus.yml
/root/prometheus-2.16.0.linux-amd64/prometheus.yml
在文件最后添加内容
- job_name: 'agent'
static_configs:
- targets: ['192.168.1.18:9100']
添加完成后重新启动prometheus
[root@zmedu-17 prometheus-2.16.0.linux-amd64]# pkill prometheus
[root@zmedu-17 prometheus-2.16.0.linux-amd64]# ss -nltup | grep 9090
[root@zmedu-17 prometheus-2.16.0.linux-amd64]# ./prometheus &
[root@zmedu-17 prometheus-2.16.0.linux-amd64]# ss -naltp |grep 9090
LISTEN 0 128 [::]:9090 [::]:* users:(("prometheus",pid=83351,fd=8))
6. 通过web端查看,是否成功
可以看到增加了一台监控的信息


prometheus监控MySQL
熟悉了上面的两个部分后,我们进入今天的服务监控,以mariadb为例:
监控mysql需要在被监控机器安装mysql_exporter
mysql_exporter下载地址:https://prometheus.io/download/

1. 上传解压安装mysqld_exporter
[root@xinsz08-20 ~]# tar xf mysqld_exporter-0.12.1.linux-amd64.tar.gz 文件名字太长,重命名一下: [root@xinsz08-20 ~]# mv mysqld_exporter-0.12.1.linux-amd64 mysqld_exporter [root@xinsz08-20 ~]# cd mysqld_exporter/ [root@xinsz08-20 mysqld_exporter]# pwd /root/mysqld_exporter
2. 安装mariadb
[root@xinsz08-20 mysqld_exporter]# yum install mariadb\* -y
[root@xinsz08-20 ~]# systemctl start mariadb
[root@xinsz08-20 ~]# systemctl enable mariadb
[root@xinsz08-20 ~]# ss -natlp |grep 3306 //查看端口是否开启
LISTEN 0 80 0.0.0.0:3306 0.0.0.0:* users:(("mysqld",pid=67872,fd=22))
3. 在数据库里创建mysql账号用户收集数据
[root@xinsz08-20 ~]# mysql
MariaDB [(none)]> grant select,replication client,process ON *.* to 'mysql_monitor'@'localhost' identified by '12345678';
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> exit
Bye
[root@xinsz08-20 ~]#
备注: 此处是prometheus服务器来找mysql_exporter,然后mysql_exporter 在找mariadb,所以这里用的是localhost,指的是mysql_exporter的ip
4. 在mysql_exporter中设置mysql配置信息
[root@xinsz08-20 mysqld_exporter]# pwd
/root/mysqld_exporter
[root@xinsz08-20 mysqld_exporter]# vim .my.cnf (手工创建)
[client]
user=mysql_monitor
password=12345678
5. 启动mysql_exporter
[root@xinsz08-20 mysqld_exporter]# nohup ./mysqld_exporter --config.my-cnf=/root/mysqld_exporter/.my.cnf &
[1] 68166
注意; 此处是 --config.my-cnf=
查看端口(9104)
[root@xinsz08-20 ~]# ss -naltp |grep 9104
LISTEN 0 128 *:9104 *:* users:(("mysqld_exporter",pid=68249,fd=3))
6. 配置prometheus拉取mysql节点信息
在prometheus的server端进行修改
[root@zmedu-17 prometheus-2.16.0.linux-amd64]# pwd
/root/prometheus-2.16.0.linux-amd64
[root@zmedu-17 prometheus-2.16.0.linux-amd64]# vim prometheus.yml
- job_name: 'mariadb'
static_configs:
- targets: ['192.168.1.20:9104']
重启prometheus服务
[root@zmedu-17 prometheus-2.16.0.linux-amd64]# pkill prometheus
[root@zmedu-17 prometheus-2.16.0.linux-amd64]# lsof -i:9090
[root@zmedu-17 prometheus-2.16.0.linux-amd64]# ./prometheus &
[1] 84662
[root@zmedu-17 ~]# ss -naltp |grep 9090
LISTEN 0 128 [::]:9090 [::]:* users:(("prometheus",pid=84662,fd=9))
7. 通过web端查看
http://192.168.1.17:9090 查看mysql的连接数,看到只有一个连接数: mysql_global_status_aria_pagecache_blocks_unused
8. prometheus.yml配置文件详解
global:
# 全局配置
scrape_interval: 15s # 多久收集一次数据
evaluation_interval: 15s # 多久评估一次规则
scrape_timeout: 10s # 每次收集数据的超时时间
# Alertmanager configuration #告警设置
alerting:
alertmanagers:
- static_configs: #告警规则,也可以基于动态方式进行告警
- targets:
rule_files: #规则文件
#收集数据配置列表
scrape_configs:
- job_name: 'prometheus'