Banner

Thanos v0.42.4

2026-11-30
说明:v0.42.4 是 Thanos 的发布版本(发布于 2026-07-30)。本教程采用最通用的 Sidecar 模式,适用于单机或小型环境验证。所有命令中的版本号已统一锁定为 v0.42.4。

一、前置条件

项目要求
Prometheus已部署并可正常采集,建议 v2.30+
对象存储本地测试可用 FILESYSTEM;生产建议 S3 / MinIO / GCS / OSS
网络Sidecar、Store、Query、Compactor 之间 gRPC 端口互通
资源至少 2 核 4G;Store 与 Compactor 随数据量增长需更多内存

二、下载并校验 Thanos v0.42.4

以 Linux amd64 为例:

export THANOS_VERSION=v0.42.4
wget https://github.com/thanos-io/thanos/releases/download/${THANOS_VERSION}/thanos-${THANOS_VERSION}.linux-amd64.tar.gz
tar -xzf thanos-${THANOS_VERSION}.linux-amd64.tar.gz
sudo mv thanos /usr/local/bin/
thanos version

若使用容器镜像,镜像标签同样使用 quay.io/thanos/thanos:v0.42.4。

三、配置对象存储

1. 本地文件系统(仅测试)

# /etc/thanos/objstore.yaml
type: FILESYSTEM
config:
  directory: /data/thanos/bucket

2. S3 / MinIO(生产常用)

# /etc/thanos/objstore.yaml
type: S3
config:
  bucket: "thanos-data"
  endpoint: "minio.example.com:9000"
  region: "us-east-1"
  access_key: "YOUR_ACCESS_KEY"
  secret_key: "YOUR_SECRET_KEY"
  insecure: false          # 本地 MinIO 无 HTTPS 时设为 true
  signature_version2: false
生产环境务必使用 HTTPS,并通过 IAM 角色或密钥管理服务注入凭据,避免明文写入配置文件。

四、调整 Prometheus 配置

Sidecar 依赖 Prometheus 的本地 TSDB 目录和 HTTP 接口,需做两处关键调整。

1. 启动参数

prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.path=/data/prometheus \
  --web.enable-lifecycle \
  --storage.tsdb.min-block-duration=2h \
  --storage.tsdb.max-block-duration=2h \
  --web.listen-address=0.0.0.0:9090
  • --web.enable-lifecycle:Sidecar 需要调用 Prometheus 的 reload 接口。
  • min/max-block-duration=2h:关闭 Prometheus 本地压缩,让 Sidecar 按 2 小时块上传。

2. 外部标签

# /etc/prometheus/prometheus.yml
global:
  scrape_interval: 15s
  external_labels:
    replica: "prom-01"      # 多副本时用于去重,必须全局唯一
    cluster: "prod"
    region: "us-west"

external_labels 是 Thanos 区分数据来源、实现去重和路由的关键。

五、部署 Sidecar

thanos sidecar \
  --tsdb.path=/data/prometheus \
  --prometheus.url=http://localhost:9090 \
  --objstore.config-file=/etc/thanos/objstore.yaml \
  --grpc-address=0.0.0.0:10901 \
  --http-address=0.0.0.0:10902 \
  --shipper.upload-compacted
  • --tsdb.path 必须与 Prometheus 的 --storage.tsdb.path 完全一致。
  • --shipper.upload-compacted 用于首次上传已存在的历史块,按需启用。
  • Sidecar 默认每 2 小时上传一个块到对象存储。

六、部署 Store Gateway

thanos store \
  --objstore.config-file=/etc/thanos/objstore.yaml \
  --data-dir=/data/thanos/store-cache \
  --grpc-address=0.0.0.0:10905 \
  --http-address=0.0.0.0:10906

Store Gateway 把对象存储中的历史块暴露为 StoreAPI,供 Query 查询。

七、部署 Query

thanos query \
  --http-address=0.0.0.0:10903 \
  --grpc-address=0.0.0.0:10904 \
  --store=127.0.0.1:10901 \
  --store=127.0.0.1:10905 \
  --query.replica-label=replica \
  --query.timeout=2m
  • --store 可重复,分别指向 Sidecar 的 gRPC 地址和 Store Gateway 的 gRPC 地址。
  • --query.replica-label 启用多副本去重,值需与 Prometheus 的 external_labels 一致。
  • 访问 http://<query-ip>:10903 即可看到类似 Prometheus 的查询界面。

八、部署 Compactor

thanos compact \
  --objstore.config-file=/etc/thanos/objstore.yaml \
  --data-dir=/data/thanos/compact \
  --http-address=0.0.0.0:10912 \
  --wait \
  --retention.resolution-raw=30d \
  --retention.resolution-5m=180d \
  --retention.resolution-1h=365d

Compactor 负责合并碎片块、生成 5m/1h 降采样数据并按策略清理过期数据。

注意:同一对象存储桶只能运行一个 Compactor 实例,避免并发写入导致数据损坏。

九、验证

  1. 检查 Sidecar 上传:查看 Sidecar 日志,确认出现上传成功记录;对象存储桶中应出现以 ULID 命名的目录。
  2. 检查 Store 加载:访问 Store Gateway 的 HTTP 端口,确认已加载块数量大于 0。
  3. 检查 Query 数据源:打开 Query UI 的 Stores 页面,确认 Sidecar 与 Store Gateway 均为健康状态。
  4. 执行查询:在 Query UI 输入:
count({job="prometheus"})

若能同时返回近期数据(来自 Sidecar)和历史数据(来自 Store Gateway),说明链路正常。

十、常见问题

现象排查方向
Sidecar 启动失败检查 Prometheus 是否开启 --web.enable-lifecycle,且 block duration 是否为 2h
Query 查不到历史数据确认 Store Gateway 已启动、对象存储配置正确、Compactor 未损坏元数据
查询结果重复检查 --query.replica-label 是否与 external_labels 中的标签名一致
上传慢或失败检查对象存储凭据、网络带宽、桶权限及磁盘空间
Compactor 报错确认只有一个实例在运行,且 --data-dir 有足够空间

十一、生产环境建议

  • 使用 Kubernetes 部署时,优先通过 Helm 或 Operator 管理,凭据用 Secret 注入。
  • Query 层可多副本 + 负载均衡,实现高可用查询入口。
  • 对象存储启用版本控制或服务端加密,防止误删与泄露。
  • 为 Thanos 各组件配置告警,重点关注上传失败、Store 不可用、Compactor 长时间未运行。
  • 首次上线建议先在小流量集群验证 1–2 个上传周期,再逐步扩大范围。
7