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

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

1. Thanos 适用场景

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

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

2. 机器规划

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

3. 部署 Thanos

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

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

yaml 复制代码
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 数据
  • 上传数据到对象存储
ini 复制代码
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):

lua 复制代码
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 负责从对象存储加载历史数据:

ini 复制代码
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 用于聚合多个数据源:

ini 复制代码
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 负责合并小块数据,提高查询性能:

ini 复制代码
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 示例

    ini 复制代码
    rate(node_cpu_seconds_total{mode="user"}[5m])

5. 监控 Thanos 本身

Thanos 提供自己的指标

在 Prometheus prometheus.yml 添加:

makefile 复制代码
scrape_configs:
  - job_name: "thanos"
    static_configs:
      - targets: ["thanos-sidecar:10902", "thanos-store:10902", "thanos-query:9091"]

关键监控指标

  • Thanos Sidecar 上传速率

    scss 复制代码
    rate(thanos_sidecar_upload_duration_seconds_count[5m])
  • Thanos Store 读取速率

    scss 复制代码
    rate(thanos_store_series_fetch_duration_seconds_count[5m])
  • Thanos Query 请求速率

    ini 复制代码
    rate(http_requests_total{job="thanos-query"}[5m])

6. 备份 & 恢复

备份 Thanos 数据

bash 复制代码
aws s3 sync s3://thanos-bucket /backup/thanos/

或:

bash 复制代码
mc alias set myminio http://minio:9000 access-key secret-key
mc cp -r myminio/thanos-bucket /backup/thanos/

恢复数据

bash 复制代码
aws s3 sync /backup/thanos/ s3://thanos-bucket

或:

bash 复制代码
mc cp -r /backup/thanos/ myminio/thanos-bucket

7. 总结

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

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

相关推荐
Aska_Lv1 分钟前
java--死锁,死循环会导致CPU使用率升高吗?
后端
Wo3Shi4七30 分钟前
MySQL JOIN 和 GROUP BY
数据库·后端
一只小闪闪36 分钟前
langchain4j搭建失物招领系统(一)---初始化项目
java·人工智能·后端
独立开阀者_FwtCoder40 分钟前
penAI重磅发布GPT-4o文生图:免费、精准、媲美真实照片!
前端·后端·面试
Fade_VV1 小时前
记录一个复合嵌套类的使用MyBatis取出数据的例子
后端
倚栏听风雨1 小时前
Class.getComponentType()详解
后端
三原1 小时前
项目管理:这个bug是你的问题!这个不是bug是需求变更!
前端·后端·团队管理
华洛1 小时前
实战派!百万PV的AI产品如何搭建RAG系统?
前端·javascript·后端
javaTodo1 小时前
Mysql索引实现原理和相关数据结构算法
后端
tomla2 小时前
震惊!遍历集合时直接调用remove()可能会出现问题!
后端