Skip to content

高可用部署 ​

架构模式 ​

基本架构 ​

┌──────────────┐
│ Prometheus 1 │ (Active)
└──────────────┘
       │
┌──────────────┐
│ Prometheus 2 │ (Standby)
└──────────────┘
       │
┌──────────────┐
│ Alertmanager │ (Cluster)
└──────────────┘

Prometheus 高可用 ​

方案一:多实例独立 ​

每个实例独立抓取,通过标签区分。

yaml
global:
  external_labels:
    monitor: 'prometheus-1'  # 实例标识

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

方案二:联邦集群 ​

中心 Prometheus 从其他实例拉取数据。

yaml
# 中心 Prometheus
scrape_configs:
  - job_name: 'federate'
    honor_labels: true
    metrics_path: '/federate'
    params:
      'match[]':
        - '{job="prometheus"}'
        - '{__name__=~"job:.*"}'
    static_configs:
      - targets:
          - 'prometheus-1:9090'
          - 'prometheus-2:9090'

方案三:Thanos 架构 ​

长期存储和全局查询。

┌─────────────────┐
│   Thanos Query  │ ← 全局查询
└────────┬────────┘
         │
    ┌────┴────┐
    ▼         ▼
┌────────┐ ┌────────┐
│ Prom 1 │ │ Prom 2 │
│+Sidecar│ │+Sidecar│
└────────┘ └────────┘
         │         │
         └────┬────┘
              ▼
        ┌──────────┐
        │  Object  │ ← 长期存储
        │  Storage │
        └──────────┘

Thanos 部署 ​

yaml
# Sidecar
thanos sidecar \
  --prometheus.url=http://localhost:9090 \
  --tsdb.path=/prometheus/data \
  --objstore.config-file=bucket.yaml

# Query
thanos query \
  --store=sidecar-1:10901 \
  --store=sidecar-2:10901

# Store Gateway
thanos store \
  --data-dir=/thanos/store \
  --objstore.config-file=bucket.yaml

Alertmanager 高可用 ​

集群配置 ​

bash
# 节点1
alertmanager \
  --cluster.listen-address=0.0.0.0:9094 \
  --cluster.peer=alertmanager-2:9094 \
  --cluster.peer=alertmanager-3:9094

# 节点2
alertmanager \
  --cluster.listen-address=0.0.0.0:9094 \
  --cluster.peer=alertmanager-1:9094 \
  --cluster.peer=alertmanager-3:9094

Prometheus 配置 ​

yaml
alerting:
  alertmanagers:
    - static_configs:
        - targets:
            - alertmanager-1:9093
            - alertmanager-2:9093
            - alertmanager-3:9093

去重机制 ​

Alertmanager 集群自动去重告警。

Alert → AM-1 (接收)
     → AM-2 (接收)
     → AM-3 (接收)

所有实例共享状态,确保只发送一次告警

存储高可用 ​

本地存储 ​

yaml
# Prometheus 配置
storage:
  tsdb:
    path: /prometheus/data
    retention: 15d

远程存储 ​

yaml
# Prometheus 远程写入
remote_write:
  - url: "http://remote-storage:8080/write"
    queue_config:
      max_samples_per_send: 1000
      max_shards: 200

# Prometheus 远程读取
remote_read:
  - url: "http://remote-storage:8080/read"

存储方案 ​

方案特点
Thanos全局查询,长期存储
Cortex多租户,长期存储
VictoriaMetrics高性能,长期存储
MimirGrafana Labs,多租户

监控 Prometheus ​

关键指标 ​

promql
# 采集延迟
prometheus_target_scrape_pool_sync_delay_seconds

# 采集失败
prometheus_target_scrapes_exceeded_sample_limit_total

# 规则评估
prometheus_rule_evaluation_failures_total

# 延迟
prometheus_sd_refresh_duration_seconds

告警规则 ​

yaml
groups:
  - name: prometheus_alerts
    rules:
      - alert: PrometheusDown
        expr: up{job="prometheus"} == 0
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: Prometheus is down

      - alert: PrometheusConfigReloadFailed
        expr: prometheus_config_last_reload_successful == 0
        for: 5m
        labels:
          severity: warning

      - alert: PrometheusNotConnectedToAlertmanager
        expr: prometheus_notifications_alertmanagers_discovered < 1
        for: 5m
        labels:
          severity: critical

备份与恢复 ​

备份 ​

bash
# 快照 API
curl -XPOST http://localhost:9090/api/v1/admin/tsdb/snapshot

# 备份数据
tar czf prometheus-backup-$(date +%Y%m%d).tar.gz /prometheus/data/snapshots/

恢复 ​

bash
# 停止 Prometheus
systemctl stop prometheus

# 恢复数据
tar xzf prometheus-backup-20240101.tar.gz -C /prometheus/data/

# 启动 Prometheus
systemctl start prometheus

最佳实践 ​

  1. 多实例部署:至少2个 Prometheus 实例
  2. 联邦或 Thanos:大规模场景
  3. Alertmanager 集群:3个节点
  4. 远程存储:长期数据保存
  5. 监控自己:监控 Prometheus 关键指标
  6. 定期备份:配置和数据

参考:https://prometheus.io/docs/introduction/faq/