1.环境准备
- 节点
Centos7 | IP |
prometheus & grafana&prometheus node | 192.168.0.xxx |
- 软件包
Prometheus-2.45.1: https://github.com/prometheus/prometheus/releases/download/v2.45.1/prometheus-2.45.1.linux-amd64.tar.gz
node_exporter-1.6.1: https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz
Grafana-10.2.0: https://dl.grafana.com/enterprise/release/grafana-enterprise-10.2.0-1.x86_64.rpm
alertmanager-0.27.0: https://github.com/prometheus/alertmanager/releases/download/v0.27.0/alertmanager-0.27.0.linux-amd64.tar.gz
2.部署prometheus
下载prometheus部署包
解压二进制文件
严重
新建用户
修改安装目录权限
创建prometheus运行数据目录
配置文件
在 prometheus监控系统,prometheus的职责是采集,查询和存储和推送报警到alertmanager。本文主要介绍下prometheus的配置文件。
1.配置文件格式
配置文件格式是yaml格式
2.配置文件指标说明
global指标
- 常用的命令行参数
alerting
这里定义和prometheus集成的alertmanager插件,用于监控报警。后续会单独进行alertmanger插件的配置、配置说明、报警媒介以及route路由规则记录。
rule_files
这个主要是用来设置告警规则,基于设定什么指标进行报警(类似触发器trigger)。这里设定好规则以后,prometheus会根据全局global设定的evaluation_interval参数进行扫描加载,规则改动后会自动加载。其报警媒介和route路由由alertmanager插件实现。
scrape_configs 配置采集目标 endpoints
支持的配置:
job_name: 任务目标名,可以理解成分组,每个分组包含具体的target组员。
scrape_interval: 5s 单独设定的话,会覆盖global设定的参数,拉取时间间隔为5s
metrics_path 监控项访问的url路径
targets: Endpoint 监控目标访问地址
上述为静态规则,没有设置自动发现。这种情况下增加主机需要自行修改规则,通过supervisor reload 对应任务,也是缺点:每次静态规则添加都要重启prometheus服务,不利于运维自动化。 也可以通过热加载的方式进行。
prometheus支持服务发现:
基于文件的服务发现方式不需要依赖其他平台与第三方服务,用户只需将要新的target信息以yaml或json文件格式添加到target文件中 ,prometheus会定期从指定文件中读取target信息并更新 好处: (1)不需要一个一个的手工去添加到主配置文件,只需要提交到要加载目录里边的json或yaml文件就可以了; (2)方便维护,且不需要每次都重启prometheus服务端。
Prometheus提供两种重新加载配置文件prometheus.yml的方法: 1.向prometheus进行发信号
2.向prometheus发送HTTP请求
创建服务脚本systemd管理
启动并验证
- 查看服务状态
- 查看服务端口
- 配置文件语法检测
访问web页面 http://192.168.0.234:9090
在Status菜单下,Configuration,Rule,Targets等:
Status–>Configuration展示prometheus.yml的配置,如下:
Status–>Targets展示监控具体的监控目标:
访问: http://192.168.0.234:9090/metrics,查看从exporter具体能抓到的数据:
在输入框中任意输入1个exporter能抓取得值,点击"Execute"按钮,即可见相应抓取数据的图形,数据以table和Graph:
3.部署node_exporter
Node_exporter收集机器的系统数据,这里采用prometheus官方提供的exporter,除node_exporter外,官方还提供consul,memcached,haproxy,mysqld等exporter,具体可查看官网。
安装 node_exporter
下载软件包
解压缩文件
修改安装目录权限
注册服务
启动服务
验证
查看 node_exporter服务 http://192.168.0.234:9101/metrics (默认端口为: 9101,默认9100)
添加 node_exporter到配置文件
安装好 node_exporter 后,我们需要在 Prometheus 中对其进行集成 1.修改 Prometheus 的配置文件 (prometheus.yml),添加 node 服务 (在 scrape_configs 属性下添加)
重启prometheus 服务
4.部署grafana
下载
安装
配置文件
设置开机启动
添加数据源
1.登录
访问: http://192.168.0.xxx:3000,默认账号/密码:admin/admin,首次登陆需要修改默认的管理员密码
2.添加数据源
在登陆首页,点击”Add data source”按钮,跳转到添加数据源页面,配置如下:
填写 URL: http://localhost:9090/:
设置页面为中文
导入主机详情模板:英文版-11074,中文-16098
5.部署alertmanager
下载
解压
修改安装目录权限
查看版本信息
设置开机启动
启动服务
访问: http://192.168.0.xxx:9093/metrics
6.配置企业微信报警
修改prometheus.yml配置文件
添加告警规则模板
重启prometheus,访问页面查看
安装docker
企业微信机器人配置及启动
群聊新建机器人,获取hook地址
启动企业微信机器人
修改alertmanager.yml配置文件
检查配置文件语法
重启alertmanager服务