复制成功
请遵守本站许可
REPORT
Chapter_Post // Field_Report

Post_Ref: RL-PROMETHE

2026.04.08

Prometheus-核心配置文件-prometheus.yml-详解

Echo HaoRan
Echo HaoRan
ANALYSIS

此贴仅进行yml文件的解析,需要知道怎么部署点击:[[在 Ubuntu 上部署 Prometheus 和 Grafana 到 K8s]]

Prometheus 配置文件完整解析#

📝 配置文件概述#

Prometheus 的配置基于 YAML 格式,定义了其所有核心行为:

  • 全局默认参数
  • 规则文件加载
  • 告警管理集成
  • 指标采集任务
  • 远程存储配置

️⃣ 全局配置(global)#

global 块定义了适用于所有配置的默认参数:

PRTCL // YAML
global:
# 默认抓取间隔
scrape_interval: 15s # 每 15 秒采集一次指标
# 抓取超时时间
scrape_timeout: 10s # 等待目标响应的最长时间
# 规则评估间隔
evaluation_interval: 15s # 每 15 秒评估一次规则
# 外部标签(用于区分不同 Prometheus 实例)
external_labels:
monitor: 'prom-prod-01' # 生产环境实例标识
region: 'us-east-1' # 区域标识

💡 提示:外部标签仅附加在时间序列数据发送到外部系统(如 Alertmanager)时。

️⃣ 规则文件配置(rule_files)#

指定需要加载的规则文件路径:

PRTCL // YAML
rule_files:
# 使用 glob 模式加载规则文件
- "/etc/prometheus/rules/*.yml"
# 也可以指定具体文件
- "/etc/prometheus/recording.rules"
- "/etc/prometheus/alerting.rules"

️⃣ 告警配置(alerting)#

配置 Alertmanager 连接信息:

PRTCL // YAML
alerting:
alertmanagers:
- static_configs:
- targets:
- 'alertmanager:9093' # Alertmanager 服务地址
scheme: http # 连接协议(http/https)
timeout: 10s # 请求超时时间

// …existing code…

️⃣ 抓取配置(scrape_configs)#

定义指标采集任务:

PRTCL // YAML
scrape_configs:
# Prometheus 自身监控
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
# 节点导出器监控
- job_name: 'node'
static_configs:
- targets: ['node-exporter:9100']
# Kubernetes 服务发现示例
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true

🔍 标签重写(relabel_configs)#

PRTCL // YAML
relabel_configs:
# 保留特定标签的目标
- source_labels: [__meta_kubernetes_pod_label_app]
regex: my-app
action: keep
# 替换标签值
- source_labels: [__meta_kubernetes_namespace]
target_label: namespace
# 删除特定标签
- regex: '__meta_kubernetes_.*'
action: labeldrop

️⃣ 远程存储配置(remote_write/remote_read)#

PRTCL // YAML
# 远程写入配置
remote_write:
- url: 'http://remote-storage:9201/write'
queue_config:
capacity: 10000
max_samples_per_send: 1000
batch_send_deadline: 30s
# 远程读取配置
remote_read:
- url: 'http://remote-storage:9201/read'
read_recent: true

️⃣ 存储配置(storage)#

PRTCL // YAML
storage:
tsdb:
# 数据保留时间
retention.time: 15d
# 数据目录
path: /prometheus
# 启用压缩
compression: true

⚙️ 完整配置示例#

PRTCL // YAML
global:
scrape_interval: 15s
evaluation_interval: 15s
external_labels:
env: 'production'
rule_files:
- '/etc/prometheus/rules/*.yml'
alerting:
alertmanagers:
- static_configs:
- targets: ['alertmanager:9093']
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node-exporter'
static_configs:
- targets: ['node-exporter:9100']
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true
remote_write:
- url: 'http://thanos-receive:9090/api/v1/receive'
storage:
tsdb:
retention.time: 15d
path: /prometheus

📋 配置验证#

PRTCL // BASH
Terminal window
# 检查配置文件语法
promtool check config /etc/prometheus/prometheus.yml
# 验证规则文件
promtool check rules /etc/prometheus/rules/*.yml
# 单元测试规则
promtool test rules test.yml

⚠️ 最佳实践#

  1. 合理设置采集间隔,避免过于频繁
  2. 使用服务发现替代静态配置
  3. 配置适当的数据保留期限
  4. 定期备份配置文件
  5. 使用版本控制管理配置

🔗 参考资源#


作者:EchoWang

小红书:汪多多是只猫

B 站:汪多多是只猫

公众号:汪多多是只猫

博客:https://blog.echospace.top

R P
Rhine Lab Pioneer Division
Auth_Verified: 2026.04.08
// END OF POST

订阅

通过 RSS 订阅本站,新文章发布时第一时间收到通知。

Follow
Classified
Chapter_06
Protocol_Ref: CC-BY-NC-SA-4.0

Prometheus-核心配置文件-prometheus.yml-详解

Author: CHONGXIReleased: 2026.04.08

Licensed under CC BY-NC-SA 4.0

评论

© 2025-2026 EchoSpace
Powered by Astro & echohaoran Non-Collaborative_Entity // Protocol_V.4.21