强曰为道

与天地相似,故不违。知周乎万物,而道济天下,故不过。旁行而不流,乐天知命,故不忧.
文档目录

14 - Thanos

14 - Thanos

14.1 概述

Thanos 是一套 Prometheus 高可用和长期存储解决方案,由 Improbable 开源,现为 CNCF 孵化项目。它解决了 Prometheus 的三大痛点:高可用、长期存储、全局查询。

解决的问题

问题原生 PrometheusThanos
高可用需要双写Sidecar + 去重
长期存储本地 15 天对象存储(S3/GCS/OSS)
全局查询联邦有限Query 层无限扩展
降采样不支持自动降采样

14.2 核心组件

┌─────────────────────────────────────────────────────────────┐
│                     Thanos 组件架构                          │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  ┌───────────────┐   ┌───────────────┐   ┌───────────────┐ │
│  │  Sidecar      │   │  Sidecar      │   │  Sidecar      │ │
│  │  + Prom A     │   │  + Prom B     │   │  + Prom C     │ │
│  └───────┬───────┘   └───────┬───────┘   └───────┬───────┘ │
│          │上传块               │上传块               │        │
│          ▼                    ▼                    ▼        │
│  ┌─────────────────────────────────────────────────────┐   │
│  │              对象存储 (S3/GCS/MinIO)                  │   │
│  └─────────────────────────────────────────────────────┘   │
│          │查询                     │                       │
│          ▼                        ▼                       │
│  ┌───────────────┐        ┌───────────────┐               │
│  │  Store         │        │  Compactor    │               │
│  │  Gateway       │        │  (降采样+压缩) │               │
│  └───────┬───────┘        └───────────────┘               │
│          │                                                  │
│          ▼                                                  │
│  ┌───────────────┐        ┌───────────────┐               │
│  │  Query         │        │  Ruler        │               │
│  │  (全局查询)     │        │  (全局告警)    │               │
│  └───────────────┘        └───────────────┘               │
│                                                             │
└─────────────────────────────────────────────────────────────┘
组件功能
Sidecar与 Prometheus 部署在一起,上传数据块到对象存储,代理查询
Store Gateway对象存储的数据查询代理,提供历史数据查询
Query全局查询入口,合并多个数据源的结果,支持去重
Compactor压缩和降采样历史数据,优化存储和查询
Ruler跨数据源的全局告警和录制规则
Receive接收 Remote Write 数据(替代 Sidecar 模式)

14.3 快速部署

Docker Compose 部署

# docker-compose.yml
version: '3.8'

services:
  # Prometheus + Sidecar
  prometheus-0:
    image: prom/prometheus:v2.52.0
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--storage.tsdb.min-block-duration=2h'
      - '--storage.tsdb.max-block-duration=2h'
      - '--web.enable-lifecycle'
    volumes:
      - ./prometheus-0.yml:/etc/prometheus/prometheus.yml
      - prometheus-0-data:/prometheus

  thanos-sidecar-0:
    image: thanosio/thanos:v0.34.0
    command:
      - 'sidecar'
      - '--prometheus.url=http://prometheus-0:9090'
      - '--tsdb.path=/prometheus'
      - '--objstore.config-file=/etc/thanos/bucket.yml'
    volumes:
      - prometheus-0-data:/prometheus
      - ./bucket.yml:/etc/thanos/bucket.yml

  # Store Gateway
  thanos-store:
    image: thanosio/thanos:v0.34.0
    command:
      - 'store'
      - '--data-dir=/thanos/store'
      - '--objstore.config-file=/etc/thanos/bucket.yml'
    volumes:
      - ./bucket.yml:/etc/thanos/bucket.yml
      - store-data:/thanos/store

  # Query
  thanos-query:
    image: thanosio/thanos:v0.34.0
    command:
      - 'query'
      - '--store=thanos-sidecar-0:10901'
      - '--store=thanos-store:10901'
      - '--query.auto-downsampling'
    ports:
      - "9091:9090"

  # Compactor
  thanos-compact:
    image: thanosio/thanos:v0.34.0
    command:
      - 'compact'
      - '--data-dir=/thanos/compact'
      - '--objstore.config-file=/etc/thanos/bucket.yml'
      - '--retention.resolution-raw=30d'
      - '--retention.resolution-5m=90d'
      - '--retention.resolution-1h=365d'
    volumes:
      - ./bucket.yml:/etc/thanos/bucket.yml
      - compact-data:/thanos/compact

