
第1部Prometheus监控摘要本文从零开始搭建一套完整的 Prometheus 监控告警体系。首先介绍 Prometheus 的核心概念、组件与架构随后详细演示在 Linux 上部署 Prometheus 服务端并通过 node_exporter 实现对本地及远程主机的监控最后接入 Grafana 完成数据的可视化展示帮助读者快速掌握云原生监控的落地实践。基础概念1Prometheus是什么Prometheus 是开源的云原生监控告警系统由 SoundCloud 开发2016 年加入 CNCF是 Kubernetes 生态标配监控组件。2Prometheus特征1.多维数据模型使用指标名称和键值对标签标识时间序列数据。2.PromQL 查询语言灵活的查询语言可利用多维特性完成数据查询。3.单机自治不依赖分布式存储单个服务器节点即可独立工作。4.HTTP 拉取采集通过 HTTP 拉取模式收集时序指标数据。5.支持推送网关借助 Pushgateway 中间网关接收时序数据适配短生命周期作业。6.多种目标发现方式可通过服务发现或者静态配置发现监控目标。7.可视化支持提供多种图形和仪表盘展示模式可对接 Grafana。3什么是指标通俗来说指标是数值型的测量结果。时间序列意味着随着时间的推移记录变化。不同应用程序需要测量的内容各不相同网页服务器请求响应时间数据库活动连接数、活动查询数指标可以帮助理解应用程序运行状态。例如业务访问量大导致应用变慢通过请求计数指标就可以定位问题扩容服务器处理负载。4Prometheus组件Prometheus 生态系统由多个组件组成很多组件是可选的Prometheus 主服务器抓取并存储时间序列数据。客户端库用于对应用程序代码做埋点、采集指标。推送网关Pushgateway用于支持短生命周期作业上报指标。专用导出器 Exporter采集 HAProxy、StatsD、Graphite 等各类服务的指标。告警管理器 Alertmanager专门处理告警。各类支持工具。补充大部分 Prometheus 组件使用 Go 语言编写便于编译以静态二进制文件部署。5Prometheus架构Prometheus 采用Pull 拉取模型整体工作流程采集指标→本地存储时序数据→规则处理→告警 / 可视化展示。指标采集Prometheus 直接从提供 HTTP 指标接口的监控目标抓取指标CPU、内存、请求延迟、业务自定义指标。对于短暂生命周期作业、无法直接被抓取的目标作业把指标推送到Pushgateway 推送网关再由 Prometheus 从网关拉取指标。支持服务发现、静态配置两种方式识别监控目标。Prometheus Server 核心处理Retrieval 模块负责抓取指标TSDB 时序数据库将采集到的指标本地磁盘存储按照指标名 标签的组合组织时序数据PromQL 查询引擎提供 PromQL 查询语言执行复杂查询、聚合运算规则引擎定义规则生成新指标、评估告警规则将告警事件推送至 Alertmanager。告警处理告警事件发送到 Alertmanager由它完成告警分组、去重、通知分发输出邮件、PagerDuty 等告警通知。可视化展示Prometheus 自带简单 Web UI一般对接GrafanaGrafana 调用 Prometheus API实现丰富仪表盘、图表可视化。总结Prometheus 收集指标本地存储执行规则告警交给 Alertmanager可视化交给 Grafana。6Prometheus应用场景Prometheus 非常适合记录任何纯数字的时间序列。适用于机器为中心的监控也适用于高度动态的面向服务架构、微服务架构监控多维数据收集与查询是它的突出优势。设计注重可靠性故障期间可以快速诊断问题。每个 Prometheus 服务器独立运行不依赖网络存储或远程服务基础设施其他部分故障时依然可用部署不需要复杂基础设施。7Prometheus不适用场景Prometheus 优先保障可靠性故障下仍可查看系统统计。不适合需要 100% 数据准确性的场景例如按请求计费采集的数据可能不够完整精确。计费统计这类业务建议使用其他系统实现Prometheus 只负责其余监控工作。8Prometheus官方网址https://prometheus.io/docs/introduction/overview/Prometheus部署1环境准备序号主机名IP地址角色1Prometheus-server192.168.100.128/24server2Prometheus-agent192.168.100.129/24agent官方网站https://prometheus.io/2源码包下载平台选择64位下载源码包[rootprometheus-server ~]# wget https://github.com/prometheus/prometheus/releases/download/v3.5.1/prometheus-3.5.1.linux-amd64.tar.gz3安装Prometheus[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[rootprometheus-server prometheus]# vim prometheus.yml- job_name:prometheus-server#23行 监控本地节点名称# metrics_path defaults to /metrics# scheme defaults to http.static_configs: - targets:[192.168.100.128:9090]#29行 设置本地节点IP启动服务[rootprometheus-server prometheus]# nohup ./prometheus --config.fileprometheus.yml [1]1922[rootprometheus-server prometheus]# nohup: 忽略输入并把输出追加到nohup.out查看端口[rootprometheus-server prometheus]# ps -ef | grep prometheusroot19221735020:33 pts/0 00:00:00 ./prometheus--config.fileprometheus.yml4使用systemd管理服务先结束当前运行的进程[rootprometheus-server prometheus]# ps -ef | grep prometheusroot19221735020:33 pts/0 00:00:00 ./prometheus--config.fileprometheus.yml root20121735020:35 pts/0 00:00:00grep--colorauto prometheus[rootprometheus-server prometheus]# kill -9 1922[rootprometheus-server prometheus]# ps -ef | grep prometheusroot21311735020:37 pts/0 00:00:00grep--colorauto prometheus[1] 已杀死nohup./prometheus--config.fileprometheus.yml添加service文件[rootprometheus-server prometheus]# vim /usr/lib/systemd/system/prometheus.service[Service]ExecStart/usr/local/src/prometheus/prometheus--config.file/usr/local/src/prometheus/prometheus.yml[Install]WantedBymulti-user.target[Unit]DescriptionprometheusAfternetwork.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.[rootprometheus-server prometheus]# systemctl status prometheus.service● prometheus.service - prometheus Loaded: loaded(/usr/lib/systemd/system/prometheus.service;enabled;vendor preset: disabled)Active: active(running)since 一2026-08-3120:38:31 CST;5s ago Main PID:2215(prometheus)Tasks:7Memory:18.8M CGroup: /system.slice/prometheus.service └─2215 /usr/local/src/prometheus/prometheus--config.file/usr/local/src/prometheus/prometheus.yml8月3120:38:31 prometheus-server prometheus[2215]:time2026-08-31T20:38:31.78808:00levelINFOsourcehead.go:744…2.126µs8月3120:38:31 prometheus-server prometheus[2215]:time2026-08-31T20:38:31.78808:00levelINFOsourcehead.go:752...tsdb8月3120:38:31 prometheus-server prometheus[2215]:time2026-08-31T20:38:31.78808:00levelINFOsourcehead.go:825…5.967µs8月3120:38:31 prometheus-server prometheus[2215]:time2026-08-31T20:38:31.78808:00levelINFOsourcehead.go:862msg…µs8月3120:38:31 prometheus-server prometheus[2215]:time2026-08-31T20:38:31.79108:00levelINFOsourcemain.go:130...MAGIC8月3120:38:31 prometheus-server prometheus[2215]:time2026-08-31T20:38:31.79108:00levelINFOsourcemain.go:131...rted 8月 31 20:38:31 prometheus-server prometheus[2215]: time2026-08-31T20:38:31.79108:00 levelINFO sourcemain.go:149...s.yml 8月 31 20:38:31 prometheus-server prometheus[2215]: time2026-08-31T20:38:31.81208:00 levelINFO sourcemain.go:1537 msg…ms 8月 31 20:38:31 prometheus-server prometheus[2215]: time2026-08-31T20:38:31.81208:00 levelINFO sourcemain.go:127...sts.8月3120:38:31 prometheus-server prometheus[2215]:time2026-08-31T20:38:31.81208:00levelINFOsourcemanager.go:...ager Hint: Some lines were ellipsized, use-lto showinfull.关闭防火墙和selinux[rootprometheus-server prometheus]# systemctl disable firewalld.service --now[rootprometheus-server prometheus]# setenforce 05访问Prometheus网站http://192.168.100.128:9090/5查看监控目标查看到本节点被监控点击metrics查看指标数据Prometheus监控主机下载监控主机源码包[rootprometheus-server ~]# wget https://github.com/prometheus/node_exporter/releases/download/v1.10.2/node_exporter-1.10.2.linux-amd64.tar.gz1使用exporter监控本地节点[rootprometheus-server ~]# tar zxvf node_exporter-1.10.2.linux-amd64.tar.gznode_exporter-1.10.2.linux-amd64/ node_exporter-1.10.2.linux-amd64/node_exporter node_exporter-1.10.2.linux-amd64/LICENSE node_exporter-1.10.2.linux-amd64/NOTICE[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[Service]ExecStart/usr/local/src/node_exporter/node_exporter[Install]WantedBymulti-user.target[Unit]Descriptionnode_exporterAfternetwork.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.[rootprometheus-server ~]# systemctl status node_exporter.service● node_exporter.service - node_exporter Loaded: loaded(/usr/lib/systemd/system/node_exporter.service;enabled;vendor preset: disabled)Active: active(running)since 一2026-08-3121:59:32 CST;6s ago Main PID:6739(node_exporter)Tasks:4Memory:5.0M CGroup: /system.slice/node_exporter.service └─6739 /usr/local/src/node_exporter/node_exporter修改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:[192.168.100.128:9100]#端口9100重启Prometheus服务[rootprometheus-server ~]# systemctl restart prometheus.service打开页面查看target监控到目标2使用exporter监控其他节点关闭防火墙及selinux[rootprometheus-agent ~]# systemctl disable firewalld.service --now[rootprometheus-agent ~]# setenforce 0解压软件包[rootprometheus-agent ~]# wget https://github.com/prometheus/node_exporter/releases/download/v1.10.2/node_exporter-1.10.2.linux-amd64.tar.gz[rootprometheus-agent ~]# tar zxvf node_exporter-1.10.2.linux-amd64.tar.gznode_exporter-1.10.2.linux-amd64/ node_exporter-1.10.2.linux-amd64/node_exporter node_exporter-1.10.2.linux-amd64/LICENSE node_exporter-1.10.2.linux-amd64/NOTICE[rootprometheus-agent ~]# mv node_exporter-1.10.2.linux-amd64 /usr/local/src/node_exporter添加service文件[rootlocalhost ~]# vim /usr/lib/systemd/system/node_exporter.service[Service]ExecStart/usr/local/src/node_exporter/node_exporter[Install]WantedBymulti-user.target[Unit]Descriptionnode_exporterAfternetwork.target重启服务[rootprometheus-agent ~]# systemctl daemon-reload[rootprometheus-agent ~]# 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.[rootprometheus-agent ~]# systemctl status node_exporter.service● node_exporter.service - node_exporter Loaded: loaded(/usr/lib/systemd/system/node_exporter.service;enabled;vendor preset: disabled)Active: active(running)since 一2026-08-3123:23:09 CST;5s ago Main PID:30985(node_exporter)Tasks:4Memory:4.9M CGroup: /system.slice/node_exporter.service └─30985 /usr/local/src/node_exporter/node_exporter回到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:[192.168.100.129:9100]重启Prometheus服务[rootprometheus-server ~]# systemctl restart prometheus.serviceGrafana展现数据grafana介绍Grafana 是一个开源的可视化和监控工具能把各种数据源比如 Prometheus、InfluxDB、MySQL的指标和日志统一展示在酷炫的仪表盘上特别适合监控系统状态和分析业务数据。核心功能数据统一 不用把数据都导入一个地方它能直接连上现有数据源比如云服务、数据库甚至 Google Sheets统一可视化。灵活仪表盘 支持多种图表类型折线图、热力图等还能自定义查询和告警团队协作和分享也方便。多数据源支持 兼容 Prometheus、MySQL、Elasticsearch 等主流数据源。软件包下载选择downloads点击开始使用Grafana进入下载页面选择开源下载进入页面后选择最新版本开源OSSLinux系统开启新节点进行部署,注意关闭防火墙[rootprometheus-agent ~]# tar zxvf grafana_12.3.1_20271043721_linux_amd64.tar.gz启动服务[rootprometheus-agent ~]# nohup /usr/local/src/grafana/bin/grafana-server --config/usr/local/src/grafana/conf/defaults.ini--homepath/usr/local/src/grafana [1]35006[rootprometheus-agent ~]# nohup: 忽略输入并把输出追加到nohup.out[rootprometheus-agent ~]# ps -ef | grep grafanaroot35006287419923:47 pts/0 00:00:07 grafana server--config/usr/local/src/grafana/conf/defaults.ini--homepath/usr/local/src/grafana root3503728741023:47 pts/0 00:00:00grep--colorauto grafana查看3000端口[rootprometheus-agent ~]# ss -anput | grep 3000tcp LISTEN0128[::]:3000[::]:* users:((grafana,pid35006,fd15))使用systemd启动[rootprometheus-agent ~]# vim /usr/lib/systemd/system/grafana.service[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[Unit]DescriptiongrafanaAfternetwork.target[rootprometheus-agent ~]# systemctl daemon-reload[rootprometheus-agent ~]# systemctl enable grafana.service --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/grafana.service to /usr/lib/systemd/system/grafana.service.[rootprometheus-agent ~]# systemctl status grafana.service● grafana.service - grafana Loaded: loaded(/usr/lib/systemd/system/grafana.service;enabled;vendor preset: disabled)Active: active(running)since 一2026-08-3123:50:38 CST;5s ago Main PID:35627(grafana)Tasks:9Memory:84.8M CGroup: /system.slice/grafana.service └─35627 grafana server--config/usr/local/src/grafana/conf/defaults.ini--homepath/usr/local/src/grafana访问网站http://192.168.100.129:3000用户名admin密码admin跳过重置密码进入首页添加数据源选择prometheus数据源添加连接prometheus-server点击测试完成通过点击save保存然后再点击Data sources添加仪表盘官网地址https://grafana.com/grafana/dashboards/选择对主机进行监控的仪表盘输入8919回到grafana网页界面点击dashboards-new-import输入8919点击load加载选择之前添加的prometheus数据源点击import可以看到主机状态