使用Thanos Store Gateway自动分层将30天前数据转冷存的策略

解读

国内云原生面试中,这道题考察的是“长期存储成本优化 + 水平扩展”落地能力。
Prometheus 本地只保留 15 天以内热数据是常态,超过 30 天的数据若仍放在 SSD 对象存储标准桶,费用会翻 3~5 倍。
Thanos Store Gateway 本身不“移动”数据,它只按需把对象存储里的 block 加载到内存索引并返回查询;真正的“转冷”动作由对象存储的生命周期规则完成。
因此,策略的核心是:

  1. 让 Thanos 组件把 30 天前的 block 统一放到独立前缀(如 thanos/cold/);
  2. 通过对象存储生命周期规则,把该前缀自动沉降为低频或归档存储;
  3. 保证 Store Gateway 仍能识别并查询冷存 block,且查询延迟可接受;
  4. 整个流程对 PromQL、Grafana、告警无感知。

知识点

  1. Thanos 架构:Sidecar/Receiver 上传 block → Objstore → Store Gateway 提供只读查询。
  2. block 时间边界:2 h 压缩一次,每 2 h 产生一个 ULID 目录;compactor 会把小 block 合并成 24 h、7 d、30 d 的大 block。
  3. 对象存储生命周期:阿里云 OSS、腾讯云 COS、华为云 OBS、MinIO 均支持按前缀+最后修改时间沉降。
  4. 索引缓存与延迟:Store Gateway 启动时会拉取 block 的 index-cache.json 与 meta.json,冷存首次访问需额外 1~3 s 解冻(部分云需先 Restore)。
  5. Compactor 的 “--retention.resolution-raw”、“--retention.resolution-5m”、“--retention.resolution-1h” 只决定“删不删”,与“冷热分层”无关。
  6. 国内合规:金融、运营商场景要求数据保存 6 个月以上,冷存必须支持合规归档型存储,且不可篡改。

答案

落地步骤(以阿里云 OSS 为例,可直接平移到腾讯云 COS、华为云 OBS):

  1. 规划目录结构
    在 bucket 内建立两级前缀:
    • thanos/hot/ // 0–30 天
    • thanos/cold/ // 30 天以后

  2. 调整上传逻辑
    Sidecar/Receiver 默认上传路径为
    /bucket/thanos/
    通过启动参数
    --objstore.config-file=objstore.yml
    在 objstore.yml 中增加
    prefix: "thanos/hot"
    保证新 block 先落到 hot 目录。

  3. 运行 Compactor 并关闭删除
    compactor:
    --data-dir=/var/thanos/compact
    --objstore.config-file=objstore.yml
    --retention.resolution-raw=0d // 不删除
    --retention.resolution-5m=0d
    --retention.resolution-1h=0d
    --compact.enable
    --downsampling.enable
    让 30 天前的 block 被合并成 5 m、1 h 分辨率,但仍留在 hot 目录。

  4. 批量“挪前缀”脚本
    利用 ossutil 或 coscli,按 block 的 ULID 时间戳(前 10 字符为时间)筛选:
    #!/bin/bash
    CUTOFF=(date−d"30daysago"+forobjin(date -d "30 days ago" +%s) for obj in (ossutil ls oss://yourbucket/thanos/hot/ | awk '{print NF}'); do ULID={obj##*/}
    T=ULID:0:10if[[{ULID:0:10} if [[ T -lt CUTOFF]];thenossutilmvCUTOFF ]]; then ossutil mv obj ${obj/hot/cold}
    fi
    done
    放入 Crontab 每日 02:00 执行一次。

  5. 配置 OSS 生命周期规则
    规则名称:thanos-cold
    前缀:thanos/cold/
    操作:
    • 创建 1 天后转为低频访问(IA)
    • 创建 7 天后转为归档存储(Archive)
    解冻时间:1 分钟(归档型)
    费用下降约 70%。

  6. 启动两级 Store Gateway
    • hot-gateway:
    --objstore.config-file=objstore-hot.yml // 只扫描 thanos/hot
    --index-cache-size=1GB
    • cold-gateway:
    --objstore.config-file=objstore-cold.yml // 只扫描 thanos/cold
    --index-cache-size=500MB
    --store.enable-index-header-lazy-reader // 延迟加载,减少内存
    Query 前端通过
    --store=dnssrv+_grpc._tcp.thanos-store-hot
    --store=dnssrv+_grpc._tcp.thanos-store-cold
    自动合并结果。

  7. 查询性能兜底
    • 对冷存 Grafana 仪表板增加 5 分钟 max_source_resolution 自动降级提示;
    • 在 Query 加 15 s 超时,防止归档解冻慢导致长尾;
    • 定期预热:每月 1 号用 promtool query instant 把上月重要指标提前查一次,触发 OSS 解冻,保证月初汇报不卡顿。

  8. 监控与告警
    • up{job="thanos-store-cold"} == 0 立即告警;
    • thanos_bucket_store_block_load_duration_seconds{job="thanos-store-cold"} > 10s 提示解冻异常;
    • OSS 监控:归档读取延迟 > 3 次/小时即通知运维手动恢复。

通过以上 8 步,即可在 30 天边界自动完成“热→冷”分层,存储成本下降 70% 以上,查询透明,且满足国内合规与面试场景对“可落地、可量化”的要求。

拓展思考

  1. 如果未来政策要求 90 天转“冷归档”、180 天转“深度归档”,只需在生命周期规则加两条阶梯,无需改 Thanos 代码。
  2. 对于跨地域灾备,可在 OSS 开启“跨区域复制”,冷前缀只复制元数据,不复制归档数据,节省 50% 流量。
  3. 当单 bucket 对象数 > 5 亿时,LIST 性能下降,可改用“bucket shard”方案:按集群+环境拆桶,Store Gateway 使用 bucket+prefix 联合路由,面试可展开谈水平扩展思路。
  4. 若采用 Thanos Receive 多租户模式,租户标签可作为对象存储前缀的一部分,生命周期规则按租户单独配置,实现“黄金/白银/青铜”级存储 SLA,体现商业思维。