volumes:
  prometheus-0-data:
  store-data:
  compact-data:

对象存储配置

# bucket.yml - MinIO 示例
type: S3
config:
  bucket: "thanos"
  endpoint: "minio:9000"
  access_key: "minioadmin"
  secret_key: "minioadmin"
  insecure: true

# bucket.yml - AWS S3 示例
type: S3
config:
  bucket: "thanos-metrics"
  region: "us-east-1"
  access_key: "<access_key>"
  secret_key: "<secret_key>"

# bucket.yml - 阿里云 OSS 示例
type: S3
config:
  bucket: "thanos-metrics"
  endpoint: "oss-cn-hangzhou.aliyuncs.com"
  access_key: "<access_key>"
  secret_key: "<secret_key>"

14.4 长期存储

数据流向

Prometheus TSDB ──► Sidecar ──► 对象存储 ──► Compactor ──► 降采样数据
(2小时块)          (上传)       (原始块)      (压缩)       (5m/1h 聚合)

查询路径:
Query ──► Store Gateway ──► 对象存储(历史数据)
     ──► Sidecar ──► Prometheus(近 2 小时数据)

降采样策略

分辨率保留时间说明
raw(原始)30 天原始数据
5m(5分钟)90 天每 5 分钟一个聚合点
1h(1小时)365 天每小时一个聚合点

Compactor 配置

# compactor 详细配置
command:
  - 'compact'
  - '--data-dir=/thanos/compact'
  - '--objstore.config-file=/etc/thanos/bucket.yml'
  - '--retention.resolution-raw=30d'
  - '--retention.resolution-5m=90d'
  - '--retention.resolution-1h=365d'
  - '--compact.concurrency=1'
  - '--downsample.concurrency=1'
  - '--wait'  # 持续运行
  - '--wait-interval=5m'
  - '--consistency-delay=30m'

14.5 全局查询

Query 组件

thanos-query:
  image: thanosio/thanos:v0.34.0
  command:
    - 'query'
    # 数据源
    - '--store=sidecar-0:10901'
    - '--store=sidecar-1:10901'
    - '--store=store-gateway:10901'
    # 去重
    - '--query.replica-label=prometheus_replica'
    - '--query.auto-downsampling'
    # 限制
    - '--query.max-concurrent=20'
    - '--query.timeout=2m'
    # Web UI
    - '--web.external-prefix=/thanos'

去重机制

场景:两个 Prometheus 实例采集相同目标

Prometheus A (replica=0):
  up{job="api", instance="node1:8080", prometheus_replica="0"} = 1

Prometheus B (replica=1):
  up{job="api", instance="node1:8080", prometheus_replica="1"} = 1

Thanos Query 去重后:
  up{job="api", instance="node1:8080"} = 1  (只保留一条)

14.6 Ruler(全局规则)

thanos-rule:
  image: thanosio/thanos:v0.34.0
  command:
    - 'rule'
    - '--data-dir=/thanos/rule'
    - '--eval-interval=30s'
    - '--rule-file=/etc/thanos/rules/*.yml'
    - '--query=thanos-query:9090'
    - '--objstore.config-file=/etc/thanos/bucket.yml'
    - '--label=thanos_ruler="ruler-0"'
    - '--alert.label-drop="thanos_ruler"'

14.7 存储成本估算

假设:
  - 10,000 活跃时间序列
  - 15s 采集间隔
  - 原始数据保留 30 天

原始数据 (raw, 30天):
  10,000 × (86400/15) × 30 × 2 bytes ≈ 3.46 GB

降采样数据 (5m, 90天):
  10,000 × (86400/300) × 90 × 2 bytes ≈ 0.52 GB

降采样数据 (1h, 365天):
  10,000 × (86400/3600) × 365 × 2 bytes ≈ 0.18 GB

总计: ≈ 4.16 GB(加上压缩约 2-3 GB)

14.8 Thanos vs 其他方案

方案优点缺点
Thanos生态成熟,CNCF 项目组件多,运维复杂
Cortex多租户支持好架构复杂
MimirGrafana 官方支持较新
VictoriaMetrics高性能,简单社区较小

14.9 本章小结

组件功能
Sidecar上传数据块 + 代理查询
Store Gateway对象存储查询
Query全局查询 + 去重
Compactor压缩 + 降采样
Ruler全局告警/录制规则

扩展阅读


上一章13 - 联邦集群 下一章15 - 容器化部署