Prometheus 安装与配置
本文档涵盖 Prometheus 的安装部署、
prometheus.yml核心配置详解,以及快速启动检查清单。
符号说明
| 符号 | 含义 |
|---|---|
| 🚨 | 陷阱 / Pitfall |
| 💡 | 最佳实践 / Best practice |
| 🔬 | 深入原理 / Deep-dive |
| ⚡ | 性能提示 / Performance note |
1. prometheus.yml 配置详解
1.1 全局结构
global: # 全局默认值(可被各模块覆盖)
scrape_configs: # 抓取目标定义(最重要)
rule_files: # 规则文件路径
alerting: # Alertmanager 配置
remote_write: # 远程写入(长期存储)
remote_read: # 远程读取(长期存储)1.2 global — 全局默认值
global:
scrape_interval: 15s # 抓取间隔。生产环境建议 15-30s
scrape_timeout: 10s # 抓取超时,必须 < scrape_interval
evaluation_interval: 15s # 规则评估间隔
external_labels: # 所有时间序列的外部标签(联邦/多集群识别)
cluster: 'us-east-1'
env: 'production'1.3 scrape_configs — 抓取配置(核心)
scrape_configs:
- job_name: 'my-service' # 必填,会作为 job 标签加入每条指标
# --- 抓取行为 ---
scrape_interval: 30s # 覆盖 global
scrape_timeout: 10s # 覆盖 global
metrics_path: /metrics # 指标路径,默认 /metrics
scheme: https # http 或 https
sample_limit: 20000 # 单次抓取可接受的样本数上限
# --- 认证 ---
basic_auth:
username: 'admin'
password_file: '/etc/prometheus/secrets/app-password'
bearer_token_file: '/var/run/secrets/token'
tls_config:
ca_file: '/etc/prometheus/ca.crt'
cert_file: '/etc/prometheus/client.crt'
key_file: '/etc/prometheus/client.key'
insecure_skip_verify: false
# --- 目标发现(三选一或组合使用)---
static_configs: # 静态列表(最简单)
- targets: ['host1:8080', 'host2:8080']
labels:
env: 'production'
- targets: ['host3:8080']
labels:
env: 'canary'
# 或使用服务发现(见 05-标签重写与服务发现)
# kubernetes_sd_configs: [...]
# consul_sd_configs: [...]
# file_sd_configs: [...]
# --- 标签重写 ---
relabel_configs: [...] # 抓取前(目标发现标签操作)
metric_relabel_configs: [...] # 抓取后(指标标签操作)1.4 完整生产环境示例
global:
scrape_interval: 30s
scrape_timeout: 10s
evaluation_interval: 30s
external_labels:
cluster: 'us-east-1'
env: 'production'
rule_files:
- '/etc/prometheus/rules/recording.yml'
- '/etc/prometheus/rules/alerts.yml'
alerting:
alertmanagers:
- static_configs:
- targets: ['alertmanager:9093']
scrape_configs:
# 1. Prometheus 自监控
- job_name: 'prometheus'
scrape_interval: 15s
static_configs:
- targets: ['localhost:9090']
# 2. 节点导出器
- job_name: 'node'
scrape_interval: 30s
static_configs:
- targets:
- 'node1.example.com:9100'
- 'node2.example.com:9100'
- 'node3.example.com:9100'
labels:
dc: 'dc1'
# 3. 自定义应用
- job_name: 'backend'
scrape_interval: 15s
metrics_path: '/actuator/prometheus'
scheme: https
basic_auth:
username: 'prometheus'
password_file: '/etc/prometheus/secrets/backend'
static_configs:
- targets:
- 'backend1:8443'
- 'backend2:8443'💡 配置变更后,用
promtool check config prometheus.yml验证语法,用curl -X POST http://localhost:9090/-/reload热加载(需启动参数--web.enable-lifecycle)。
2. Docker Compose 快速启动
services:
prometheus:
image: prom/prometheus:latest
ports: ["9090:9090"]
volumes:
- ./prometheus:/etc/prometheus
- promdata:/prometheus
node-exporter:
image: prom/node-exporter:latest
network_mode: host
pid: host
volumes:
- /proc:/host/proc:ro
- /sys:/host/sys:ro
- /:/rootfs:ro
command:
- '--path.procfs=/host/proc'
- '--path.sysfs=/host/sys'
blackbox:
image: prom/blackbox-exporter:latest
ports: ["9115:9115"]
volumes:
- ./blackbox.yml:/etc/blackbox_exporter/config.yml
alertmanager:
image: prom/alertmanager:latest
ports: ["9093:9093"]
volumes:
- ./alertmanager.yml:/etc/alertmanager/config.yml
grafana:
image: grafana/grafana:latest
ports: ["3000:3000"]
volumes:
promdata: {}附录:快速启动检查清单
- 下载并解压 Prometheus
- 编写
prometheus.yml(至少配置scrape_configs) -
promtool check config prometheus.yml验证配置 -
./prometheus --config.file=prometheus.yml启动 - 访问
http://localhost:9090/targets确认目标 UP - 在
http://localhost:9090/graph测试 PromQL 查询 - 配置 Recording Rules 和 Alerting Rules
- 配置 Alertmanager 通知渠道
- 安装需要的 Exporters
- 搭建 Grafana 看板(导入社区模板或自建)
- 设置长期存储(远程写入)和备份策略
- 配置告警静默和维护窗口
- 文档化:告警 runbook、指标字典、架构图
参考资料: