快速开始
本指南是一份“Hello World”风格的教程,它展示了如何安装、配置和使用简单的 Prometheus 实例的详细过程。你将下载、运行本地的 Prometheus,并配置它来抓取自身及示例应用的指标,然后对收集到的时间序列数据进行查询、规则计算和图表化操作。
下载和运行 Prometheus
从这里下载最新版本的 Prometheus,然后解压并运行:
tar xvfz prometheus-*.tar.gzcd prometheus-*
在启动 Prometheus 之前,让我们先进行一些配置。
配置 Prometheus 监控自身
Prometheus 通过抓取指标 HTTP 端点来收集来自 _Target(采集目标)_的指标。由于 Prometheus 以相同的方式暴露自身数据,因此它也可以抓取自身暴露的指标和监控自身的健康状况。
虽然只收集自身数据的 Prometheus 服务器用处不大,但它却是一个很好的上手教程。请将以下基本 Prometheus 配置保存为名为prometheus.yml
的文件:
global: scrape_interval: 15s # 默认每15秒抓取一次 Target。
# 将这些标签附加到与外部系统通信时(联邦、远程存储、Alertmanager)的任何时间序列或告警。 external_labels: monitor: 'codelab-monitor'
# 配置恰好包含一个要抓取的端点的抓取配置,在此处是 Prometheus 本身scrape_configs: # 采集任务名称作为`job=<job_name>`标签被添加到从此配置中抓取的任何时间序列。 - job_name: 'prometheus'
# 覆盖全局默认设置,每5秒抓取一次 Target。 scrape_interval: 5s
static_configs: - targets: ['localhost:9090']
对于配置选项的完整规范,请参阅配置文档。
启动 Prometheus
使用新创建的配置文件启动 Prometheus,切换到包含 Prometheus 二进制文件的目录后运行:
# 启动Prometheus。# 默认情况下,Prometheus 将数据库存储在 ./data(--storage.tsdb.path)。./prometheus --config.file=prometheus.yml
现在 Prometheus 应该启动成功。你也应该能够浏览有关其自身的状态页面 localhost:9090。给它几秒钟时间从其自己的 HTTP 指标端点收集数据。
你还可以通过跳转至其指标端点验证 Prometheus 是否在提供自身的指标:localhost:9090/metrics
使用表达式浏览器
让我们探索一下 Prometheus 收集的有关自身运行状态的数据。要使用 Prometheus 内置的表达式浏览器,请跳转至 http://localhost:9090/graph 并在“Graph”标签页内选择“Table”视图。
如 localhost:9090/metrics 给出的数据所示,Prometheus 导出的一个自监控指标是prometheus_target_interval_length_seconds
(抓取 Target 的实际时间间隔)。输入以下内容到表达式控制台,然后点击“Execute”:
prometheus_target_interval_length_seconds
这应该返回多个不同的时间序列(以及每个时间序列记录的最新值),每个都带有指标名称prometheus_target_interval_length_seconds
,但具有不同的标签。这些标签区分不同的百分位数和 Target 实际的抓取间隔。
如果我们仅对99%的延迟感兴趣,可以使用此查询:
prometheus_target_interval_length_seconds{quantile="0.99"}
要计算返回的时间序列数量,你可以这样编写表达式:
count(prometheus_target_interval_length_seconds)
有关表达语言的更多信息,请参阅表达式语言文档。
使用绘图界面
要在绘图界面中绘制表达式,请跳转至 http://localhost:9090/graph 并点击“Graph”标签页。
例如,输入以下表达式以绘制 Prometheus 数据块的创建速率:
rate(prometheus_tsdb_head_chunks_created_total[1m])
试一试自己调整绘图范围参数和其他设置,观察具体效果。
启动一些示例 Target
让我们添加额外的 Target 以供 Prometheus 抓取。
下面以 Node Exporter 作为例子。有关这个例子的更多信息,请参见这里。
tar -xzvf node_exporter-*.*.tar.gzcd node_exporter-*.*
# 在单独的终端中,分别启动3个示例 Target :./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
现在应该有3个 Target 在 http://localhost:8080/metrics、http://localhost:8081/metrics 和 http://localhost:8082/metrics 上监听。
配置 Prometheus 监控示例 Target
现在我们将配置 Prometheus 来抓取这些新的 Target。让我们将所有三个端点分组到一个名为node
的采集任务中。我们假设前两个端点是生产实例,而第三个端点代表了一个灰度实例。为了在 Prometheus 中模拟这一点,我们可以向一组端点添加额外的标签。在这个例子中,我们将group="production"
标签添加到第一个组的端点上,同时将group="canary"
添加到第二个组的端点上。
要实现这一点,在prometheus.yml
的scrape_configs
部分中添加以下采集任务定义并重启 Prometheus 实例:
scrape_configs: - job_name: 'node'
# 覆盖全局默认设置,并每5秒从此作业抓取 target。 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 可以通过配置记录规则(recording rules)将表达式转换为持久化的新时间序列。假设我们对每个实例(但保留job
、instance
和mode
维度)在过去5分钟内平均的 CPU 时间(node_cpu_seconds_total
)的每秒变化率感兴趣。我们可以这样写:
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 # 默认每15秒抓取一次 target。 evaluation_interval: 15s # 每15秒计算一次规则。
# 将这些额外标签附加到由此 Prometheus 实例收集的所有时间序列。 external_labels: monitor: 'codelab-monitor'
rule_files: - 'prometheus.rules.yml'
scrape_configs: - job_name: 'prometheus'
# 覆盖全局默认设置,每5秒从 target 进行一次抓取。 scrape_interval: 5s
static_configs: - targets: ['localhost:9090']
- job_name: 'node'
# 覆盖全局默认设置,每5秒从 Target 进行一次抓取。 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>
来执行配置加载。
优雅地关闭你的 Prometheus 实例
虽然 Prometheus 在发生突然故障时具有一定的恢复机制,但我们还是建议使用SIGTERM
信号来安全地关闭 Prometheus 实例。如果你正在运行 Linux,则可以通过使用kill -s SIGTERM <PID>
执行此操作,将<PID>
替换为你的 Prometheus PID。
该文档基于 Prometheus 官方文档翻译而成。