本指南是一个 “Hello World” 风格的教程,展示了如何安装、配置和使用一个简单的 Prometheus 实例。你将本地下载并运行 Prometheus,配置它抓取自身和一个示例应用程序,然后使用查询、规则和图形来使用收集的时间序列数据。
下载适用于你平台的最新版本 的 Prometheus,然后解压并运行它
tar xvfz prometheus-*.tar.gz
cd 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 二进制文件的目录并运行
# 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 的内置表达式浏览器,请导航到 https://127.0.0.1:9090/graph 并在 “Graph” 选项卡中选择 “Table” 视图。
正如你可以从 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)
有关表达式语言的更多信息,请参阅 表达式语言文档。
要绘制表达式图形,请导航到 https://127.0.0.1:9090/graph 并使用 “Graph” 选项卡。
例如,输入以下表达式以绘制在自抓取的 Prometheus 中每秒创建的 chunks 的速率
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
你现在应该有示例目标监听在 https://127.0.0.1:8080/metrics、https://127.0.0.1:8081/metrics 和 https://127.0.0.1:8082/metrics 上。
现在我们将配置 Prometheus 来抓取这些新目标。让我们将所有三个端点分组到一个名为 node
的作业中。我们将假设前两个端点是生产目标,而第三个端点代表一个 canary 实例。为了在 Prometheus 中建模,我们可以将几个端点组添加到单个作业中,并为每个目标组添加额外的标签。在本例中,我们将为第一组目标添加 group="production"
标签,为第二组目标添加 group="canary"
。
为了实现这一点,请将以下作业定义添加到你的 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 可以通过配置的记录规则将表达式预先记录到新的持久化时间序列中。假设我们有兴趣记录在 5 分钟窗口内测量的每个实例的所有 cpu 的平均每秒 cpu 时间速率 (node_cpu_seconds_total
)(但保留 job
、instance
和 mode
维度)。我们可以这样写
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'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
- 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,并通过表达式浏览器查询或绘制图形来验证是否可以使用指标名称为 job_instance_mode:node_cpu_seconds:avg_rate5m
的新时间序列。
正如在 配置文档 中提到的,Prometheus 实例可以在不重启进程的情况下重新加载其配置,方法是使用 SIGHUP
信号。如果你在 Linux 上运行,这可以通过使用 kill -s SIGHUP <PID>
来执行,将 <PID>
替换为你的 Prometheus 进程 ID。
虽然 Prometheus 在进程突然失败的情况下确实有恢复机制,但建议使用信号或中断来干净地关闭 Prometheus 实例。在 Linux 上,这可以通过向 Prometheus 进程发送 SIGTERM
或 SIGINT
信号来完成。例如,你可以使用 kill -s <SIGNAL> <PID>
,将 <SIGNAL>
替换为信号名称,将 <PID>
替换为 Prometheus 进程 ID。或者,你可以在控制终端按下中断字符,默认情况下是 ^C
(Control-C)。
这篇文档是 开源的。请通过提交 issue 或 pull request 来帮助改进它。