本文介绍Prometheus的Exporter组件的作用,原理,已经使用该组件对主机,MySQL,Redis等实施监控的实践。
Prometheus 已经成为云原生应用监控行业的标准,在很多流行的监控系统中都已经实现了 Prometheus的监控接口,例如 etcd、Kubernetes、CoreDNS等,它们可以直接被Prometheus监控,但大多数监控对象都没办法直接提供监控接口,主要原因有: (1)很多系统在Prometheus诞生前的很多年就已发布,例如MySQL、Redis等; (2)它们本身不支持 HTTP 接口,例如对于硬件性能指标,操作系统并没有原生的HTTP接口可以获取; (3)考虑到安全性、稳定性及代码耦合等因素的影响,软件作者并不愿意将监控代码加入现有代码中。 这些都导致无法通过一个规范解决所有监控问题。在此背景之下,Exporter 应运而生。Exporter 是一个采集监控数据并通过 Prometheus 监控规范对外提供数据的组件。除了官方实现的Exporter如Node Exporter、HAProxy Exporter、MySQLserver Exporter,还有很多第三方实现如Redis Exporter和RabbitMQ Exporter等。
Exporter 主要通过被监控对象提供的监控相关的接口获取监控数据,主要有如下几种方式: (1)HTTP/HTTPS方式。例如 RabbitMQ exporter通过 RabbitMQ的 HTTPS接口获取监控数据。 (2)TCP方式。例如Redis exporter通过Redis提供的系统监控相关命令获取监控指标,MySQL server exporter通过MySQL开放的监控相关的表获取监控指标。 (3)本地文件方式。例如Node exporter通过读取proc文件系统下的文件,计算得出整个操作系统的状态。 (4)标准协议方式。
Prometheus 在面对众多繁杂的监控对象时并没有采用逐一适配的方式,而是制定了一套独特的监控数据规范,符合这套规范的监控数据都可以被Prometheus统一采集、分析和展现。
所有的Exporter程序都需要按照Prometheus的规范,返回监控的样本数据。以Node Exporter为例,当访问/metrics地址时会返回以下内容:
# HELP node_cpu Seconds the cpus spent in each mode.
# TYPE node_cpu counter
node_cpu{cpu="cpu0",mode="idle"} 362812.7890625
# HELP node_load1 1m load average.
# TYPE node_load1 gauge
node_load1 3.0703125
Exporter返回的样本数据,主要由三个部分组成:样本的一般注释信息(HELP),样本的类型注释信息(TYPE)和样本。Prometheus会对Exporter响应的内容逐行解析:
如果当前行以# HELP开始,Prometheus将会按照以下规则对内容进行解析,得到当前的指标名称以及相应的说明信息:# HELP <metrics_name> <doc_string> 如果当前行以# TYPE开始,Prometheus会按照以下规则对内容进行解析,得到当前的指标名称以及指标类型: # TYPE <metrics_name> <metrics_type> 除了# 开头的所有行都会被视为是监控样本数据。 每一行样本需要满足以下格式规范:
metric_name [
"{" label_name "=" `"` label_value `"` { "," label_name "=" `"` label_value `"` } [ "," ] "}"
] value [ timestamp ]
详见:https://github.com/prometheus/client_golang/blob/master/examples/random/main.go
rpcDurations = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Name: "rpc_durations_seconds",
Help: "RPC latency distributions.",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
},
[]string{"service"},
)
prometheus.MustRegister(rpcDurations)
go func() {
for {
v := rand.Float64() * *uniformDomain
rpcDurations.WithLabelValues("uniform").Observe(v)
time.Sleep(time.Duration(100*oscillationFactor()) * time.Millisecond)
}
}()
http.Handle("/metrics", promhttp.HandlerFor(
prometheus.DefaultGatherer,
promhttp.HandlerOpts{
// Opt into OpenMetrics to support exemplars.
EnableOpenMetrics: true,
},
))
log.Fatal(http.ListenAndServe(*addr, nil))
# HELP rpc_durations_seconds RPC latency distributions.
# TYPE rpc_durations_seconds summary
rpc_durations_seconds{service="uniform",quantile="0.5"} 4.2852774516474985e-05
rpc_durations_seconds{service="uniform",quantile="0.9"} 0.00012093205759592392
rpc_durations_seconds{service="uniform",quantile="0.99"} 0.00012093205759592392
rpc_durations_seconds_sum{service="uniform"} 0.0002537090545263203
rpc_durations_seconds_count{service="uniform"} 4
在一台Ubuntu Linux 机器上安装并运行 Node Exporter。
V1.1.2版本下载地址:https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz
下载并解压。Linux wget下载不成功的话,可以浏览器直接下载后存放上去。
cd /home/datadisk
wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz
tar zxvf node_exporter-1.1.2.linux-amd64.tar.gz
进入 node_exporter-1.1.2.linux-amd64
文件夹,后台启动node_exporter:
nohup ./node_exporter &
请确认默认的9100端口是否在安全组中打开,采用psping可测试。
在 prometheus.yml 中配置 node_exporter 的metrics 端点,内容如下:
global:
scrape_interval: 5s
evaluation_interval: 5s
scrape_timeout: 5s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'huige_test'
static_configs:
- targets: ['114.67.107.227:9100'] #ip需要改为自己的IP
启动 prometheus:
docker run -d -p 9090:9090 --name prometheus2021 -v /root/arta/NODE0/prometheus_monitor/prometheus.yml:/etc/prometheus/prometheus.yml -v /root/arta/NODE0/prometheus_monitor/data:/prometheus prom/prometheus
访问 http://114.67.87.227:9090/targets#job-huige_node_test发现已经出现了 target “huige_node_test” ,并且为UP状态。
(1) V-0.12.1版本下载地址: https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
下载并解压。Linux wget下载不成功的话,可以浏览器直接下载后存放上去。
cd /home/datadisk
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
tar zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz
(2) 分配mysqld_exporter访问MySQL数据库的账号和权限 mysqld_exporter需要连接到Mysql,所以需要Mysql的权限,我们先为它创建用户并赋予所需的权限,密码自行修改。
mysql> GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'prometheus'@'localhost' identified by 'prometheus2021';
mysql> GRANT SELECT ON performance_schema.* TO 'prometheus'@'localhost';
mysql> FLUSH PRIVILEGES;
(3)启动 mysqld_exporter 进入 mysqld_exporter-0.12.1.linux-amd64 文件夹 新增并编辑配置文件.my.cnf
[client]
user=prometheus
password=prometheus2021
后台启动程序, 默认监听9104端口,要netstat确保端口未被占用。开启后,记得安全组或者防火墙打开9104端口。
nohup ./mysqld_exporter --config.my-cnf=".my.cnf" &
在 prometheus.yml 中配置 node_exporter 的metrics 端点,内容如下:
global:
scrape_interval: 5s
evaluation_interval: 5s
scrape_timeout: 5s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'huige_test'
static_configs:
- targets: ['114.67.107.227:9100'] #ip需要改为自己的IP
- job_name: 'huige_mysql_test'
static_configs:
- targets: ['114.67.87.227:9104'] #ip需要改为自己的IP
重新启动 prometheus:
docker restart prometheus2021
访问 http://114.67.87.226:9090/targets#job-huige_mysql_test发现已经出现了 target “job-huige_mysql_test” ,并且为UP状态。
1、下载 redis exporter
// 下载地址:https://github.com/oliver006/redis_exporter/releases
# ls redis_exporter-v1.0.0.linux-amd64.tar.gz
redis_exporter-v1.0.0.linux-amd64.tar.gz
# tar xvf redis_exporter-v1.0.0.linux-amd64.tar.gz -C /usr/local/
2、配置 redis_exporter 2.1 添加账号授权给 redis exporter,以便 redis_exporter 能够连接到 redis server
2.2 配置 redis_exporter service 文件
# vim /etc/systemd/system/redis_exporter.service
[Unit]
Description=redis_exporter
After=network.target
[Service]
Restart=on-failure
ExecStart=/usr/local/redis_exporter-v1.0.0.linux-amd64/redis_exporter -redis.addr 192.168.22.33:6379 -redis.password 123456
[Install]
WantedBy=multi-user.target
# systemctl daemon-reload
# systemctl start redis_exporter
# systemctl status redis_exporter
-redis.addr 192.168.22.33:6379 -redis.password 123456 --> 配置 redis 连接信息
3、 查看采集到的数据
4、添加 prometheus 监控
# vim prometheus.yml
- file_sd_configs:
- files:
- 'configs/redis.yml'
job_name: Redis
metrics_path: /metrics
relabel_configs:
- source_labels: [__address__]
regex: (.*)
target_label: instance
replacement: $1
- source_labels: [__address__]
regex: (.*)
target_label: __address__
replacement: $1:9121
# vim configs/redis.yml
- labels:
service: redis_192.168.22.33
targets:
- 192.168.22.11
其他更多exporter配置,参考官网https://prometheus.io/docs/instrumenting/exporters/
(1)Prometheus系列--Exporter原理 https://zhuanlan.zhihu.com/p/273229856 (2)Prometheus 集成 Node Exporter https://zhuanlan.zhihu.com/p/78290435 (3) Prometheus Exporter 监控 Redis[K8S] https://zhuanlan.zhihu.com/p/70091205 Prometheus 监控 Redis https://www.jianshu.com/p/fffaaff05001 (4) 第04期:Prometheus 数据采集(三) https://zhuanlan.zhihu.com/p/166557763 (5) Prometheus + Granafa 构建高大上的MySQL监控平台【MySQL主备】 https://didispace-wx.blog.csdn.net/article/details/111828879 使用Prometheus和Grafana监控Mysql服务器性能 https://segmentfault.com/a/1190000007040144 (6) 官网各种exporters列表 https://prometheus.io/docs/instrumenting/exporters/