Prometheus And Grafana 2

Page content

0. Intro

Prometheus 是基于 pull 模式的监控系统,需要在被监控处设置各种 Exporter,通过获取输出的信息,Prometheus 存储到对应的 index 中。

1. 示例:从 Node Exporter 收集监控数据

官方提供了一些 exporter,为一些基础指标提供监控,还有更多第三方可以下载,监控更多应用。

这里以监控服务器节点资源的 exporter 为例,介绍 Prometheus/exporter/Grafana 之间是如何收集、处理、展示监控信息的。

1.1. 下载安装 node_exporter

1$ export VERSION=0.18.1
2$ curl -LO https://github.com/prometheus/node_exporter/releases/download/v$VERSION/node_exporter-$VERSION.linux-amd64.tar.gz
3$ tar -zxf node_exporter-$VERSION.linux-amd64.tar.gz
4
5$ cd node_exporter-$VERSION.linux-amd64
6$ cp node_exporter /usr/bin/node_exporter

设置为开机启动

1$ sudo vim /usr/lib/systemd/system/node_exporter.service
 1[Unit]
 2Description=Prometheus Node Exporter
 3After=network.target
 4
 5[Service]
 6ExecStart=/usr/bin/node_exporter
 7User=nobody
 8
 9[Install]
10WantedBy=multi-user.target
1$ sudo systemctl enable node_exporter
2$ sudo systemctl start node_exporter

访问 9100 端口的 /metrics ,可以看到输出的 metrics 信息:

 1# HELP go_gc_duration_seconds A summary of the GC invocation durations.
 2# TYPE go_gc_duration_seconds summary
 3go_gc_duration_seconds{quantile="0"} 1.1492e-05
 4go_gc_duration_seconds{quantile="0.25"} 1.8635e-05
 5go_gc_duration_seconds{quantile="0.5"} 6.4224e-05
 6go_gc_duration_seconds{quantile="0.75"} 8.3151e-05
 7go_gc_duration_seconds{quantile="1"} 0.000217303
 8go_gc_duration_seconds_sum 7.794444601
 9go_gc_duration_seconds_count 124123
10# HELP go_goroutines Number of goroutines that currently exist.
11# TYPE go_goroutines gauge
12go_goroutines 8
13# HELP go_info Information about the Go environment.
14# TYPE go_info gauge
15go_info{version="go1.12.5"} 1
16# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
17# TYPE go_memstats_alloc_bytes gauge
18go_memstats_alloc_bytes 2.347544e+06
19# HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.
20# TYPE go_memstats_alloc_bytes_total counter
21go_memstats_alloc_bytes_total 2.62430469184e+11
22# HELP go_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table.
23# TYPE go_memstats_buck_hash_sys_bytes gauge
24go_memstats_buck_hash_sys_bytes 1.944633e+06
25# HELP go_memstats_frees_total Total number of frees.
26# TYPE go_memstats_frees_total counter
27go_memstats_frees_total 2.741147498e+09
28
29...

1.2. 在 Prometheus 中添加其引用

修改 prometheus.yml 文件,在 scrape_configs 节点下添加新的 job:

1scrape_configs:
2  # node_exporter configs
3  - job_name: 'node_exporter'
4    static_configs:
5    - targets:
6      - 127.0.0.1:9100

保存,重启 Prometheus:

1$ sudo systemctl restart prometheus

1.3. 在 Grafana 中配置图表

在 Grafana Labs 上有很多官方、非官方的 dashboard。node_exporter 对应的中文版 dashboard,非常全面,直接复制右边的 ID,在我们自己的 Grafana 中导入即可。

import-dashboard-1

选择数据源为 Prometheus。

import-dashboard-2

这是运行了一段时间后的 dashboard,包含 4 个 node:

node-exporter-grafana-dashboard-view