Prometheus 入门指南

这是Prometheus官方文档的“入门指南”,以下是该网页内容的中文翻译:

入门指南 | Prometheus

本指南是一个“Hello World”风格的教程,展示了如何安装、配置和使用一个简单的Prometheus实例。你将下载并本地运行Prometheus,配置它来抓取自身和一个示例应用程序的数据,然后使用查询、规则和图表来使用收集到的时间序列数据。

下载并运行Prometheus

下载适用于你平台的最新版本的Prometheus,然后解压并运行它:

1
2
3
4
5
6
7
8
9
10
11
12
tar xvfz prometheus-*.tar.gz
cd prometheus-*
```

在启动Prometheus之前,我们先来配置它。

配置Prometheus监控自身

Prometheus通过抓取目标的HTTP指标端点来收集指标。由于Prometheus以相同的方式暴露关于自身的数据,它也可以抓取并监控自己的健康状况。

虽然一个只收集关于自身数据的Prometheus服务器并没有太大用处,但它是一个很好的开始示例。将以下基本的Prometheus配置保存为名为
prometheus.yml的文件:

global:
scrape_interval: 15s # 默认情况下,每15秒抓取目标。

在与外部系统(联邦,远程存储,Alertmanager)通信时,将这些标签附加到任何时间序列或警报上。

external_labels:
monitor: ‘codelab-monitor’

一个抓取配置,包含一个要抓取的端点:

这里是Prometheus本身。

scrape_configs:

作业名称被添加为标签job=<job_name>到从这个配置中抓取的任何时间序列上。

  • job_name: ‘prometheus’

    覆盖全局默认值,每5秒从这个作业抓取目标。

    scrape_interval: 5s

    static_configs:

    • targets: [‘localhost:9090’]
1
2
3
4
5
6
  
有关配置选项的完整规范,请参阅配置文档。

启动Prometheus

使用你新创建的配置文件启动Prometheus,转到包含Prometheus二进制文件的目录并运行:

启动Prometheus。

# 默认情况下,Prometheus将其数据库存储在./data(flag --storage.tsdb.path)。
./prometheus --config.file=prometheus.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
  
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`(目标抓取之间的实际时间)。在表达式控制台中输入以下内容,然后点击“Execute”:

prometheus_target_interval_length_seconds

1
2
3
4
5
  
这应该返回一系列不同的时间序列(每个都有记录的最新值),每个都有指标名称 
prometheus_target_interval_length_seconds ,但有不同的标签。这些标签指定了不同的延迟百分位数和目标组间隔。

如果我们只对99百分位数的延迟感兴趣,我们可以使用这个查询:

prometheus_target_interval_length_seconds{quantile=“0.99”}

1
2
  
要计算返回的时间序列数量,你可以写:

count(prometheus_target_interval_length_seconds)

1
2
3
4
5
6
7
8
  
有关表达式语言的更多信息,请参阅表达式语言文档。

使用图形界面

要绘制表达式,请导航到http://localhost:9090/graph并使用“Graph”标签。

例如,输入以下表达式以绘制自抓取Prometheus每秒创建的块的速率:

rate(prometheus_tsdb_head_chunks_created_total[1m])

1
2
3
4
5
6
7
8
  
尝试使用不同的图形范围参数和其他设置进行实验。

启动一些示例目标

让我们为Prometheus添加一些额外的目标来抓取。

Node Exporter被用作示例目标,有关使用它的更多信息,请查看这些说明。

tar -xzvf node_exporter-..tar.gz
cd node_exporter-.

在不同的终端中启动3个示例目标:

./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

1
2
3
4
5
6
7
8
9
10
11
12
13
  
现在你应该有示例目标在http://localhost:8080/metrics、http://localhost:8081/metrics和http://localhost:8082/metrics上监听。

配置Prometheus监控示例目标

现在我们将配置Prometheus来抓取这些新目标。让我们将所有三个端点分组到一个名为 
node 的作业中。我们假设前两个端点是生产目标,而第三个代表金丝雀实例。在Prometheus中模拟这一点,我们可以将多个端点组添加到一个作业中,为每个目标组添加额外的标签。在这个例子中,我们将为第一组目标添加 
group="production" 标签,而为第二组添加 
group="canary"

为了实现这一点,在 
prometheus.yml 的 
scrape_configs 部分添加以下作业定义,并重新启动你的Prometheus实例:

scrape_configs:

  • job_name: ‘node’

    覆盖全局默认值,每5秒从这个作业抓取目标。

    scrape_interval: 5s

    static_configs:

    • targets: [‘localhost:8080’, ‘localhost:8081’]
      labels:
      group: ‘production’

    • targets: [‘localhost:8082’]
      labels:
      group: ‘canary’

1
2
3
4
5
6
7
8
9
10
11
  
转到表达式浏览器并验证Prometheus现在是否有关于这些示例端点暴露的时间序列的信息,例如 
node_cpu_seconds_total 。

配置规则以聚合抓取的数据到新的时间序列

虽然在我们的示例中不是问题,但对成千上万的时间序列进行聚合的查询在临时计算时可能会变慢。为了使这更高效,Prometheus可以通过配置的_recording rules_将表达式预记录到新的持久时间序列中。假设我们对记录每秒钟CPU时间(
`node_cpu_seconds_total`)的平均值感兴趣,该值是按实例计算的(但保留
`job`、
`instance`和
`mode`维度),在5分钟的窗口内测量。我们可以这样写:

avg by (job, instance, mode) (rate(node_cpu_seconds_total[5m]))

1
2
3
4
5
6
  
尝试绘制这个表达式。

要将这个表达式产生的新指标记录为
`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]))
1
2
3
4
5
6
  
要让Prometheus接受这个新规则,在
`prometheus.yml`中添加一个
`rule_files`语句。配置现在应该看起来像这样:

```

global:
scrape_interval: 15s # 默认情况下,每15秒抓取目标。
evaluation_interval: 15s # 每15秒评估规则。

将这些额外的标签附加到这个Prometheus实例收集的所有时间序列上。

external_labels:
monitor: ‘codelab-monitor’

rule_files:

  • ‘prometheus.rules.yml’

scrape_configs:

  • job_name: ‘prometheus’

    覆盖全局默认值,每5秒从这个作业抓取目标。

    scrape_interval: 5s

    static_configs:

    • targets: [‘localhost:9090’]
  • job_name: ‘node’

    覆盖全局默认值,每5秒从这个作业抓取目标。

    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在突然的进程故障情况下有恢复机制,但建议使用  
`SIGTERM`  
信号来干净地关闭Prometheus实例。  
如果你在Linux上运行,可以使用  
`kill -s SIGTERM <PID>`  
来执行此操作,将  
`<PID>`  
替换为你的Prometheus进程ID。  
  
此文档是开源的。请通过提交问题或拉取请求来帮助改进它。  
  
欢迎关注公众号 **江达小记**  
  
  
  
![江达小记](/images/wechatmpscan.png)