CentOS7下简单搭建Prometheus+Grafana监控系统

白昼怎懂夜的黑 提交于 2021-02-08 01:46:06

Prometheus is an open-source systems monitoring and alerting toolkit originally built at SoundCloud.

1、Features

Prometheus's main features are:
  • a multi-dimensional data model with time series data identified by metric name and key/value pairs
  • PromQL, a flexible query language to leverage this dimensionality
  • no reliance on distributed storage; single server nodes are autonomous
  • time series collection happens via a pull model over HTTP
  • pushing time series is supported via an intermediary gateway
  • targets are discovered via service discovery or static configuration
  • multiple modes of graphing and dashboarding support

2、Components

The Prometheus ecosystem consists of multiple components, many of which are optional:
  • the main Prometheus server which scrapes and stores time series data
  • client libraries for instrumenting application code
  • a push gateway for supporting short-lived jobs
  • special-purpose exporters for services like HAProxy, StatsD, Graphite, etc.
  • an alertmanager to handle alerts
  • various support tools
Most Prometheus components are written in Go, making them easy to build and deploy as static binaries.

3、Architecture

This diagram illustrates the architecture of Prometheus and some of its ecosystem components:
Prometheus scrapes metrics from instrumented jobs, either directly or via an intermediary push gateway for short-lived jobs. It stores all scraped samples locally and runs rules over this data to either aggregate and record new time series from existing data or generate alerts. Grafana or other API consumers can be used to visualize the collected data.

下面介绍CentOS7下简单搭建Prometheus+Grafana监控系统
1、先官网下载安装包
https://prometheus.io/download/
1)prometheus-2.17.2.linux-amd64.tar.gz
2)node_exporter-0.18.1.linux-amd64.tar.gz
grafana官网下载:https://grafana.com/grafana/download
3)https://dl.grafana.com/oss/release/grafana-6.7.3-1.x86_64.rpm
上传安装包到CentOS7服务器上
2、安装并配置prometheus服务端
groupadd prometheus
useradd -g prometheus -m -d /opt/prometheus/ -s /sbin/nologin prometheus
tar -zxf /data/prometheus-2.17.2.linux-amd64.tar.gz -C /opt
cd /opt/
mv prometheus-2.17.2.linux-amd64/* prometheus
cd prometheus
chown -R prometheus *
prometheus配置文件语法校验方法
./promtool check config prometheus.yml
先采用默认配置启动Prometheus Server
登录Prometheus Server的Web界面http://192.168.31.80:9090可以看到只有一个Targets:http://127.0.0.1:9090/metrics
设置Prometheus为系统服务,并配置为开机自启动
touch /usr/lib/systemd/system/prometheus.service
chown prometheus:prometheus /usr/lib/systemd/system/prometheus.service
vi /usr/lib/systemd/system/prometheus.service
并加入如下配置
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --web.enable-lifecycle --storage.tsdb.path=/opt/prometheus/data --storage.tsdb.retention=60d
Restart=on-failure
[Install]
WantedBy=multi-user.target
启动参数说明:
--config.file -- 指明prometheus的配置文件路径
--web.enable-lifecycle -- 指明prometheus配置更改后可以进行热加载
--storage.tsdb.path -- 指明监控数据存储路径
--storage.tsdb.retention --指明数据保留时间

启动服务,并设置为开机自启动
systemctl daemon-reload
systemctl enable prometheus.service
systemctl start prometheus.service 
systemctl status prometheus.service

可以看到已经报错了,服务未正常启动

排错
/opt/prometheus下data目录属主不对导致
解决办法:chown -R prometheus:prometheus *
3、安装node_exporter
比如就在该服务器上安装node_exporter
cd /data
tar -zxf node_exporter-0.18.1.linux-amd64.tar.gz -C /usr/local/
cd /usr/local/
mv node_exporter-0.18.1.linux-amd64 node_exporter
cd node_exporter/
ll -trh

创建系统服务
touch /usr/lib/systemd/system/node_exporter.service 
chown prometheus:prometheus /usr/lib/systemd/system/node_exporter.service 
chown -R prometheus:prometheus /usr/local/node_exporter* 
vi /usr/lib/systemd/system/node_exporter.service
加入如下行
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
启动node_exporter并设置为开启自启动
systemctl enable node_exporter.service
systemctl start node_exporter.service
systemctl status node_exporter.service
启动成功,测试API访问
http://192.168.31.80:9100/metrics
能正常返回数据说明node_exporter正常
并将其加入prometheus.yml配置文件中
  - job_name: 'CentOS7_VMServer'  
    static_configs:
    - targets: ['192.168.31.80:9100']
然后重启服务
 systemctl restart prometheus.service
这是再登录prometheus 的Web界面可以看到新增的这个Target
http://192.168.31.80:9090/targets
4、安装Grafana图形化工具包
缺少组件包的情况,使用
yum localinstall grafana-6.7.3-1.x86_64.rpm 
systemctl enable grafana-server.service
systemctl start grafana-server.service
systemctl status grafana-server.service
5、配置数据源并添加Dashboard
1)、登录grafana,默认端口3000,初始账号/密码:admin/admin
http://192.168.31.80:3000/
2)添加数据源
示例:Name CentOS7_VM_Prometheus
URL :http://192.168.31.80:9090
3)添加一个DashBoard,Import 例如8919 ID这个
效果如下(点击图片查看清晰大图)
总结:
  • Prometheus 属于一站式监控告警平台,依赖少,功能齐全。

  • Prometheus 支持对云或容器的监控,其他系统主要对主机监控。

  • Prometheus 数据查询语句表现力更强大,内置更强大的统计函数。

  • Prometheus 在数据存储扩展性以及持久性上没有 InfluxDB,OpenTSDB,Sensu 好

Prometheus vs Zabbix

  • Zabbix 使用的是 C 和 PHP, Prometheus 使用 Golang, 整体而言 Prometheus 运行速度更快一点。

  • Zabbix 属于传统主机监控,主要用于物理主机,交换机,网络等监控,Prometheus 不仅适用主机监控,还适用于 Cloud, SaaS, Openstack,Container 监控。

  • Zabbix 在传统主机监控方面,有更丰富的插件。

  • Zabbix 可以在 WebGui 中配置很多事情,但是 Prometheus 需要手动修改文件配置。

本文分享自微信公众号 - WalkingCloud(WalkingCloud2018)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!