4.1 prometheus.yml 结构
global:
scrape_interval: 15s # 全局抓取周期
scrape_timeout: 10s # 单次抓取超时
evaluation_interval: 15s # 规则评估周期
external_labels: # 联邦/远程写时区分来源
cluster: "prod-cn"
rule_files: # 告警/记录规则文件
- "rules/*.yml"
alerting: # 指向 Alertmanager
alertmanagers:
- static_configs:
- targets: ["alertmanager:9093"]
scrape_configs: # 抓取任务(重点)
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
热加载配置
改完配置无需重启:启动时加 --web.enable-lifecycle,然后 curl -X POST http://localhost:9090/-/reload。也可用 promtool check config prometheus.yml 先校验语法。
4.2 scrape_config 详解
- job_name
- 抓取任务名,会成为每条序列的
job标签。 - metrics_path
- 抓取路径,默认
/metrics。 - scheme
http或https,默认 http。- scrape_interval / scrape_timeout
- 可覆盖全局值,为不同 job 设置不同抓取频率。
- *_sd_configs
- 服务发现配置(kubernetes_sd_configs、file_sd_configs、dns_sd_configs、consul_sd_configs 等)。
- relabel_configs
- 抓取前对目标标签做重写、过滤;
metric_relabel_configs则在抓取后作用于每条指标。 - basic_auth / bearer_token / tls_config
- 抓取受保护端点时的认证与 TLS 配置。
4.3 静态配置
scrape_configs:
- job_name: "node"
scrape_interval: 30s
static_configs:
- targets:
- "10.0.0.11:9100"
- "10.0.0.12:9100"
labels:
env: "prod"
team: "infra"
静态配置适合数量固定的目标。但在容器/云环境实例频繁增减,手动维护列表不现实——这就要靠服务发现。
4.4 服务发现
文件服务发现(最通用)
# prometheus.yml
- job_name: "file-sd"
file_sd_configs:
- files: ["targets/*.json"]
refresh_interval: 30s
// targets/api.json —— 由 CMDB/脚本动态生成
[
{
"targets": ["10.0.1.5:8080", "10.0.1.6:8080"],
"labels": { "job": "api", "env": "prod" }
}
]
DNS 服务发现
- job_name: "dns"
dns_sd_configs:
- names: ["_prometheus._tcp.svc.local"]
type: SRV
refresh_interval: 30s
Kubernetes 服务发现
- job_name: "k8s-pods"
kubernetes_sd_configs:
- role: pod # 可选 node/service/endpoints/pod/ingress
relabel_configs:
# 只抓带 prometheus.io/scrape=true 注解的 Pod
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: "true"
# 用注解里的 port 覆盖抓取地址
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace
regex: "([^:]+)(?::\d+)?;(\d+)"
replacement: "$1:$2"
target_label: __address__
# 把 namespace / pod 名提升为普通标签
- source_labels: [__meta_kubernetes_namespace]
target_label: namespace
- source_labels: [__meta_kubernetes_pod_name]
target_label: pod
4.5 relabel_configs 重打标签
relabel 是 Prometheus 最强大也最烧脑的机制。它在抓取前对目标的元标签(__meta_*、__address__ 等以 __ 开头的内部标签)做一系列变换。
| action | 作用 |
|---|---|
keep | 只保留 regex 匹配 source_labels 的目标 |
drop | 丢弃 regex 匹配的目标 |
replace | 用 replacement 写入 target_label(默认 action) |
labelmap | 按 regex 批量把匹配的 label 名映射成新名 |
labeldrop / labelkeep | 删除 / 保留匹配名字的标签 |
hashmod | 对 source 取哈希取模,用于抓取分片 |
relabel vs metric_relabel
relabel_configs 作用于抓取目标(决定抓不抓、给目标打什么标签);metric_relabel_configs 作用于抓回来的每条指标(常用于 drop 掉高基数或无用指标,省存储)。
# metric_relabel:丢弃某个高基数指标,省成本
metric_relabel_configs:
- source_labels: [__name__]
regex: "go_gc_duration_seconds.*"
action: drop
4.6 小结与下一步
- prometheus.yml 三块核心:global、rule_files/alerting、scrape_configs。
- 动态环境用服务发现(K8s/文件/DNS),静态目标用 static_configs。
- relabel_configs 控制"抓哪些目标 + 目标打什么标签";metric_relabel_configs 精简抓回的指标。
- 下一章逛 Exporter 生态——不用改代码就能监控主机、数据库、容器。