【實踐】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
http://www.lxweimin.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/

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容