系统监控Prometheus之Docker安装部署Prometheus

什么是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  
相关推荐
云边云科技_云网融合5 分钟前
工业制造多厂区、供应链远程数据传输网络如何优化?
运维·服务器·网络
张毅2004-10-106 分钟前
docker详解:安装docker,docker架构,docker镜像、容器、网络、存储,容器监控,容器日志,综合实验
linux·运维·docker·云原生·云计算
笑一下蒜了.14 分钟前
Linux 存储多路径访问手册|multipath.conf 各配置段详解
android·linux·运维
营养充电站19 分钟前
C++23 新特性在 CLion 中的实战体验
macos·docker·jupyter·正则表达式
文优25 分钟前
Linux 常用命令速查手册(工作版)
linux·运维·docker
小叮当爱咖啡34 分钟前
Day1.环境+自动化脚本
运维·自动化
小狼嚎月36 分钟前
K8s PV / PVC / StorageClass 有状态应用持久化
云原生·容器·kubernetes
云飞云共享云桌面42 分钟前
非标装备厂降本方案|8‑10 人三维研发共用一台主机,兼容SolidWorks等多款 3D 绘图软件
运维·服务器·3d·自动化·负载均衡·制造
AAA@峥1 小时前
K8s 资源混乱怎么办?Namespace 隔离 + 上下文切换实战
容器·贪心算法·kubernetes
云边有个稻草人1 小时前
Ubuntu部署Prometheus与Alertmanager:服务接入和远程监控
数据库·ubuntu·prometheus