Prometheus 入门指南
这是Prometheus官方文档的“入门指南”,以下是该网页内容的中文翻译:
入门指南 | Prometheus
本指南是一个“Hello World”风格的教程,展示了如何安装、配置和使用一个简单的Prometheus实例。你将下载并本地运行Prometheus,配置它来抓取自身和一个示例应用程序的数据,然后使用查询、规则和图表来使用收集到的时间序列数据。
下载并运行Prometheus
下载适用于你平台的最新版本的Prometheus,然后解压并运行它:
1 | tar xvfz prometheus-*.tar.gz |
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 |
|
启动Prometheus。
# 默认情况下,Prometheus将其数据库存储在./data(flag --storage.tsdb.path)。
./prometheus --config.file=prometheus.yml
1 |
|
prometheus_target_interval_length_seconds
1 |
|
prometheus_target_interval_length_seconds{quantile=“0.99”}
1 |
|
count(prometheus_target_interval_length_seconds)
1 |
|
rate(prometheus_tsdb_head_chunks_created_total[1m])
1 |
|
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 |
|
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 |
|
avg by (job, instance, mode) (rate(node_cpu_seconds_total[5m]))
1 |
|
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]))
- record: job_instance_mode:node_cpu_seconds:avg_rate5m
1 |
|
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。
此文档是开源的。请通过提交问题或拉取请求来帮助改进它。
欢迎关注公众号 **江达小记**
