前言
在现代分布式系统和云原生环境中,为了确保复杂的分布式系统和服务的高可用性、可靠性和性能,通常采用实时可视化监控和分析,实现故障快速响应、资源优化和安全保障,从而提升用户满意度和运营效率。
在目前主流的解决方案中,Prometheus
和 Grafana
是两个使用 Go 开发的强大开源工具,通常一起使用来实现全面的监控和可视化系统。其中Prometheus
负责采集和存储监控对象的时序数据。
架构
Prometheus是一个开源系统监控和警报工具包,最初由SoundCloud构建。自2012年成立以来,许多公司和组织都采用了Prometheus,该项目拥有非常活跃的开发人员和用户社区。https://prometheus.io/docs/introduction/overview 。
Prometheus的主要特点是:
- 采用一种多维数据模型,包含由
metric name
(指标名称)和k/v
(键值对)标识的时间序列数据比如我们在对河水水质的抽样检测中,【大肠杆菌检测】和【有害化学物质检测】样本就可以类比成是两个
metric
指标名称;为了了解【大肠杆菌检测】的水质结果,我们还需要一些具体的指标数据来描述这个样本,如:检测时间(time=‘2024-01-02 03:04:05’)、检测位置(position=‘河流上游’)、大肠杆菌含量(proportion=‘0.02’)这几组数据可以类比成是几组k/v
键值对。 - 内置了一个灵活、强大的数据查询语言
PromQL
查询关系型数据库数据用 SQL 语言,那查询指标时序数据用的就是专属的
PromQL
语言,其具备和 SQL 一样的范围查询、分类和聚合等功能。 - 不依赖分布式存储,单个服务器节点是自治的
Prometheus 核心部分只有一个单独的二进制文件,不存在任何的第三方依赖(数据库,缓存等等)。唯一需要的就是本地磁盘,因此不会有潜在级联故障的风险。但 Prometheus 也有远程读写功能,能将数据同时存储到其他中间件。
- 通过HTTP上的拉模型进行时序数据收集
Prometheus 基于 Pull 模型的架构方式定时轮询的拉取收集时序数据,这意味着可以从任何位置、任何类型实现了该数据协议的系统或程序中通过 HTTP 采集到指标数据。
- 通过中间网关支持推送时序数据
对于短作业类型程序,程序可以在退出时主动 Push 推送指标数据到 Prometheus 实现监控
- 通过服务发现或静态配置发现目标
- 多种图形和仪表板模式支持
Prometheus 生态系统由多个组件组成,其中许多组件是可选的:
prometheus server
:Prometheus 主服务器用于抓取和存储时间序列数据client libraries
:客户端库是某种语言(例如Go,Java,Python,Ruby)的库,可以通过编写自定义收集器以从其他系统中轻松提取指标并将指标公开给Prometheuspush gateway
:一个能够支持短时任务的推送网关exporters
:为各类服务提供的特殊数据导出器,比如 HAProxy、StatsD、Graphite 等alertmanager
:专门用来处理报警- 以及支持其他各式工具
下图展示了Prometheus的架构及其生态系统的一些组件:
对于监控系统而言,大量的监控任务必然导致有大量的数据产生。而 Prometheus 可以高效地处理这些数据,对于单一 Prometheus Server 实例而言它就可以处理数以百万的监控指标和每秒处理数十万的数据点。
Prometheus 自身提供了一定的可视化能力,且可以和很多流行的可视化工具进行整合如:Grafana。
Prometheus Server 安装
下载适用于您的平台的 Prometheus的最新版本:https://prometheus.io/download ,然后将其解压缩:
tar xvfz prometheus-*.tar.gz
cd prometheus-*
Prometheus服务器是一个名为 prometheus
(或Microsoft Windows上的 prometheus.exe
)的二进制文件。且下载的目录中附带了一个名为 prometheus.yml
的文件中的示例配置:
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=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
示例配置文件中有三个配置块:global
、rule_files
和 scrape_configs
。global块控制Prometheus服务器的全局配置;rule_files
块指定了 Prometheus 的一些特殊规则配置文件路径;最后一个块 scrape_configs
控制 Prometheus 监视的资源。
有关配置选项的完整规范,请参阅配置文档:https://prometheus.io/docs/operating/configuration
Prometheus 服务器是允许使用--web.enable-lifecycle
参数在运行时动态加载配置文件变更的,最后使用配置文件来启动Prometheus服务器:
# By default, Prometheus stores its database in ./data (flag --storage.tsdb.path).
./prometheus --config.file=prometheus.yml --web.enable-lifecycle
Prometheus实例可以使用
SIGHUP
信号重新加载其配置,而无需重新启动进程。如果你运行在Linux上,这可以通过使用kill -s SIGHUP <PID>
来执行,用你的Prometheus进程ID替换<PID>
。
由于 Prometheus 还将自身的数据作为HTTP端点公开,因此它可以抓取和监控自己的健康状况。在默认配置中,有一个名为prometheus
的job
,用于抓取Prometheus服务器公开的时间序列数据。该job包含一个静态配置的目标,即localhost
上的9090
端口。Prometheus通过目标上的/metrics
路径拉取数据。因此,此默认作业是通过URL进行抓取:http://localhost:9090/metrics
。
在服务器启动后,你可以通过http://localhost:9090
访问 Prometheus 自带的 Web UI 管理后台:
在该页面下,可以看到已经正常监控到配置文件中的 Prometheus 服务自身的各项运行指标数据。通过http://localhost:9090/metrics
查看采集到的 Prometheus服务器原始指标数据:
概念和术语
上述虽然已经成功启动了 Prometheus 服务器实现对数据的采集,但 Prometheus 本质是一个数模模型和运算的工具,要想顺利理解 Prometheus,进一步看懂和使用上图的Prometheus管理工具系统或能根据自己的需求场景查询、读懂监控数据,还需要先了解 Prometheus 的一些基本术语和概念。
术语表
术语 | 解释 |
---|---|
Exporter | 导出器是一个二进制文件,与您想要从中获取指标的应用程序一起运行。导出器公开Prometheus指标,通常通过将以非Prometheus格式公开的指标转换为Prometheus支持的格式。 |