【实践】6.Prometheus-exporter原理和监控MySQL,Redis,主机实践

1. 摘要

本文介绍Prometheus的Exporter组件的作用,原理,已经使用该组件对主机,MySQL,Redis等实施监控的实践。

2. Exporter原理

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等。

2.1 Exporter分类

2.1.1 社区提供的

2.1.2 用户自定义的

2.2 Exporter获取监控数据的方式

Exporter 主要通过被监控对象提供的监控相关的接口获取监控数据,主要有如下几种方式:
(1)HTTP/HTTPS方式。例如 RabbitMQ exporter通过 RabbitMQ的 HTTPS接口获取监控数据。
(2)TCP方式。例如Redis exporter通过Redis提供的系统监控相关命令获取监控指标,MySQL server exporter通过MySQL开放的监控相关的表获取监控指标。
(3)本地文件方式。例如Node exporter通过读取proc文件系统下的文件,计算得出整个操作系统的状态。
(4)标准协议方式。

2.3 Exporter规范

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 ]

2.3.1 GO自定义Exporter

详见: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

3. 集成主机节点监控-Node Exporter[已测试]

3.1 Node Exporter 安装及运行

在一台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可测试。

3.2 Prometheus 配置

在 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状态。

4. 集成Mysql服务器性能监控- mysqld_exporter[已测试]

4.1 Node Exporter 安装及运行

(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"   &

4.2 Prometheus 配置更新

在 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状态。

5. 集成REDIS监控- redis_exporter

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、 查看采集到的数据

image

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/

6. 参考

(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/

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,200评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,526评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,321评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,601评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,446评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,345评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,753评论 3 387
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,405评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,712评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,743评论 2 314
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,529评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,369评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,770评论 3 300
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,026评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,301评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,732评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,927评论 2 336

推荐阅读更多精彩内容