一、Prometheus介绍
1.1 Prometheus概述
Prometheus(普罗米修斯)是一个最初在SoundCloud上构建的监控系统。自2012年成为社区开源项目,
拥有非常活跃的开发人员和用户社区。为强调开源及独立维护,Prometheus于2016年加入云原生云计算基金会(CNCF),成为继Kubernetes之后的第二个托管项目;原生支持K8s和docker。
https://prometheus.io
https://github.com/prometheus
1.2 Prometheus 特点
• 多维数据模型:由度量名称和键值对标识的时间序列数据(数据模型包含指标名称和多个标签;可以通过指定的标签,查询指定的数据)
• PromQL:一种灵活的查询语言,可以利用多维数据完成复杂的查询
• 不依赖分布式存储,单个服务器节点可直接工作
• 基于HTTP的pull方式采集时间序列数据(从被管理节点获取这些监控指标进行存储,分析)
• 推送时间序列数据通过PushGateway组件支持(适用于于定时任务)
• 通过服务发现或静态配置发现目标
• 多种图形模式及仪表盘支持(grafana)
1.3 Prometheus组件与架构
• Prometheus Server:收集指标和存储时间序列数据,并提供查询接口
• ClientLibrary:客户端库(提供一些语言库,可以集成到项目里面;能方便暴露采集的指标)
• Push Gateway:短期存储指标数据。主要用于临时性的任务
• Exporters:采集已有的第三方服务监控指标并暴露metrics
• Alertmanager:告警
• Web UI:简单的Web控制台
二、Prometheus 部署
• 二进制部署
https://prometheus.io/docs/prometheus/latest/getting_started/
• Docker部署
https://prometheus.io/docs/prometheus/latest/installation/
•访问方式
访问地址:http://IP:9090
2.1 prometheus二进制部署
解压安装包
[root@prometheus ~]# tar zxf prometheus-2.27.1.linux-amd64.tar.gz
启动
[root@prometheus prometheus-2.27.1.linux-amd64]# ./prometheus
访问
http://192.168.66.100:9090
2.2 premetheus -h 命令行常用参数
• --config.file=“prometheus.yml” # 指定配置文件
• --web.listen-address= “0.0.0.0:9090” # 监听地址和端口
• --log.level=info # 日志级别
• --alertmanager.timeout=10s # 与报警组件的超时时间
• --storage.tsdb.path="data/ " # 数据目录
• --storage.tsdb.retention.time=15d # 数据保存时间,默认15天
2.3 配置为系统服务管理:
vi /usr/lib/systemd/system/prometheus.service
[Unit]
Description=prometheus
[Service]
ExecStart=/opt/monitor/prometheus/prometheus --config.file=/opt/monitor/prometheus/prometheus.yml
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process #只杀主进程
[Install]
WantedBy=multi-user.target
[root@localhost ~]# mkdir -p /opt/monitor
[root@localhost ~]# cd /opt/monitor/
[root@localhost monitor]# mv prometheus-2.27.1.linux-amd64/ prometheus
[root@localhost monitor]# systemctl daemon-reload
[root@localhost monitor]# systemctl start prometheus
[root@localhost monitor]# systemctl enable prometheus
2.4 prometheus 配置文件
[root@localhost prometheus]# vim prometheus.yml
• global:全局配置
scrape_interval: 15s # 采集数据时间间隔
evaluation_interval: 15s # 评估告警规则时间间隔,默认1分钟
scrape_timeout: 5s # 采集数据超时时间,默认10秒
• rule_files:告警规则
• scrape_configs:配置被监控端,称为target,每个target用
job_name分组管理,又分为静态配置和服务发现
• alerting:告警配置
• remote_write/remote_read:从远程数据库读写
2.5 配置被监控端
目标(targets):被监控端
实例(Instances):每个被监控端称为实例
作业(Job):具有相同目标的实例集合称为作业
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node'
static_configs:
- targets: ['192.168.1.10:9090']
2.6 监控指标数据模型
数据模型:
• Prometheus将所有数据存储为时间序列;
• 具有相同度量名称以及标签属于同一个指标;
• 每个时间序列都由度量标准名称和一组键值对(称为标签)唯一标识,
通过标签查询指定指标
指标格式:
<metric name> {
<label name>=<label value>,...}
三、监控案例
3.1 如何监控服务
如果要想监控,前提是能获取被监控端指标数据,并且这个
数据格式必须遵循Prometheus数据模型,这样才能识别和
采集,一般使用exporter提供监控指标数据。
1、自己写metrics接口,遵循数据模型。
1)先知道怎么收集你要监控指标。
2)集成官方的客户端或者自己数据格式,暴露出来。
2、使用社区维护的exporter(采集器,以数据模型暴露metrics接口)
exporter列表:
https://prometheus.io/docs/instrumenting/exporters
3.2 Grafana部署
Grafana是一个开源的度量分析和可视化系统。
部署文档:https://grafana.com/grafana/download
访问地址:http://IP:3000
用户名/密码:admin/admin # 第一次需要重置密码
[root@localhost monitor]# wget https://dl.grafana.com/enterprise/release/grafana-enterprise-8.4.4.linux-amd64.tar.gz
[root@localhost monitor]# tar zxf grafana-enterprise-8.4.4.linux-amd64.tar.gz
[root@localhost monitor]# mv grafana-8.4.4 grafana
配置为系统服务管理:
vim /usr/lib/systemd/system/grafana.service
[Unit]
Description=grafana
[Service]
ExecStart=/opt/monitor/grafana/bin/grafana-server -homepath=/opt/monitor/grafana
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start grafana
systemctl enable grafana
Grafana只用于展示数据,但这个数据从哪里来?
需要你根据提供数据的服务选择,支持的数据源如下:
选择Prometheus,只需要指定URL即可。
3.3 监控Linux服务器
node_exporter:用于监控Linux系统的指标采集器。
安装在被监控端的服务器,不是通过网络连接,而是通过读取本地文件系统收集的。
常用指标:
• CPU
• 内存
• 硬盘
• 网络流量
• 文件描述符
• 系统负载
• 系统服务
数据接口:http://IP:9100
使用文档:https://prometheus.io/docs/guides/node-exporter/
GitHub:https://github.com/prometheus/node_exporter
3.3.1 安装node_exporter
[root@node1 ~]# wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
[root@node1 ~]# tar zxf node_exporter-1.3.1.linux-amd64.tar.gz
[root@node1 ~]# mv node_exporter-1.3.1.linux-amd64 /usr/local/node_exporter
[root@node1 ~]# cd /usr/local/node_exporter/
[root@node1 node_exporter]# ./node_exporter
3.3.2 配置为系统服务管理
vi /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
[Service]
ExecStart=/usr/local/node_exporter/node_exporter --
web.config=/usr/local/node_exporter/config.yml
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start node_exporter
systemctl enable node_exporter
3.3.3 在Prometheus配置文件添加被监控端
[root@localhost prometheus]# vim prometheus.yml
static_configs:
- targets: ['localhost:9090']
- job_name: 'web-services'
static_configs:
- targets: ['192.168.66.101:9000','192.168.66.102:9100']
3.3.4 检查配置文件,并执行加载
[root@localhost prometheus]# ./promtool check config ./prometheus.yml
[root@localhost prometheus]# kill -HUP 21689
3.3.5 使用Grafana展示node_exporter数据指标
仪表盘ID: 9276
把以下两个网卡设备改为ens33
3.3.6 启用HTTP认证
生成密码
[root@node1 ~]# yum install httpd-tools -y
[root@node1 ~]# htpasswd -nBC 12 '' | tr -d ':\n'
New password:
Re-type new password:
$2y$12$JvsLhXCFePQNY5VV2jD7PeZTEJifCNAg0aCj7iBoQRO4/SRH/vMgi[root@node1 ~]#
修改配置文件
[root@node1 ~]# cd /usr/local/node_exporter/
[root@node1 node_exporter]# vim config.yml
basic_auth_users:
prometheus: $2y$12$JvsLhXCFePQNY5V