Prometheus体系
Prometheus + NodeExporter + Grafana + AlertManager
本文介绍 Prometheus + NodeExporter + Grafana + AlertManager 的安装,监控宿主机CPU与内存等情况
安装Prometheus
官网给出了很多种安装方法(https://prometheus.io/docs/prometheus/latest/installation/),最常用的就是二进制与docker镜像,这里我们使用已经编译好的二进制进行安装。
从官网(https://prometheus.io/download/)
下载最新版本(2.15.2)的二进制包,解压,然后查看prometheus版本信息
$ wget https://github.com/prometheus/prometheus/releases/download/v2.15.2/prometheus-2.15.2.linux-amd64.tar.gz
$ tar xzvf prometheus-2.15.2.linux-amd64.tar.gz
$ cd prometheus-2.15.2.linux-amd64
$ ./prometheus --version当使用二进制进行安装时,最好用systemd来管理。我们把整个prometheus-2.15.2.linux-amd64文件夹移动到/usr/local/目录下,并重命名为prometheus
$ mv ./prometheus-2.15.2.linux-amd64 /usr/local/prometheus然后创建文件/usr/lib/systemd/system/prometheus.service,内容如下
[Unit]
Description=prometheus
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus
Restart=on-failure
[Install]
WantedBy=multi-user.target关于启动参数可以使用命令/usr/local/prometheus/prometheus --help查看。这里我们把数据的存储目录指定到/var/lib/prometheus
然后启动prometheus,并查看prometheus是否启动成功
如果启动失败,则可以通过journalctl -xeu prometheus --no-pager查看错误日志(因为systemd管理的service的stdout日志都由journald接管)。如果成功,此时我们可以通过http://ip:9090访问prometheus自带的UI

安装NodeExporter
从官网(https://prometheus.io/download/#node_exporter)下载最新版本(0.18.1)的二进制的NodeExporter,解压
同样,我们使用systemd来管理这个服务。我们把解压后的整个node_exporter-0.18.1.linux-amd64文件夹移动到/usr/local/目录下,并重命名为node_exportor
然后创建文件/usr/lib/systemd/system/node_exporter.service,内容如下
关于启动参数可以使用命令/usr/local/node_exporter/node_exporter --help查看。
然后启动node_exporter,并查看是否启动成功
配置Prometheus抓取NodeExporter的数据
当node_exporter启动成功后,我们便可以配置prometheus,去抓取node_exporter的数据。node_exporter默认的端口是9100。
一开始的时候,prometheus只有一个抓取对象,就是抓取自已的数据。即一开始时,/usr/local/prometheus/prometheus.yml的scrape_configs:的内容如下:
修改配置文件/usr/local/prometheus/prometheus.yml,加入新的job,抓取node_exporter的数据,添加完后scrape_configs:的完整内容如下:
然后重启prometheus
然后,我们去到prometheus的Status -> Targets,就可以看到上面的两个target,均为UP状态

接下来,我们通过Prometheus来查看一下主机的内存使用率,输入以下查询语句然后就可以看到内存使用率的曲线图(注意:其实在截这个图时,我们的node-exporter与prometheus已经运行了很长一段时间,如果你的运行时间不长,看到的曲线会比较少)

安装Grafana
我们使用rpm包来安装,见官方教程(https://grafana.com/docs/installation/rpm/)。首先下载rpm包,然后安装
安装后grafana的相关信息如下:
环境变量文件:
/etc/sysconfig/grafana-server配置文件:
/etc/grafana/grafana.ini数据库文件:
/var/lib/grafana/grafana.db日志目录:
/var/log/grafana
启动Grafana
默认情况下grafana会监听localhost:3000,用户名与密码为admin/admin。
接下来,我们为grafana配置数据源。详见教程https://grafana.com/docs/features/datasources/prometheus/。
安装AlertManager
github上给了AlertManager的几种安装方法:(https://github.com/prometheus/alertmanager)。
这里,我们使用二进制进行安装。首先下载二进制文件,这里我们下载最新版本0.20.0(https://prometheus.io/download/#alertmanager)
同样,我们使用systemd来管理这个服务。我们把解压后的整个alertmanager-0.20.0.linux-amd64文件夹移动到/usr/local/目录下,并重命名为alertmanager
然后创建文件/usr/lib/systemd/system/alertmanager.service,内容如下
编辑/usr/local/alertmanager/alertmanager.yml,内容如下(参考【5】【6】:
启动alertmanager
为Prometheus配置告警规则
编辑/usr/local/prometheus/prometheus.yml文件,在rule_files区域添加如下内容:
然后创建文件/usr/local/prometheus/rule_files/memory_alert.yml,内容如下
不久后,便可以接收到告警邮件,如下

Reference
【1】https://prometheus.io/docs/prometheus/latest/installation/ 【2】 https://www.cnblogs.com/yanyouqiang/p/7240696.html 【3】https://grafana.com/docs/installation/rpm/ 【4】https://grafana.com/docs/features/datasources/prometheus/ 【5】https://www.cnblogs.com/longcnblogs/p/9620733.html 【6】 https://github.com/prometheus/alertmanager/blob/master/doc/examples/simple.yml
Last updated
Was this helpful?