本指南是一个 "Hello World" 风格的教程,展示如何安装、配置和使用一个简单的 Prometheus 实例。你将在本地下载并运行 Prometheus,配置它来抓取自身和一个示例应用程序的指标,然后使用查询、规则和图表来处理收集到的时间序列数据。
下载并运行 Prometheus
为你的平台下载最新版本的 Prometheus,然后解压并运行:
tar xvfz prometheus-*.tar.gz
cd prometheus-*
在启动 Prometheus 之前,让我们先对其进行配置。
配置 Prometheus 监控自身
Prometheus 通过抓取目标的 HTTP 指标端点来收集指标数据。由于 Prometheus 也以相同的方式暴露自身的数据,因此它也可以抓取和监控自身的健康状况。
虽然一个只收集自身数据的 Prometheus 服务器并不是很实用,但它是一个很好的入门示例。将以下基本的 Prometheus 配置保存为名为 prometheus.yml 的文件:
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# 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'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
有关配置选项的完整说明,请参阅配置文档。
启动 Prometheus
要使用新创建的配置文件启动 Prometheus,请切换到包含 Prometheus 二进制文件的目录并运行:
# Start Prometheus.
# By default, Prometheus stores its database in ./data (flag --storage.tsdb.path).
./prometheus --config.file=prometheus.yml
Prometheus 应该会启动成功。你还可以在 localhost:9090 浏览关于其自身的状态页面。给它几秒钟时间从自身的 HTTP 指标端点收集数据。
你也可以通过访问其指标端点来验证 Prometheus 正在提供关于自身的指标:localhost:9090/metrics
使用表达式浏览器
让我们来探索 Prometheus 收集到的关于自身的数据。要使用 Prometheus 内置的表达式浏览器,请导航到 http://localhost:9090/query 并选择 "Graph" 标签页。
从 localhost:9090/metrics 可以看到,Prometheus 导出的关于自身的一个指标名为 prometheus_target_interval_length_seconds(目标抓取之间的实际时间间隔)。在表达式控制台中输入以下内容,然后点击 "Execute":
prometheus_target_interval_length_seconds
这应该会返回多个不同的时间序列(以及每个序列的最新记录值),每个序列的指标名称都是 prometheus_target_interval_length_seconds,但具有不同的标签。这些标签表示不同的延迟百分位数和目标组间隔。
如果我们只对第 99 百分位的延迟感兴趣,可以使用以下查询:
prometheus_target_interval_length_seconds{quantile="0.99"}
要统计返回的时间序列数量,可以编写:
count(prometheus_target_interval_length_seconds)
有关表达式语言的更多信息,请参阅表达式语言文档。
使用图表界面
要绘制表达式的图表,请导航到 http://localhost:9090/query 并使用 "Graph" 标签页。
例如,输入以下表达式来绘制 Prometheus 自身抓取中每秒创建的 chunk 速率:
rate(prometheus_tsdb_head_chunks_created_total[1m])
你可以尝试调整图表的时间范围参数和其他设置。
启动一些示例目标
让我们添加更多目标供 Prometheus 抓取。
这里使用 Node Exporter 作为示例目标,有关其使用的更多信息,请参阅这些说明。
tar -xzvf node_exporter-*.*.tar.gz
cd node_exporter-*.*
# Start 3 example targets in separate terminals:
./node_exporter --web.listen-address 127.0.0.1:8080
./node_exporter --web.listen-address 127.0.0.1:8081
./node_exporter --web.listen-address 127.0.0.1:8082
现在你应该有示例目标分别监听在 http://localhost:8080/metrics、http://localhost:8081/metrics 和 http://localhost:8082/metrics。
配置 Prometheus 监控示例目标
现在我们来配置 Prometheus 抓取这些新目标。让我们将所有三个端点分组到一个名为 node 的 job 中。我们假设前两个端点是生产目标,而第三个代表金丝雀实例。为了在 Prometheus 中对此建模,我们可以将多组端点添加到一个 job 中,并为每组目标添加额外的标签。在这个示例中,我们将为第一组目标添加 group="production" 标签,为第二组添加 group="canary" 标签。
要实现这一点,请将以下 job 定义添加到 prometheus.yml 的 scrape_configs 部分,并重启 Prometheus 实例:
scrape_configs:
- job_name: 'node'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:8080', 'localhost:8081']
labels:
group: 'production'
- targets: ['localhost:8082']
labels:
group: 'canary'
转到表达式浏览器,验证 Prometheus 现在已经有了这些示例端点暴露的时间序列信息,例如 node_cpu_seconds_total。
配置规则将抓取的数据聚合为新的时间序列
虽然在我们的示例中不是问题,但在实时计算时,跨越数千个时间序列的聚合查询可能会变得很慢。为了提高效率,Prometheus 可以通过配置的_记录规则_将表达式预先记录到新的持久化时间序列中。假设我们想记录所有实例上每秒 CPU 时间(node_cpu_seconds_total)的平均速率(但保留 job、instance 和 mode 维度),在 5 分钟的窗口内测量。我们可以这样编写:
avg by (job, instance, mode) (rate(node_cpu_seconds_total[5m]))
尝试绘制这个表达式的图表。
要将这个表达式产生的时间序列记录到一个名为 job_instance_mode:node_cpu_seconds:avg_rate5m 的新指标中,请创建一个包含以下记录规则的文件,并保存为 prometheus.rules.yml:
groups:
- name: cpu-node
rules:
- record: job_instance_mode:node_cpu_seconds:avg_rate5m
expr: avg by (job, instance, mode) (rate(node_cpu_seconds_total[5m]))
要让 Prometheus 加载这个新规则,请在 prometheus.yml 中添加 rule_files 语句。配置现在应该如下所示:
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
evaluation_interval: 15s # Evaluate rules every 15 seconds.
# Attach these extra labels to all timeseries collected by this Prometheus instance.
external_labels:
monitor: 'codelab-monitor'
rule_files:
- 'prometheus.rules.yml'
scrape_configs:
- job_name: 'prometheus'