一、介绍
1.什么是Prometheus?
- Prometheus(普罗米修斯)是一个最初在SoundCloud上构建的监控系统。自2012年成为社区开源项目,拥有非常活跃的开发人员和用户社区。为强调开源及独立维护,Prometheus于2016年加入云原生云计算基金会(CNCF),成为继Kubernetes之后的第二个托管项目。
- 一套数据监控解决方案。它能让你随时掌控系统的运行状态,快速定位出现问题的位置,快速排除故障(对于中小规模的团队来说,可以极大地减少成本,加快研发速度)。
项目地址
https://github.com/prometheus
2.Prometheus的特点有什么?
- 多维数据模型:由度量名称和键值对标识的时间序列数据
- PromQL:一种灵活的查询语言,可以利用多维数据完成复杂的查询
- 不依赖分布式存储,单个服务器节点可直接工作
- 基于HTTP的pull方式采集时间序列数据
- 推送时间序列数据通过PushGateway组件支持
- 通过服务发现或静态配置发现目标
- 多种图形模式及仪表盘支持(grafana)
3.Prometheus的组件都有哪些?
下面两张图说明了Prometheus的整体架构,以及一些组件作用:


- Prometheus Server 用于定时抓取数据指标(metrics)、存储时间序列数据(TSDB)
- Jobs/exporter 收集被监控端数据并暴露指标给Prometheus
- Pushgateway 监控端的数据会用push的方式主动传给此组件,随后被Prometheus 服务定时pull此组件数据即可
- Alertmanager 报警组件,可以通过邮箱、微信等方式
- Web UI 用于多样的UI展示,一般为Grafana
- 还有一些例如配置自动发现目标的小组件和后端存储组件
简单地说,Prometheus 的实现架构也并不复杂。其实就是收集数据、处理数据、可视化展示,再进行数据分析进行报警处理。
但其珍贵之处在于提供了一整套可行的解决方案,并且形成了一整个生态,能够极大地降低我们的研发成本。
4.什么时候使用Prometheus
- 监控的对象动态可变,无法预先配置的时候
- Prometheus 是专为云环境(k8s/docker)提供的监控工具
- 想要更直观更简单的直接观察某项指标的数据变化时
Prometheus官网 是用「From metrics to insight」(用指标洞察系统的意思)描述了 Prometheus 的用途。
Prometheus 究竟能做什么?
- 对于运维人员来说,他们需要监控机器的 CPU、内存、硬盘的使用情况,以此来保证运行在机器上的应用的稳定性。
- 对于研发人员来说,他们关注某个异常指标的变化情况,从而来保证业务的稳定运行。
- 对于产品或运营来说,他们更关心产品层面的事情,例如:某个活动参加人数的增长情况,活动积分的发放情况。
对于上面说到的这些功能,Prometheus 都能够实现。Prometheus 能根据这些收集的数据实现告警功能。
二、部署
从 https://prometheus.io/download/ 找到最新版本的Prometheus Sevrer软件包:
访问地址:http://IP:9090
• --web.listen-address= "0.0.0.0:9090" # 监听地址和端口
• --log.level=info # 日志级别
• --alertmanager.timeout=10s # 与报警组件的超时时间
• --storage.tsdb.path="data/ " # 数据目录
• --storage.tsdb.retention.time=15d # 数据保存时间,默认15天
输入 http://localhost:9090/graph 可以看到如下页面,这个是 Prometheus 自带的监控管理界面。

三、配置文件说明
global: 全局配置(如果有内部单独设定,会覆盖这个参数)
alerting: 告警插件定义。这里会设定alertmanager这个报警插件。
rule_files: 告警规则。 按照设定参数进行扫描加载,用于自定义报警规则,其报警媒介和route路由由alertmanager插件实现。
scrape_configs:采集配置。配置数据源,包含分组job_name以及具体target。又分为静态配置和服务发现
原始配置文件内容
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
1.global指标说明
# my global config
global:
scrape_interval: 15s # 默认15s 全局每次数据收集的间隔
evaluation_interval: 15s # 规则扫描时间间隔是15秒,默认不填写是 1分钟
scrape_timeout: 5s #超时时间
external_labels: # 用于外部系统标签的,不是用于metrics(度量)数据
2.alerting说明
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
这里定义和prometheus集成的alertmanager插件,用于监控报警。后续会单独进行alertmanger插件的配置、配置说明、报警媒介以及route路由规则记录。
3.rule_files说明
这个主要是用来设置告警规则,基于设定什么指标进行报警(类似触发器trigger)。这里设定好规则以后,prometheus会根据全局global设定的evaluation_interval参数进行扫描加载,规则改动后会自动加载。其报警媒介和route路由由alertmanager插件实现。
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
4.scrape_configs配置采集目标endpoints
scrape_configs 默认规则:
scrape_configs:
# The job name is added as a label `job=` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
- job_name: 任务目标名,可以理解成分组,每个分组包含具体的target组员。
- scrape_interval: 5s #这里如果单独设定的话,会覆盖global设定的参数,拉取时间间隔为5s
- metrics_path # 监控项访问的url路径,https://prometheus.21yunwei.com/metrics【通过前端web做了反向代理到后端】
- targets: Endpoint # 监控目标访问地址
说明:上述为静态规则,没有设置自动发现。这种情况下增加主机需要自行修改规则,通过supervisor reload 对应任务,也是缺点:每次静态规则添加都要重启prometheus服务,不利于运维自动化。
四、总结
对于工作多年的研发人员,对自己负责的系统必须了然于胸。而要做到了然于胸,单靠一味的自信是不够的,必须借助一套功能强大的业务监控系统。Prometheus 作为这其中的翘楚,凭借其完善的功能、海量数据支持,可以让我们较低成本地实现业务监控。