Prometheus
概述
特征
- 一种多维数据模型,其中包含通过 metric 名称和键/值对标识的时间序列数据
- 可利用各种维度的灵活的查询语句
- 不依赖分布式存储;单服务节点是自治的
- 时间序列通过 HTTP 拉取方式进行收集
- 支持通过中间网关
- 通过服务发现或静态配置发现目标
- 多种图形和仪表板支持模式
组件
-
用于采集和存取时间序列数据的 Prometheus server
-
用于监测应用的客户端库
-
用于支持短期的作业的 push gateway
-
诸如 HAProxy,StatsD,Graphite 等服务的专用 exporter
-
用于处理告警的 alertmanager
-
多种工具支持
架构
- Prometheus 的体系结构及其部分生态系统组件:
-
说明
Prometheus 直接或通过 push gateway(主要用于短期作业)从已监测的作业中采集指标数据。它在本地存储所有已采集到的数据,并对这些数据运行规则,以汇总和记录新的时间序列,或产生告警。Grafana 或其它 API 可用于可视化收集的数据。
适合场景
-
Prometheus 非常适合记录纯数字时间序列的场景。它即适合以机器为中心的监控,也适合高度动态的面向服务的体系结构监控。在微服务中,它的优势在于多维数据收集和查询的支持。
-
Prometheus 的设计旨在提高可靠性,在故障发生时能够快速定位问题。每个 Prometheus 服务器都是独立的,而不依赖于网络存储或其他远程服务。当其它基础设备发生故障时,您可以依靠它,并且无需昂贵的基础设备即可使用它
不合适场景
- Prometheus 重视可靠性,即使在故障情况下,您始终可以查看有关系统的可用统计信息。如果你需要 100% 的准确性,例如按请求计费,因为收集的数据可能不够详细和完整,Prometheus 并不是一个好的选择。在这种场景下,您最好使用其他系统来收集和分析数据以进行计费,使用 Prometheus 进行其余的监视。
下载安装Prometheus
环境准备
-
CentOS7,JDK8
-
关闭防火墙
#关闭命令 service firewalld stop chkconfig firewalld off
-
关闭selinux 配置文件:/etc/selinux/config
#SELINUX=enforcing SELINUX=disabled
下载解压Prometheus
-
下载地址
https://prometheus.io/download/
-
在/路径下建立文件夹prometheus 并进入目录进行解压
[root@localhost prometheus]# tar -zxvf prometheus-2.27.1.linux-amd64.tar.gz
-
重命名解压的目录为prometheus-2.27.1
[root@localhost prometheus]# mv prometheus-2.27.1.linux-amd64 prometheus-2.27.1
配置文件
-
Prometheus 的下载文件中包含一个名为
prometheus.yml
的示例配置,我们可以根据它来开始配置。global: scrape_interval: 15s evaluation_interval: 15s rule_files: # - "first.rules" # - "second.rules" scrape_configs: - job_name: prometheus static_configs: - targets: ['localhost:9090']
-
示例配置文件中包含三个配置块:
global
,rule_files
和scrape_configs
。
-global
配置块控制 Prometheus server 的全局配置。目前有两个参数。scrape_interval
参数控制 Prometheus 多久采集一次 targets。您可以为单个 targets 重新配置此参数。在示例中,全局配置为 15 秒采集一次。evaluation_interval
参数控制 Prometheus 多久评估一次 rules。Prometheus 使用 rules 来创建新的时间序列并生成报警。
rule_files
配置块指定我们希望 Prometheus server 加载规则的文件位置。目前,我们没有任何规则。
scrape_configs
配置块控制 Prometheus 监控哪些资源。由于 Prometheus 将有关自身的数据暴露为 HTTP 端点,因此它可以采集并监控其自身的运行状况。在默认配置中,只有一个名为prometheus
的作业,它会采集 Prometheus 服务暴露的时间序列数据。该作业包含一个单独的静态配置的 target,为localhost
,9090
端口。Prometheus 希望数据 指标可以通过 target 的/metrics
路径可以访问。所以这个默认作业通过http://localhost:9090/metrics
进行指标数据采集。
启动Prometheus
[root@localhost prometheus-2.27.1]# ./prometheus --config.file=prometheus.yml --web.enable-lifecycle
- 启动后访问http://192.168.237.128:9090/ 看到有关其自身状态的页面。它需要大约 30 秒的时间从自己的 HTTP 指标数据端点收集有关自身的数据。
- 也可以通过访问 Prometheus 自身的指标数据端点http://192.168.237.128:9090/metrics来验证Prometheus 是否正在提供有关其自身数据的指标数据。
关闭Prometheus
使用kill关闭
- 使用
pgrep -f prometheus
找到运行的 Prometheus 进程号 - 使用 kill -TERM+进程号 命令来关闭 或者kill -9 进程号
使用HTTP端口
-
Prometheus 提供了 HTTP 关闭接口,但在使用之前,需要通过 --web.enable-lifecycle 参数开启 lifecycle 功能,(即需要在启动 Prometheus 的时候,添加
--web.enable-lifecycle
)然后就可以使用 HTTP 请求来关闭程序了[root@localhost prometheus-2.27.1]# ./prometheus --config.file=prometheus.yml --web.enable-lifecycle
例如:curl -X POST http://localhost:9090/-/quit
此时 HTTP 接口会返回:Requesting termination… Goodbye![root@localhost ~]# curl -X POST http://localhost:9090/-/quit Requesting termination... Goodbye!
使用 Node Exporter 监控 Linux 主机指标
下载并启动Node Exporter
-
在https://prometheus.io/download/进行下载对应的node_exporter-1.1.2.linux-amd64.tar.gz文件
-
将其放入/prometheus路径下并进行解压
[root@localhost prometheus]# tar -zxvf node_exporter-1.1.2.linux-amd64.tar.gz
-
进入到解压后的文件路径下,进行启动Node Exporter命令:./node_exporter
[root@localhost prometheus]# cd node_exporter-1.1.2/ [root@localhost node_exporter-1.1.2]# ls LICENSE node_exporter NOTICE [root@localhost node_exporter-1.1.2]# ./node_exporter
- 查看启动是否成功,浏览器打开如:http://192.168.237.128:9100
使用Prometheus集成Node Exporter
- 修改prometheus的prometheus.yml文件,添加如下:
- job_name: 'nodes'
static_configs:
- targets:
- 192.168.237.128:9100
-
启动Prometheus服务
[root@localhost prometheus-2.27.1]# ./prometheus --config.file=prometheus.yml --web.enable-lifecycle
-
验证 http://192.168.237.128:9090/targets
- 如上图所示,即Prometheus将Node Exporter的指标集成起来进行了监控。
安装并启动Grafana
下载Grafana的rpm包
- https://mirrors.bfsu.edu.cn/grafana/yum/rpm/,这里下载版本为 grafana-7.3.7-1.x86_64.rpm
安装Grafana
-
将下载的rpm包放入到如下路径 /usr/local/src,并进行安装
[root@localhost src]# yum install grafana-7.3.7-1.x86_64.rpm
-
yum将其安装到路径为 /etc/grafana/
[root@localhost src]# cd /etc/grafana/ [root@localhost grafana]# ls grafana.ini ldap.toml provisioning
-
启动grafana
[root@localhost grafana]# systemctl start grafana-server
-
如启动失败如下
-
查看启动grafana日志
tail /var/log/grafana/grafana.log
-
日志如下图
-
可在日志的/var/run/路径下建立文件路径grafana,然后执行授权命令
[root@localhost grafana]# sudo chown -R grafana:grafana /var/run/grafana
-
重新启动Grafana命令即可
[root@localhost grafana]# systemctl start grafana-server
-
验证Granfana是否启动成功,可执行指令看3000端口是否开启
[root@localhost grafana]# ss -tnl
-
浏览器打开如:http://192.168.237.128:3000/ 默认用户密码均为admin
- 提示修改密码
将Grafana对接到Prometheus
- 登录后,按如下所示,进入DataSources
-
点击添加Add DataSources按钮
-
选择Prometheus
- 录入Prometheus的URL如:http://192.168.237.128:9090,点击最下方的Save&Test
进入到之前保存成功的数据源面板下
- 将这三项导入
- 进入Dashboards进行查看
- 进入 查看