什么是Prometheus?

Prometheus 是一个开源的系统监控和警报工具包,最初由 SoundCloud 开发,现为云原生计算基金会(CNCF)的毕业项目。它采用拉取式(Pull-based)模型,通过 HTTP 协议周期性地从被监控的目标(称为 Exporter)抓取指标数据,并将其存储在本地的时间序列数据库中。Prometheus 的设计特别适合监控动态的云原生环境,如 Kubernetes 集群和微服务架构。
核心定位
专注指标(Metrics)监控,采集、存储、查询与告警系统 / 服务的时序数据,适配微服务、K8s、容器等动态分布式环境。
核心特性
- 多维数据模型:时序数据 = 指标名 + 标签(key=value)+ 时间戳 + 数值,支持灵活过滤 / 聚合Prometheus。
- Pull 采集为主:主动 HTTP 拉取指标;短生命周期任务可通过 PushGateway 推送Prometheus。
- PromQL 查询语言:强大的时序查询、聚合、计算能力。
- 内置时序数据库(TSDB):高效存储与查询,单机自治、不依赖分布式存储。
- 服务发现:支持静态配置、K8s、Consul 等自动发现监控目标。
- 告警能力:内置 Alertmanager,支持告警路由、去重、通知(邮件 / 钉钉 / 企业微信等)。
核心组件
- Prometheus Server:核心,负责拉取、存储、查询与告警规则计算。
- Exporter:将第三方服务(如 MySQL、Redis、主机)指标转为 Prometheus 格式。
- PushGateway:接收短任务推送的指标,供 Server 拉取Prometheus。
- Alertmanager:处理告警、去重、抑制、通知Prometheus。
- Client Libraries:嵌入应用,暴露自定义业务指标Prometheus。
典型指标类型
- Counter:只增不减(如 HTTP 请求总数、错误数)。
- Gauge:可增可减(如内存占用、CPU 使用率)。
- Histogram/Summary:统计分布(如接口响应延迟、请求耗时)。
生态与常用搭配
- 可视化:Grafana(构建仪表盘)。
- 长期存储:Thanos、Cortex、M3DB 等。
- 日志 / 链路:配合 Loki、Jaeger 实现 "监控 + 日志 + 链路" 全链路可观测。
Docker使用命令
docker pull 获取镜像
如果我们本地没有 prometheus 镜像,我们可以使用 docker pull 命令来载入 prometheus 镜像:
bash
$ docker pull prom/prometheus
docker run 启动容器
以下命令使用 prometheus 镜像启动一个容器,参数为以命令行模式进入该容器:
bash
$ docker run -itd --name=prometheus --restart=always -p 9090:9090 prom/prometheus
安装步骤
以下以"监控服务器指标"为例,介绍 Prometheus 的安装流程。
1. 安装 Prometheus Server
通过 Docker 快速部署:
bash
yangyanping@yangyaningdeAir bin % docker pull prom/prometheus
Using default tag: latest
latest: Pulling from prom/prometheus
0d1d799270c5: Pull complete
7938917f4b9d: Pull complete
ef445d38f2c6: Pull complete
a2f967955733: Pull complete
c2d2d0563ae3: Pull complete
1dccce9f415d: Pull complete
e5d54fbf8ee1: Pull complete
37404d8f503a: Pull complete
d23996ce6dd9: Pull complete
3cc769d96815: Pull complete
Digest: sha256:1f0f50f06acaceb0f5670d2c8a658a599affe7b0d8e78b898c1035653849a702
Status: Downloaded newer image for prom/prometheus:latest
docker.io/prom/prometheus:latest
yangyanping@yangyaningdeAir bin % mkdir -p /data/prometheus
yangyanping@yangyaningdeAir bin %
yangyanping@yangyaningdeAir bin %
yangyanping@yangyaningdeAir bin % docker run -itd --name=prometheus --restart=always -p 9090:9090 prom/prometheus
74602ea06c8cb54d67ee38c2ac58891cd5e76239cf4d34dab8d9f64510866e2c
yangyanping@yangyaningdeAir bin %
容器创建成功后,可通过 http://服务器IP:9100

2.安装 Grafana
Grafana 是一个跨平台开源的度量分析和可视化工具,可以通过将采集的数据查询后可视化展示,并支持及时通知。
默认账户密码:admin/admin
bash
# 拉取镜像
yangyanping@yangyaningdeAir bin % docker pull grafana/grafana
Using default tag: latest
latest: Pulling from grafana/grafana
d8ad8cd72600: Pull complete
75c01d8d80b6: Pull complete
6a3f88e6196e: Pull complete
4f4fb700ef54: Pull complete
7b1a69e8b4bd: Pull complete
55b6a5e3acd3: Pull complete
c3549c1604e4: Pull complete
9a83ba4b179c: Pull complete
fabd13cce4d7: Pull complete
796e7a91af38: Pull complete
Digest: sha256:9e1e77ade304069aee3196e9a4f210830e96e80ce9a2640891eccc324b152faf
Status: Downloaded newer image for grafana/grafana:latest
docker.io/grafana/grafana:latest
# 启动容器
yangyanping@yangyaningdeAir bin % docker run -d --name=grafana -p 3000:3000 grafana/grafana
