Thanos 部署方案(适用于 Prometheus 远程存储 & 长期存储)

986 阅读3分钟

Thanos 部署方案(适用于 Prometheus 远程存储 & 长期存储)

1. Thanos 适用场景

Thanos 是一个 Prometheus 扩展组件,可以实现:

  • 长期存储:把 Prometheus 的历史数据存储到 对象存储(S3、MinIO、GCS) ,防止本地数据丢失。
  • 跨集群聚合:支持多个 Prometheus 实例查询统一的数据。
  • 数据压缩 & 低成本存储:使用 块存储 来降低存储成本。

2. 机器规划

组件作用推荐 CPU推荐内存存储运行方式
Prometheus采集数据8-16 核16-32GB500GB+ SSD独立运行
Thanos Sidecar连接 Prometheus2-4 核4-8GB50GB SSD同 Prometheus
Thanos Store读取长期存储4-8 核8-16GB1TB+ SSD独立运行
Thanos Compactor数据合并 & 压缩4-8 核8GB+1TB+ SSD独立运行
Thanos Query统一查询入口4-8 核8-16GB100GB SSD独立运行
对象存储(S3/MinIO)长期存储16 核32GB+10TB+ HDD独立运行

3. 部署 Thanos

1️⃣ 部署 Prometheus 并启用远程写入

prometheus.yml 配置 远程写入和存储

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'node'
    static_configs:
      - targets: ['localhost:9100']

# 启用 Remote Write
remote_write:
  - url: "http://thanos-sidecar:10902/api/v1/receive"

确保 Prometheus 版本 >= 2.28,以支持 Thanos 远程存储。


2️⃣ 部署 Thanos Sidecar

Thanos Sidecar 运行在 Prometheus 旁边,负责:

  • 实时查询 Prometheus 数据
  • 上传数据到对象存储
docker run -d --name thanos-sidecar \
  -v /prometheus/data:/data \
  -v /thanos:/thanos \
  --network=host \
  quay.io/thanos/thanos:latest \
  sidecar \
  --tsdb.path="/data" \
  --objstore.config-file="/thanos/config.yaml" \
  --prometheus.url="http://localhost:9090"

📌 对象存储配置/thanos/config.yaml):

type: S3
config:
  bucket: "thanos-bucket"
  endpoint: "s3.amazonaws.com"
  access_key: "your-access-key"
  secret_key: "your-secret-key"
  region: "us-east-1"

这里以 S3 为例,如果使用 MinIO,改成 endpoint: "minio:9000"


3️⃣ 部署 Thanos Store(查询历史数据)

Thanos Store 负责从对象存储加载历史数据:

docker run -d --name thanos-store \
  -v /thanos:/thanos \
  --network=host \
  quay.io/thanos/thanos:latest \
  store \
  --objstore.config-file="/thanos/config.yaml"

4️⃣ 部署 Thanos Query(统一查询入口)

Thanos Query 用于聚合多个数据源:

docker run -d --name thanos-query \
  --network=host \
  quay.io/thanos/thanos:latest \
  query \
  --http-address="0.0.0.0:9091" \
  --store="thanos-sidecar:10901" \
  --store="thanos-store:10901"

访问 http://localhost:9091 即可使用 Thanos Query 统一查询数据。


5️⃣ 部署 Thanos Compactor(数据合并 & 压缩)

Thanos Compactor 负责合并小块数据,提高查询性能:

docker run -d --name thanos-compactor \
  -v /thanos:/thanos \
  --network=host \
  quay.io/thanos/thanos:latest \
  compact \
  --objstore.config-file="/thanos/config.yaml" \
  --retention.resolution-raw=30d \
  --retention.resolution-5m=90d \
  --retention.resolution-1h=180d

这里配置了 30 天保留原始数据,90 天 5 分钟数据,180 天 1 小时数据


4. Grafana 配置

1️⃣ 添加 Thanos Query 数据源

  • Type: Prometheus

  • URL: http://thanos-query:9091

  • Scrape Interval: 15s

  • PromQL 示例

    rate(node_cpu_seconds_total{mode="user"}[5m])
    

5. 监控 Thanos 本身

Thanos 提供自己的指标

在 Prometheus prometheus.yml 添加:

scrape_configs:
  - job_name: "thanos"
    static_configs:
      - targets: ["thanos-sidecar:10902", "thanos-store:10902", "thanos-query:9091"]

关键监控指标

  • Thanos Sidecar 上传速率

    rate(thanos_sidecar_upload_duration_seconds_count[5m])
    
  • Thanos Store 读取速率

    rate(thanos_store_series_fetch_duration_seconds_count[5m])
    
  • Thanos Query 请求速率

    rate(http_requests_total{job="thanos-query"}[5m])
    

6. 备份 & 恢复

备份 Thanos 数据

aws s3 sync s3://thanos-bucket /backup/thanos/

或:

mc alias set myminio http://minio:9000 access-key secret-key
mc cp -r myminio/thanos-bucket /backup/thanos/

恢复数据

aws s3 sync /backup/thanos/ s3://thanos-bucket

或:

mc cp -r /backup/thanos/ myminio/thanos-bucket

7. 总结

组件作用部署方式
Thanos Sidecar连接 Prometheus,上传数据运行在 Prometheus 旁
Thanos Store读取长期存储数据独立运行
Thanos Query提供统一查询接口独立运行
Thanos Compactor数据压缩 & 优化存储独立运行
MinIO / S3存储长期数据远程存储

📌 使用 Thanos,可以让 Prometheus 具备高可用、长期存储、跨集群查询能力,适用于 大规模监控场景!🚀