Prometheus 监控与告警第1部Prometheus 监控一、基础概念1. Prometheus 是什么Prometheus 是一个开源系统监控和警报工具最初由 SoundCloud 创建。自 2012 年以来许多公司和组织都采用了 Prometheus该项目拥有非常活跃的开发者和用户社区。它现在是一个独立的开源项目独立于任何公司进行维护。为了强调这一点并明确项目的治理结构Prometheus 于 2016 年作为继 Kubernetes 之后的第二个托管项目加入了云原生计算基金会CNCF。Prometheus 将其指标收集并存储为时间序列数据即指标信息与记录时的时间戳以及称为标签的可选键值对一起存储。2. Prometheus 特征多维数据模型使用指标名称和键值对标识时间序列数据。灵活的查询语言 PromQL可以利用数据的多维特性进行查询。不依赖分布式存储单个服务器节点是自治的。通过 HTTP 的拉取模型进行时间序列数据的收集。支持通过中间网关进行时间序列的推送。可以通过服务发现或静态配置发现监控目标。提供多种图形和仪表盘支持模式。3. 什么是指标通俗来说指标是数值型的测量结果。时间序列意味着随着时间的推移记录变化。不同的应用程序需要测量的内容各不相同。对于一个网页服务器来说可能是请求的响应时间对于一个数据库来说可能是活动连接数或活动查询数等。指标在理解应用程序为什么以某种方式工作中起着重要作用。假设你在运行一个网络应用程序并发现应用程序很慢。你需要一些信息来找出应用程序出了什么问题。例如当请求的数量很大时应用程序可能变慢。如果你有请求计数的指标你可以找出原因并增加服务器数量来处理负载。4. Prometheus 组件普罗米修斯生态系统由多个组成部分组成其中许多是可选的主要的 Prometheus 服务器用于抓取和存储时间序列数据。用于仪表化应用程序代码的客户端库。用于支持短期作业的推送网关。用于服务如 HAProxy、StatsD、Graphite 等的特定用途的导出器。用于处理告警的告警管理器。各种支持工具。大部分 Prometheus 组件都是使用 Go 语言编写的这使得它们易于构建和部署为静态二进制文件。5. Prometheus 架构Prometheus 可以直接从提供 Prometheus 兼容的 HTTP 端点的目标中抓取指标数据。这使得它可以收集各种指标例如 CPU 使用率、内存消耗、请求延迟以及自定义的应用程序特定指标。此外对于短暂的作业或无法直接访问的目标Prometheus 可以利用一个称为推送网关的中间组件。作业可以将其指标推送到推送网关然后 Prometheus 定期从网关中抓取数据。一旦 Prometheus 收集到指标数据它会将其本地存储在时间序列数据库中。它根据指标名称和相关标签的组合来组织数据。这使得可以高效地查询和检索特定的指标或指标集。Prometheus 提供了一种强大的查询语言称为 PromQLPrometheus 查询语言允许用户对收集到的数据执行复杂的查询和聚合操作。用户可以定义规则对时间序列数据进行处理生成新的衍生指标执行聚合操作或者根据特定条件计算告警。为了可视化收集到的指标并创建仪表盘Prometheus 可以与 Grafana 集成Grafana 是一个流行的开源数据可视化工具。Grafana 可以通过其 API 消费 Prometheus 的数据并创建丰富的可视化效果、图表和图形。它提供了一个用户友好的界面用于探索和监控收集到的指标数据。总而言之Prometheus 是一个功能强大的监控和告警工具可以从被监控的作业或服务中收集指标数据将其本地存储并对数据进行规则处理同时通过 Grafana 或其他 API 消费者进行可视化。这种组合为实时监控和故障排除应用程序和基础设施提供了强大的功能。6. Prometheus 应用场景Prometheus 非常适合记录任何纯数字的时间序列。它适用于机器中心的监控也适用于高度动态的面向服务的架构监控。在微服务的世界中它对多维数据的收集和查询支持是其特点之一。Prometheus 的设计注重可靠性它成为你在故障期间快速诊断问题的系统。每个 Prometheus 服务器都是独立的不依赖于网络存储或其他远程服务。在基础架构的其他部分出现故障时你可以依赖它并且不需要建立复杂的基础设施来使用它。7. Prometheus 不适用场景Prometheus 非常重视可靠性。即使在故障情况下仍然可以查看有关系统的可用统计信息。如果需要 100% 的准确性例如对每个请求进行计费那么 Prometheus 可能不是一个好的选择因为收集到的数据可能不够详细和完整。在这种情况下最好使用其他系统来收集和分析计费数据而将 Prometheus 用于其他监控方面。8. Prometheus 官方网址https://prometheus.io/docs/introduction/overview/二、Prometheus 部署1. 环境准备序号主机名IP 地址角色1Prometheus-server10.1.8.140/24server2Prometheus-agent10.1.8.131/24agent官方网站https://prometheus.io/download/2. 源码包下载在官网下载页面选择Linux平台、amd64架构的源码包。3. 安装 Prometheus# 下载源码包[rootprometheus-server ~]# wget https://github.com/prometheus/prometheus/releases/download/v3.5.1/prometheus-3.5.1.linux-amd64.tar.gz# 解压[rootprometheus-server ~]# tar zxvf prometheus-3.5.1.linux-amd64.tar.gzprometheus-3.5.1.linux-amd64/ prometheus-3.5.1.linux-amd64/NOTICE prometheus-3.5.1.linux-amd64/LICENSE prometheus-3.5.1.linux-amd64/prometheus.yml prometheus-3.5.1.linux-amd64/promtool prometheus-3.5.1.linux-amd64/prometheus# 移动到安装目录[rootprometheus-server ~]# mv prometheus-3.5.1.linux-amd64 /usr/local/src/prometheus[rootprometheus-server ~]# cd /usr/local/src/prometheus/[rootprometheus-server prometheus]# lsLICENSE NOTICE prometheus prometheus.yml promtool修改配置文件prometheus.yml# 主要修改以下部分scrape_configs:-job_name:prometheus-server# metrics_path defaults to /metrics# scheme defaults to http.static_configs:-targets:[10.1.8.140:9090]# 设置本地节点IP4. 启动服务[rootprometheus-server prometheus]# nohup ./prometheus --config.fileprometheus.yml [1]376855. 使用 systemd 管理服务先结束当前运行的进程[rootprometheus-server prometheus]# ps -ef | grep prometheusroot3768536686023:27 pts/0 00:00:00 ./prometheus--config.fileprometheus.yml root3773736686023:31 pts/0 00:00:00grep--colorauto prometheus[rootprometheus-server prometheus]# kill -9 37685[1] 已杀死nohup添加 service 文件[rootprometheus-server prometheus]# vim /usr/lib/systemd/system/prometheus.service[Unit] Descriptionprometheus Afternetwork.target [Service] ExecStart/usr/local/src/prometheus/prometheus --config.file/usr/local/src/prometheus/prometheus.yml [Install] WantedBymulti-user.target启动服务[rootprometheus-server prometheus]# systemctl daemon-reload[rootprometheus-server prometheus]# systemctl enable prometheus.service --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/prometheus.service to /usr/lib/systemd/system/prometheus.service.6. 关闭防火墙和 SELinux[rootprometheus-server prometheus]# systemctl disable firewalld.service --nowRemoved symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.[rootprometheus-server prometheus]# setenforce 07. 访问 Prometheus 网站打开浏览器访问http://10.1.8.140:9090/8. 查看监控目标在 Web 界面点击Status → Target health可以查看到本节点被监控的状态。点击http://10.1.8.140:9090/metrics可查看指标数据。三、Prometheus 监控主机1. 下载 node_exporter 源码包在 Prometheus 官网下载页面找到node_exporterExporter for machine metrics选择 Linux amd64 版本下载。[rootprometheus-server ~]# wget https://github.com/prometheus/node_exporter/releases/download/v1.10.2/node_exporter-1.10.2.linux-amd64.tar.gz2. 使用 exporter 监控本地节点# 解压[rootprometheus-server ~]# tar zxvf node_exporter-1.10.2.linux-amd64.tar.gz# 移动到安装目录[rootprometheus-server ~]# mv node_exporter-1.10.2.linux-amd64 /usr/local/src/node_exporter[rootprometheus-server ~]# cd /usr/local/src/node_exporter/[rootprometheus-server node_exporter]# lsLICENSE node_exporter NOTICE添加 service 文件[rootprometheus-server ~]# vim /usr/lib/systemd/system/node_exporter.service[Unit] Descriptionnode_exporter [Service] ExecStart/usr/local/src/node_exporter/node_exporter [Install] WantedBymulti-user.target启动服务[rootprometheus-server ~]# systemctl daemon-reload[rootprometheus-server ~]# systemctl enable node_exporter.service --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/node_exporter.service to /usr/lib/systemd/system/node_exporter.service.修改 Prometheus 配置文件增加监控配置项[rootprometheus-server ~]# vim /usr/local/src/prometheus/prometheus.yml在末尾添加-job_name:node_exporter_prometheus-server# metrics_path defaults to /metrics# scheme defaults to http.static_configs:-targets:[10.1.8.140:9100]# 端口9100重启 Prometheus 服务[rootprometheus-server ~]# systemctl restart prometheus打开页面查看 Target可以看到node_exporter_prometheus-server监控目标状态为up。3. 使用 exporter 监控其他节点在 agent 节点10.1.8.131上操作# 关闭防火墙及 SELinux[rootprometheus-agent ~]# systemctl disable firewalld.service --now[rootprometheus-agent ~]# setenforce 0# 解压软件包[rootprometheus-agent ~]# tar zxvf node_exporter-1.10.2.linux-amd64.tar.gz[rootprometheus-agent ~]# mv node_exporter-1.10.2.linux-amd64 /usr/local/src/node_exporter添加 service 文件并启动[rootprometheus-agent ~]# vim /usr/lib/systemd/system/node_exporter.service[Unit] Descriptionnode_exporter [Service] ExecStart/usr/local/src/node_exporter/node_exporter [Install] WantedBymulti-user.target[rootprometheus-agent ~]# systemctl daemon-reload[rootprometheus-agent ~]# systemctl enable node_exporter.service --now回到 Prometheus-server 服务上添加配置[rootprometheus-server ~]# vim /usr/local/src/prometheus/prometheus.yml在末尾添加-job_name:node_exporter_agent# metrics_path defaults to /metrics# scheme defaults to http.static_configs:-targets:[10.1.8.131:9100]重启 Prometheus 服务[rootprometheus-server ~]# systemctl restart prometheus打开页面查看 Target可以看到node_exporter_agent监控目标状态为up。四、Grafana 展现数据主机名IPgrafana10.1.8.1411. Grafana 介绍Grafana 是一个开源的可视化和监控工具能把各种数据源比如 Prometheus、InfluxDB、MySQL的指标和日志统一展示在酷炫的仪表盘上特别适合监控系统状态和分析业务数据。核心功能数据统一不用把数据都导入一个地方它能直接连上现有数据源比如云服务、数据库甚至 Google Sheets统一可视化。灵活仪表盘支持多种图表类型折线图、热力图等还能自定义查询和告警团队协作和分享也方便。多数据源支持兼容 Prometheus、MySQL、Elasticsearch 等主流数据源。2. 软件包下载官网地址https://grafana.com/进入下载页面选择版本最新版如 12.3.1EditionOSS开源版系统Linux3. 部署 Grafana开启新节点进行部署注意关闭防火墙[rootgrafana ~]# tar zxvf grafana_12.3.1_20271043721_linux_amd64.tar.gz[rootgrafana ~]# mv grafana-12.3.1 /usr/local/src/grafana4. 启动服务[rootgrafana ~]# nohup /usr/local/src/grafana/bin/grafana-server --config/usr/local/src/grafana/conf/defaults.ini --homepath/usr/local/src/grafana [1]50437[rootgrafana ~]# ps -ef | grep grafanaroot504374868517:41 pts/2 00:00:03 grafana server--config/usr/local/src/grafana/conf/defaults.ini--homepath/usr/local/src/grafana# 查看 3000 端口[rootgrafana ~]# ss -anput | grep 3000tcp LISTEN0128:::3000 :::* users:((grafana,pid50437,fd15))5. 使用 systemd 启动[rootgrafana ~]# kill -9 50437创建 service 文件[rootgrafana ~]# vim /usr/lib/systemd/system/grafana.service[Unit] Descriptiongrafana Afternetwork.target [Service] ExecStart/usr/local/src/grafana/bin/grafana-server --config/usr/local/src/grafana/conf/defaults.ini --homepath/usr/local/src/grafana [Install] WantedBymulti-user.target启动[rootgrafana ~]# systemctl daemon-reload[rootgrafana ~]# systemctl enable grafana.service --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/grafana.service to /usr/lib/systemd/system/grafana.service.6. 访问网站打开浏览器访问http://10.1.8.141:3000用户名admin密码admin首次登录会提示修改密码可以选择Skip跳过。7. 添加数据源点击左侧菜单Connections → Data sources点击Add data source选择Prometheus填写 Prometheus 地址点击Save test提示 “Successfully queried the Prometheus API” 即表示成功8. 添加仪表盘Grafana 官网仪表盘地址https://grafana.com/grafana/dashboards/搜索对主机进行监控的仪表盘输入 ID8919Prometheus Linux 主机详情回到 Grafana 网页界面点击Dashboards → New → Import输入8919点击Load选择之前添加的 Prometheus 数据源如prometheus-smart点击Import即可看到主机状态仪表盘包含 CPU、内存、磁盘、网络等各项指标。第2部Prometheus 告警一、Alertmanager 介绍实现 Prometheus 的告警需 Alertmanager 这个组件。Alertmanager 与 Prometheus 是相互分离的两个组件需单独安装配置。通过在 Prometheus 中定义 AlertRule告警规则Prometheus 会周期性的对告警规则进行计算如果满足告警触发条件就会向 Alertmanager 发送告警信息。在 Prometheus 中一条告警规则主要由告警名称和告警规则两部分组成告警名称用户为告警规则命名告警规则由 PromQL 进行定义其实际意义是当表达式PromQL查询结果持续多长时间During后触发告警Prometheus 服务器根据报警规则将警报发送给 Alertmanager然后 Alertmanager 将静默silencing、抑制inhibition、分组聚合aggregation等消息通过 Email、钉钉等发送通知。Alertmanager 特性分组聚合分组将同一类型的报警归类单个报警通知。适用于当系统宕机导致大量报警被同时触发此时分组机制可将这些被触发的告警合并为一个告警通知避免一次性发送大量告警通知。静默提供了一个简单的机制可以快速根据标签对告警进行静默处理。特定时间不会发送告警通知。抑制指当警报发出后停止重复发送由此警报引发其他错误的警报的机制。如网络不可达导致其他服务连接相关警报。二、Alertmanager 部署主机名IPalertmanager10.1.8.1421. 从 Prometheus 官网获取在 Prometheus 官网下载页面找到alertmanager选择 Linux amd64 版本。[rootalertmanager ~]# wget https://github.com/prometheus/alertmanager/releases/download/v0.30.1/alertmanager-0.30.1.linux-amd64.tar.gz2. 源码包解压[rootalertmanager ~]# tar zxvf alertmanager-0.30.1.linux-amd64.tar.gzalertmanager-0.30.1.linux-amd64/ alertmanager-0.30.1.linux-amd64/alertmanager.yml alertmanager-0.30.1.linux-amd64/NOTICE alertmanager-0.30.1.linux-amd64/alertmanager alertmanager-0.30.1.linux-amd64/LICENSE alertmanager-0.30.1.linux-amd64/amtool[rootalertmanager ~]# mv alertmanager-0.30.1.linux-amd64 /usr/local/src/alertmanager3. 启动服务[rootalertmanager ~]# nohup /usr/local/src/alertmanager/alertmanager --config.file/usr/local/src/alertmanager/alertmanager.yml [1]19884. 使用 systemd 管理[rootalertmanager ~]# kill -9 1988[rootalertmanager ~]# vim /usr/lib/systemd/system/alertmanager.service[Unit] Descriptionalertmanager [Service] ExecStart/usr/local/src/alertmanager/alertmanager --config.file/usr/local/src/alertmanager/alertmanager.yml [Install] WantedBymulti-user.target[rootalertmanager ~]# systemctl daemon-reload[rootalertmanager ~]# systemctl enable alertmanager.service --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/alertmanager.service to /usr/lib/systemd/system/alertmanager.service.5. 查看端口[rootalertmanager ~]# ss -anput | grep 9093tcp LISTEN0128:::9093 :::* users:((alertmanager,pid2127,fd7))6. 打开网页访问http://10.1.8.142:9093/三、集成到 Prometheus在prometheus.yml中修改[rootprometheus-server ~]# vim /usr/local/src/prometheus/prometheus.yml# Alertmanager configurationalerting:alertmanagers:-static_configs:-targets:-10.1.8.142:9093# 修改为 alertmanager 的 IP 及对应端口# 规则文件rule_files:-rules/*.yml# 规则文件都存放到 rules 目录下# 增加 alertmanager 监控项-job_name:alertmanager# metrics_path defaults to /metrics# scheme defaults to http.static_configs:-targets:-10.1.8.142:9093四、邮箱告警及告警规则1. 注册邮箱开启授权码以 163 邮箱为例登录邮箱进入设置 → POP3/SMTP/IMAP确保IMAP/SMTP 服务和POP3/SMTP 服务已开启在授权密码管理中新增授权码用于第三方登录2. 在 Alertmanager 服务器上配置邮箱告警[rootalertmanager ~]# cd /usr/local/src/alertmanager/[rootalertmanager alertmanager]# cp -p alertmanager.yml alertmanager.yml.bak[rootalertmanager alertmanager]# vim alertmanager.ymlglobal:resolve_timeout:5msmtp_smarthost:smtp.163.com:25smtp_from:xxxxxx163.comsmtp_auth_username:xxxxxxxxxx163.comsmtp_auth_password:XXXXXXXXXXXXsmtp_require_tls:falseroute:group_by:[alertname]group_wait:10sgroup_interval:5mrepeat_interval:1mreceiver:mailreceivers:-name:mailemail_configs:-to:xxxxxxxx163.com配置解释global全局设置配置解决告警时间间隔和邮件发送服务resolve_timeout: 5m定义持续多长时间未接收到告警标记后就将告警状态标记为 resolvedsmtp_smarthost: smtp.163.com:25邮件服务器smtp_from告警发送邮箱smtp_auth_username邮箱名smtp_auth_password邮箱认证使用授权码smtp_require_tls: false是否启动 TLSroute路由树每个告警都会在配置的顶级路由中进入路由树group_by: [alertname]告警过滤中分组标签group_wait: 10s分组等待的时间group_interval: 5m上下两组发送告警的间隔时间repeat_interval: 1m重复发送告警时间默认为 1hreceiver: mail指定告警媒介类型receivers告警接收器name: mail告警来源自定义名称email_configs.to指定接收端 email重启 alertmanager[rootalertmanager alertmanager]# systemctl restart alertmanager3. 在 Prometheus-server 上设置告警规则[rootprometheus-server ~]# mkdir /usr/local/src/prometheus/rules[rootprometheus-server ~]# vim /usr/local/src/prometheus/rules/node_alerts.ymlgroups:-name:general.rulesrules:-alert:NodeFilesystemUsageexpr:100-(node_filesystem_free_bytes{mountpoint/,fstype~ext4|xfs}/ node_filesystem_size_bytes{fstype~ext4|xfs}* 100)30for:1mlabels:severity:warningannotations:summary:Instance {{ $labels.instance }}: {{ $labels.mountpoint }} 分区使用率过高description:{{ $labels.instance }}: {{ $labels.job }}: {{ $labels.mountpoint }} 这个分区使用百分之三十当前值: {{ $value }}告警规则解释groups.name告警规则组名称rules定义规则alert: NodeFilesystemUsage告警名称在 Alertmanager 及邮箱可见expr表达式获取硬盘使用率大于 30% 触发告警for: 1m持续时间表示持续 1 分钟满足条件则触发报警0 表示不使用持续时间labels.severity定义当前告警规则级别annotations注释告警通知summary告警摘要description告警详细描述重启 Prometheus 服务[rootprometheus-server ~]# systemctl restart prometheus4. 验证邮箱告警在 Prometheus-server 服务器上操作先查看当前/分区占用[rootprometheus-server ~]# df -hT文件系统 类型 容量 已用 可用 已用% 挂载点 /dev/sda2 xfs 180G4.0G 176G3% /进行测试创建大容量文件[rootprometheus-server ~]# dd if/dev/zero of/test bs10M count10000此时Prometheus 页面已经出现告警提示Alerts 页面显示NodeFilesystemUsage状态为 PENDING → FIRINGAlertmanager中也已经出现邮件告警163 邮箱中收到了告警邮件包含告警标签和注释信息测试完成后可删除测试文件[rootprometheus-server ~]# rm -f /tests注释告警通知 -summary告警摘要 -description告警详细描述重启 Prometheus 服务[rootprometheus-server ~]# systemctl restart prometheus4. 验证邮箱告警在 Prometheus-server 服务器上操作先查看当前/分区占用[rootprometheus-server ~]# df -hT文件系统 类型 容量 已用 可用 已用% 挂载点 /dev/sda2 xfs 180G4.0G 176G3% /进行测试创建大容量文件[rootprometheus-server ~]# dd if/dev/zero of/test bs10M count10000此时Prometheus 页面已经出现告警提示Alerts 页面显示NodeFilesystemUsage状态为 PENDING → FIRINGAlertmanager中也已经出现邮件告警163 邮箱中收到了告警邮件包含告警标签和注释信息测试完成后可删除测试文件[rootprometheus-server ~]# rm -f /test
Prometheus 监控与告警
Prometheus 监控与告警第1部Prometheus 监控一、基础概念1. Prometheus 是什么Prometheus 是一个开源系统监控和警报工具最初由 SoundCloud 创建。自 2012 年以来许多公司和组织都采用了 Prometheus该项目拥有非常活跃的开发者和用户社区。它现在是一个独立的开源项目独立于任何公司进行维护。为了强调这一点并明确项目的治理结构Prometheus 于 2016 年作为继 Kubernetes 之后的第二个托管项目加入了云原生计算基金会CNCF。Prometheus 将其指标收集并存储为时间序列数据即指标信息与记录时的时间戳以及称为标签的可选键值对一起存储。2. Prometheus 特征多维数据模型使用指标名称和键值对标识时间序列数据。灵活的查询语言 PromQL可以利用数据的多维特性进行查询。不依赖分布式存储单个服务器节点是自治的。通过 HTTP 的拉取模型进行时间序列数据的收集。支持通过中间网关进行时间序列的推送。可以通过服务发现或静态配置发现监控目标。提供多种图形和仪表盘支持模式。3. 什么是指标通俗来说指标是数值型的测量结果。时间序列意味着随着时间的推移记录变化。不同的应用程序需要测量的内容各不相同。对于一个网页服务器来说可能是请求的响应时间对于一个数据库来说可能是活动连接数或活动查询数等。指标在理解应用程序为什么以某种方式工作中起着重要作用。假设你在运行一个网络应用程序并发现应用程序很慢。你需要一些信息来找出应用程序出了什么问题。例如当请求的数量很大时应用程序可能变慢。如果你有请求计数的指标你可以找出原因并增加服务器数量来处理负载。4. Prometheus 组件普罗米修斯生态系统由多个组成部分组成其中许多是可选的主要的 Prometheus 服务器用于抓取和存储时间序列数据。用于仪表化应用程序代码的客户端库。用于支持短期作业的推送网关。用于服务如 HAProxy、StatsD、Graphite 等的特定用途的导出器。用于处理告警的告警管理器。各种支持工具。大部分 Prometheus 组件都是使用 Go 语言编写的这使得它们易于构建和部署为静态二进制文件。5. Prometheus 架构Prometheus 可以直接从提供 Prometheus 兼容的 HTTP 端点的目标中抓取指标数据。这使得它可以收集各种指标例如 CPU 使用率、内存消耗、请求延迟以及自定义的应用程序特定指标。此外对于短暂的作业或无法直接访问的目标Prometheus 可以利用一个称为推送网关的中间组件。作业可以将其指标推送到推送网关然后 Prometheus 定期从网关中抓取数据。一旦 Prometheus 收集到指标数据它会将其本地存储在时间序列数据库中。它根据指标名称和相关标签的组合来组织数据。这使得可以高效地查询和检索特定的指标或指标集。Prometheus 提供了一种强大的查询语言称为 PromQLPrometheus 查询语言允许用户对收集到的数据执行复杂的查询和聚合操作。用户可以定义规则对时间序列数据进行处理生成新的衍生指标执行聚合操作或者根据特定条件计算告警。为了可视化收集到的指标并创建仪表盘Prometheus 可以与 Grafana 集成Grafana 是一个流行的开源数据可视化工具。Grafana 可以通过其 API 消费 Prometheus 的数据并创建丰富的可视化效果、图表和图形。它提供了一个用户友好的界面用于探索和监控收集到的指标数据。总而言之Prometheus 是一个功能强大的监控和告警工具可以从被监控的作业或服务中收集指标数据将其本地存储并对数据进行规则处理同时通过 Grafana 或其他 API 消费者进行可视化。这种组合为实时监控和故障排除应用程序和基础设施提供了强大的功能。6. Prometheus 应用场景Prometheus 非常适合记录任何纯数字的时间序列。它适用于机器中心的监控也适用于高度动态的面向服务的架构监控。在微服务的世界中它对多维数据的收集和查询支持是其特点之一。Prometheus 的设计注重可靠性它成为你在故障期间快速诊断问题的系统。每个 Prometheus 服务器都是独立的不依赖于网络存储或其他远程服务。在基础架构的其他部分出现故障时你可以依赖它并且不需要建立复杂的基础设施来使用它。7. Prometheus 不适用场景Prometheus 非常重视可靠性。即使在故障情况下仍然可以查看有关系统的可用统计信息。如果需要 100% 的准确性例如对每个请求进行计费那么 Prometheus 可能不是一个好的选择因为收集到的数据可能不够详细和完整。在这种情况下最好使用其他系统来收集和分析计费数据而将 Prometheus 用于其他监控方面。8. Prometheus 官方网址https://prometheus.io/docs/introduction/overview/二、Prometheus 部署1. 环境准备序号主机名IP 地址角色1Prometheus-server10.1.8.140/24server2Prometheus-agent10.1.8.131/24agent官方网站https://prometheus.io/download/2. 源码包下载在官网下载页面选择Linux平台、amd64架构的源码包。3. 安装 Prometheus# 下载源码包[rootprometheus-server ~]# wget https://github.com/prometheus/prometheus/releases/download/v3.5.1/prometheus-3.5.1.linux-amd64.tar.gz# 解压[rootprometheus-server ~]# tar zxvf prometheus-3.5.1.linux-amd64.tar.gzprometheus-3.5.1.linux-amd64/ prometheus-3.5.1.linux-amd64/NOTICE prometheus-3.5.1.linux-amd64/LICENSE prometheus-3.5.1.linux-amd64/prometheus.yml prometheus-3.5.1.linux-amd64/promtool prometheus-3.5.1.linux-amd64/prometheus# 移动到安装目录[rootprometheus-server ~]# mv prometheus-3.5.1.linux-amd64 /usr/local/src/prometheus[rootprometheus-server ~]# cd /usr/local/src/prometheus/[rootprometheus-server prometheus]# lsLICENSE NOTICE prometheus prometheus.yml promtool修改配置文件prometheus.yml# 主要修改以下部分scrape_configs:-job_name:prometheus-server# metrics_path defaults to /metrics# scheme defaults to http.static_configs:-targets:[10.1.8.140:9090]# 设置本地节点IP4. 启动服务[rootprometheus-server prometheus]# nohup ./prometheus --config.fileprometheus.yml [1]376855. 使用 systemd 管理服务先结束当前运行的进程[rootprometheus-server prometheus]# ps -ef | grep prometheusroot3768536686023:27 pts/0 00:00:00 ./prometheus--config.fileprometheus.yml root3773736686023:31 pts/0 00:00:00grep--colorauto prometheus[rootprometheus-server prometheus]# kill -9 37685[1] 已杀死nohup添加 service 文件[rootprometheus-server prometheus]# vim /usr/lib/systemd/system/prometheus.service[Unit] Descriptionprometheus Afternetwork.target [Service] ExecStart/usr/local/src/prometheus/prometheus --config.file/usr/local/src/prometheus/prometheus.yml [Install] WantedBymulti-user.target启动服务[rootprometheus-server prometheus]# systemctl daemon-reload[rootprometheus-server prometheus]# systemctl enable prometheus.service --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/prometheus.service to /usr/lib/systemd/system/prometheus.service.6. 关闭防火墙和 SELinux[rootprometheus-server prometheus]# systemctl disable firewalld.service --nowRemoved symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.[rootprometheus-server prometheus]# setenforce 07. 访问 Prometheus 网站打开浏览器访问http://10.1.8.140:9090/8. 查看监控目标在 Web 界面点击Status → Target health可以查看到本节点被监控的状态。点击http://10.1.8.140:9090/metrics可查看指标数据。三、Prometheus 监控主机1. 下载 node_exporter 源码包在 Prometheus 官网下载页面找到node_exporterExporter for machine metrics选择 Linux amd64 版本下载。[rootprometheus-server ~]# wget https://github.com/prometheus/node_exporter/releases/download/v1.10.2/node_exporter-1.10.2.linux-amd64.tar.gz2. 使用 exporter 监控本地节点# 解压[rootprometheus-server ~]# tar zxvf node_exporter-1.10.2.linux-amd64.tar.gz# 移动到安装目录[rootprometheus-server ~]# mv node_exporter-1.10.2.linux-amd64 /usr/local/src/node_exporter[rootprometheus-server ~]# cd /usr/local/src/node_exporter/[rootprometheus-server node_exporter]# lsLICENSE node_exporter NOTICE添加 service 文件[rootprometheus-server ~]# vim /usr/lib/systemd/system/node_exporter.service[Unit] Descriptionnode_exporter [Service] ExecStart/usr/local/src/node_exporter/node_exporter [Install] WantedBymulti-user.target启动服务[rootprometheus-server ~]# systemctl daemon-reload[rootprometheus-server ~]# systemctl enable node_exporter.service --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/node_exporter.service to /usr/lib/systemd/system/node_exporter.service.修改 Prometheus 配置文件增加监控配置项[rootprometheus-server ~]# vim /usr/local/src/prometheus/prometheus.yml在末尾添加-job_name:node_exporter_prometheus-server# metrics_path defaults to /metrics# scheme defaults to http.static_configs:-targets:[10.1.8.140:9100]# 端口9100重启 Prometheus 服务[rootprometheus-server ~]# systemctl restart prometheus打开页面查看 Target可以看到node_exporter_prometheus-server监控目标状态为up。3. 使用 exporter 监控其他节点在 agent 节点10.1.8.131上操作# 关闭防火墙及 SELinux[rootprometheus-agent ~]# systemctl disable firewalld.service --now[rootprometheus-agent ~]# setenforce 0# 解压软件包[rootprometheus-agent ~]# tar zxvf node_exporter-1.10.2.linux-amd64.tar.gz[rootprometheus-agent ~]# mv node_exporter-1.10.2.linux-amd64 /usr/local/src/node_exporter添加 service 文件并启动[rootprometheus-agent ~]# vim /usr/lib/systemd/system/node_exporter.service[Unit] Descriptionnode_exporter [Service] ExecStart/usr/local/src/node_exporter/node_exporter [Install] WantedBymulti-user.target[rootprometheus-agent ~]# systemctl daemon-reload[rootprometheus-agent ~]# systemctl enable node_exporter.service --now回到 Prometheus-server 服务上添加配置[rootprometheus-server ~]# vim /usr/local/src/prometheus/prometheus.yml在末尾添加-job_name:node_exporter_agent# metrics_path defaults to /metrics# scheme defaults to http.static_configs:-targets:[10.1.8.131:9100]重启 Prometheus 服务[rootprometheus-server ~]# systemctl restart prometheus打开页面查看 Target可以看到node_exporter_agent监控目标状态为up。四、Grafana 展现数据主机名IPgrafana10.1.8.1411. Grafana 介绍Grafana 是一个开源的可视化和监控工具能把各种数据源比如 Prometheus、InfluxDB、MySQL的指标和日志统一展示在酷炫的仪表盘上特别适合监控系统状态和分析业务数据。核心功能数据统一不用把数据都导入一个地方它能直接连上现有数据源比如云服务、数据库甚至 Google Sheets统一可视化。灵活仪表盘支持多种图表类型折线图、热力图等还能自定义查询和告警团队协作和分享也方便。多数据源支持兼容 Prometheus、MySQL、Elasticsearch 等主流数据源。2. 软件包下载官网地址https://grafana.com/进入下载页面选择版本最新版如 12.3.1EditionOSS开源版系统Linux3. 部署 Grafana开启新节点进行部署注意关闭防火墙[rootgrafana ~]# tar zxvf grafana_12.3.1_20271043721_linux_amd64.tar.gz[rootgrafana ~]# mv grafana-12.3.1 /usr/local/src/grafana4. 启动服务[rootgrafana ~]# nohup /usr/local/src/grafana/bin/grafana-server --config/usr/local/src/grafana/conf/defaults.ini --homepath/usr/local/src/grafana [1]50437[rootgrafana ~]# ps -ef | grep grafanaroot504374868517:41 pts/2 00:00:03 grafana server--config/usr/local/src/grafana/conf/defaults.ini--homepath/usr/local/src/grafana# 查看 3000 端口[rootgrafana ~]# ss -anput | grep 3000tcp LISTEN0128:::3000 :::* users:((grafana,pid50437,fd15))5. 使用 systemd 启动[rootgrafana ~]# kill -9 50437创建 service 文件[rootgrafana ~]# vim /usr/lib/systemd/system/grafana.service[Unit] Descriptiongrafana Afternetwork.target [Service] ExecStart/usr/local/src/grafana/bin/grafana-server --config/usr/local/src/grafana/conf/defaults.ini --homepath/usr/local/src/grafana [Install] WantedBymulti-user.target启动[rootgrafana ~]# systemctl daemon-reload[rootgrafana ~]# systemctl enable grafana.service --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/grafana.service to /usr/lib/systemd/system/grafana.service.6. 访问网站打开浏览器访问http://10.1.8.141:3000用户名admin密码admin首次登录会提示修改密码可以选择Skip跳过。7. 添加数据源点击左侧菜单Connections → Data sources点击Add data source选择Prometheus填写 Prometheus 地址点击Save test提示 “Successfully queried the Prometheus API” 即表示成功8. 添加仪表盘Grafana 官网仪表盘地址https://grafana.com/grafana/dashboards/搜索对主机进行监控的仪表盘输入 ID8919Prometheus Linux 主机详情回到 Grafana 网页界面点击Dashboards → New → Import输入8919点击Load选择之前添加的 Prometheus 数据源如prometheus-smart点击Import即可看到主机状态仪表盘包含 CPU、内存、磁盘、网络等各项指标。第2部Prometheus 告警一、Alertmanager 介绍实现 Prometheus 的告警需 Alertmanager 这个组件。Alertmanager 与 Prometheus 是相互分离的两个组件需单独安装配置。通过在 Prometheus 中定义 AlertRule告警规则Prometheus 会周期性的对告警规则进行计算如果满足告警触发条件就会向 Alertmanager 发送告警信息。在 Prometheus 中一条告警规则主要由告警名称和告警规则两部分组成告警名称用户为告警规则命名告警规则由 PromQL 进行定义其实际意义是当表达式PromQL查询结果持续多长时间During后触发告警Prometheus 服务器根据报警规则将警报发送给 Alertmanager然后 Alertmanager 将静默silencing、抑制inhibition、分组聚合aggregation等消息通过 Email、钉钉等发送通知。Alertmanager 特性分组聚合分组将同一类型的报警归类单个报警通知。适用于当系统宕机导致大量报警被同时触发此时分组机制可将这些被触发的告警合并为一个告警通知避免一次性发送大量告警通知。静默提供了一个简单的机制可以快速根据标签对告警进行静默处理。特定时间不会发送告警通知。抑制指当警报发出后停止重复发送由此警报引发其他错误的警报的机制。如网络不可达导致其他服务连接相关警报。二、Alertmanager 部署主机名IPalertmanager10.1.8.1421. 从 Prometheus 官网获取在 Prometheus 官网下载页面找到alertmanager选择 Linux amd64 版本。[rootalertmanager ~]# wget https://github.com/prometheus/alertmanager/releases/download/v0.30.1/alertmanager-0.30.1.linux-amd64.tar.gz2. 源码包解压[rootalertmanager ~]# tar zxvf alertmanager-0.30.1.linux-amd64.tar.gzalertmanager-0.30.1.linux-amd64/ alertmanager-0.30.1.linux-amd64/alertmanager.yml alertmanager-0.30.1.linux-amd64/NOTICE alertmanager-0.30.1.linux-amd64/alertmanager alertmanager-0.30.1.linux-amd64/LICENSE alertmanager-0.30.1.linux-amd64/amtool[rootalertmanager ~]# mv alertmanager-0.30.1.linux-amd64 /usr/local/src/alertmanager3. 启动服务[rootalertmanager ~]# nohup /usr/local/src/alertmanager/alertmanager --config.file/usr/local/src/alertmanager/alertmanager.yml [1]19884. 使用 systemd 管理[rootalertmanager ~]# kill -9 1988[rootalertmanager ~]# vim /usr/lib/systemd/system/alertmanager.service[Unit] Descriptionalertmanager [Service] ExecStart/usr/local/src/alertmanager/alertmanager --config.file/usr/local/src/alertmanager/alertmanager.yml [Install] WantedBymulti-user.target[rootalertmanager ~]# systemctl daemon-reload[rootalertmanager ~]# systemctl enable alertmanager.service --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/alertmanager.service to /usr/lib/systemd/system/alertmanager.service.5. 查看端口[rootalertmanager ~]# ss -anput | grep 9093tcp LISTEN0128:::9093 :::* users:((alertmanager,pid2127,fd7))6. 打开网页访问http://10.1.8.142:9093/三、集成到 Prometheus在prometheus.yml中修改[rootprometheus-server ~]# vim /usr/local/src/prometheus/prometheus.yml# Alertmanager configurationalerting:alertmanagers:-static_configs:-targets:-10.1.8.142:9093# 修改为 alertmanager 的 IP 及对应端口# 规则文件rule_files:-rules/*.yml# 规则文件都存放到 rules 目录下# 增加 alertmanager 监控项-job_name:alertmanager# metrics_path defaults to /metrics# scheme defaults to http.static_configs:-targets:-10.1.8.142:9093四、邮箱告警及告警规则1. 注册邮箱开启授权码以 163 邮箱为例登录邮箱进入设置 → POP3/SMTP/IMAP确保IMAP/SMTP 服务和POP3/SMTP 服务已开启在授权密码管理中新增授权码用于第三方登录2. 在 Alertmanager 服务器上配置邮箱告警[rootalertmanager ~]# cd /usr/local/src/alertmanager/[rootalertmanager alertmanager]# cp -p alertmanager.yml alertmanager.yml.bak[rootalertmanager alertmanager]# vim alertmanager.ymlglobal:resolve_timeout:5msmtp_smarthost:smtp.163.com:25smtp_from:xxxxxx163.comsmtp_auth_username:xxxxxxxxxx163.comsmtp_auth_password:XXXXXXXXXXXXsmtp_require_tls:falseroute:group_by:[alertname]group_wait:10sgroup_interval:5mrepeat_interval:1mreceiver:mailreceivers:-name:mailemail_configs:-to:xxxxxxxx163.com配置解释global全局设置配置解决告警时间间隔和邮件发送服务resolve_timeout: 5m定义持续多长时间未接收到告警标记后就将告警状态标记为 resolvedsmtp_smarthost: smtp.163.com:25邮件服务器smtp_from告警发送邮箱smtp_auth_username邮箱名smtp_auth_password邮箱认证使用授权码smtp_require_tls: false是否启动 TLSroute路由树每个告警都会在配置的顶级路由中进入路由树group_by: [alertname]告警过滤中分组标签group_wait: 10s分组等待的时间group_interval: 5m上下两组发送告警的间隔时间repeat_interval: 1m重复发送告警时间默认为 1hreceiver: mail指定告警媒介类型receivers告警接收器name: mail告警来源自定义名称email_configs.to指定接收端 email重启 alertmanager[rootalertmanager alertmanager]# systemctl restart alertmanager3. 在 Prometheus-server 上设置告警规则[rootprometheus-server ~]# mkdir /usr/local/src/prometheus/rules[rootprometheus-server ~]# vim /usr/local/src/prometheus/rules/node_alerts.ymlgroups:-name:general.rulesrules:-alert:NodeFilesystemUsageexpr:100-(node_filesystem_free_bytes{mountpoint/,fstype~ext4|xfs}/ node_filesystem_size_bytes{fstype~ext4|xfs}* 100)30for:1mlabels:severity:warningannotations:summary:Instance {{ $labels.instance }}: {{ $labels.mountpoint }} 分区使用率过高description:{{ $labels.instance }}: {{ $labels.job }}: {{ $labels.mountpoint }} 这个分区使用百分之三十当前值: {{ $value }}告警规则解释groups.name告警规则组名称rules定义规则alert: NodeFilesystemUsage告警名称在 Alertmanager 及邮箱可见expr表达式获取硬盘使用率大于 30% 触发告警for: 1m持续时间表示持续 1 分钟满足条件则触发报警0 表示不使用持续时间labels.severity定义当前告警规则级别annotations注释告警通知summary告警摘要description告警详细描述重启 Prometheus 服务[rootprometheus-server ~]# systemctl restart prometheus4. 验证邮箱告警在 Prometheus-server 服务器上操作先查看当前/分区占用[rootprometheus-server ~]# df -hT文件系统 类型 容量 已用 可用 已用% 挂载点 /dev/sda2 xfs 180G4.0G 176G3% /进行测试创建大容量文件[rootprometheus-server ~]# dd if/dev/zero of/test bs10M count10000此时Prometheus 页面已经出现告警提示Alerts 页面显示NodeFilesystemUsage状态为 PENDING → FIRINGAlertmanager中也已经出现邮件告警163 邮箱中收到了告警邮件包含告警标签和注释信息测试完成后可删除测试文件[rootprometheus-server ~]# rm -f /tests注释告警通知 -summary告警摘要 -description告警详细描述重启 Prometheus 服务[rootprometheus-server ~]# systemctl restart prometheus4. 验证邮箱告警在 Prometheus-server 服务器上操作先查看当前/分区占用[rootprometheus-server ~]# df -hT文件系统 类型 容量 已用 可用 已用% 挂载点 /dev/sda2 xfs 180G4.0G 176G3% /进行测试创建大容量文件[rootprometheus-server ~]# dd if/dev/zero of/test bs10M count10000此时Prometheus 页面已经出现告警提示Alerts 页面显示NodeFilesystemUsage状态为 PENDING → FIRINGAlertmanager中也已经出现邮件告警163 邮箱中收到了告警邮件包含告警标签和注释信息测试完成后可删除测试文件[rootprometheus-server ~]# rm -f /test