Thanos —— 给 Prometheus 加上长期存储、全局查询与降采样的分布式套件
Thanos —— 给 Prometheus 加上长期存储、全局查询与降采样的分布式套件
分类:01 指标采集与时序存储 适用:已有多套 Prometheus / 多 Kubernetes 集群,需要无限期留存与统一查询视图 一句话:以一组可组合的组件无缝叠加在原生 Prometheus 之上,用对象存储换无限留存、用 Query 扇出换全局视图 官方:https://thanos.io/ | https://github.com/thanos-io/thanos 授权:Apache-2.0(CNCF Incubating 项目)
版本、镜像 tag、二进制资产名均以官方最新稳定版为准(见 releases 页);各组件 flag 以 thanos <component> --help 为准。
1. 它解决什么问题
| 痛点(原生 Prometheus 单点) | Thanos 的应对 |
|---|---|
| 本地 TSDB 保留期受磁盘限制 | Sidecar / Receiver 把 2 小时 TSDB 块上传到对象存储,留存近乎无限 |
| 多套 Prometheus 各自为政,无全局视图 | Query 通过 StoreAPI 扇出到所有 Sidecar / Store / Ruler / Receiver 并合并 |
| HA 双副本数据重复、无自动去重 | Query 基于 --query.replica-label 对 replica 标签实时去重 |
| 单机故障即数据丢失、查询不可用 | 块不可变且多副本存于对象存储;Query / Store 无状态可水平扩展 |
| 长周期查询扫原始点太慢 | Compactor 生成 5m / 1h 降采样数据,按分辨率设保留策略 |
不负责:采集与被监控端服务发现(仍由 Prometheus 完成)、对象存储本身、通知渠道落地(交 Alertmanager)。组件多、运维复杂度高于单机方案。
2. 核心概念与架构
关键术语
| 术语 | 含义 |
|---|---|
| TSDB Block | Prometheus 切出的 2 小时不可变块(ULID 目录 + meta.json + chunks/index),Thanos 存储与复制的最小单位 |
| StoreAPI | 组件间共享的 gRPC 契约(Series / LabelNames / LabelValues),Sidecar/Store/Receive/Ruler 均实现 |
| external labels | Prometheus 配置中全局唯一标识(cluster、replica 等),是跨实例识别与去重的依据 |
| Sidecar | 与 Prometheus 同机/Pod 运行,上传块 + 暴露本地实时数据给 StoreAPI |
| Store Gateway | 读取对象存储中的块,以 StoreAPI 对外提供历史数据,靠缓存加速 |
| Query / Query Frontend | 实现 Prometheus HTTP API 的全局查询层 / 查询拆分与缓存前端 |
| Compactor | 单例:压缩块、生成降采样、执行保留策略、清理垃圾 |
| Ruler | 基于全局视图评估告警/记录规则,可回写对象存储 |
| Receiver | 接收 Prometheus remote write,本地 TSDB + 上传块,可替代 Sidecar |
| downsampling | 由 Compactor 生成的 5m / 1h 低分辨率聚合数据 |
长期存储 + 全局查询 + 降采样如何解决 Prometheus 单点
[单点]:Prom(P1) --本地 2h 块--> 本地盘(有限留存) —— 宕机即丢、无全局视图、长查询慢
[Thanos]:
P1 + Sidecar --upload--> ┌──────────── 对象存储(S3/GCS/COS...) ────────────┐
P2 + Sidecar --upload--> │ raw 块(2h) + 5m/1h 降采样块 + meta.json │
Receive --upload-------> │ 不可变、可多副本、可版本控制 = 长期存储/备份 │
└───┬───────────────┬───────────────┬────────────┘
│ Store Gateway │ Compactor(单例:压缩/降采样/保留)
v v
Grafana --> Query Frontend --> Query <--StoreAPI 扇出--> Sidecar(实时) / Store(历史) / Ruler / Receive
| 基于 replica 标签去重、实现 Prometheus HTTP API
Ruler --规则评估--> Query --告警--> Alertmanager --> 邮件/Slack/Webhook
| 能力 | 机制 | 收益 |
|---|---|---|
| 长期存储 | 不可变块上传对象存储,与 Prometheus 生命周期解耦 | 无限留存、廉价的异地备份、Prometheus 可随意重建 |
| 全局查询 | Query 无状态扇出所有 StoreAPI 端点并合并 | 跨集群统一 PromQL 视图,Grafana 只配一个数据源 |
| 降采样 | Compactor 预聚合 5m / 1h 分辨率 | 月/年级范围查询从扫原始点变为扫聚合点,大幅提速降本 |
3. 安装与部署
3.1 环境准备
| 项 | 要求 |
|---|---|
| Prometheus | 需 v2.2.1+(版本兼容以官方 Makefile 的 PROM_VERSIONS 为准);启用 --web.enable-lifecycle,迁移快照需 --web.enable-admin-api |
| 对象存储 | 一个 bucket(GCS / AWS S3 / MinIO / Azure / Swift / 腾讯 COS / 阿里 OSS 等),配好 AK/SK 与最小权限 |
| external labels | 每个 Prometheus 必须配置全局唯一的 external_labels,HA 对用 replica: A / replica: B 区分 |
| 时钟 / 网络 | 全组件 NTP 同步;Query 必须能路由到所有 StoreAPI 的 gRPC 端口 |
| 磁盘 | Store / Compactor / Receive / Ruler 需本地 --data-dir(Compactor 临时空间按并发 × 最大块估算) |
3.2 多种安装方式
Thanos 是单一二进制(一个 thanos 文件 + 子命令覆盖全部组件),官方不提供 deb/rpm 包。
A. 二进制 + systemd
# 资产名以 releases 页为准(常见命名格式)
curl -LO "https://github.com/thanos-io/thanos/releases/download/<version>/thanos-<version>.linux-amd64.tar.gz"
tar xzf thanos-<version>.linux-amd64.tar.gz
install -m 0755 thanos-<version>.linux-amd64/thanos /usr/local/bin/thanos
thanos --version
useradd -r -s /sbin/nologin thanos && mkdir -p /var/lib/thanos/{store,compact,rule,receive} && chown -R thanos:thanos /var/lib/thanos
# /etc/systemd/system/thanos-query.service(其他组件改 ExecStart 子命令即可)
[Unit]
Description=Thanos Query
After=network-online.target
[Service]
User=thanos
Restart=on-failure
ExecStart=/usr/local/bin/thanos query --http-address=0.0.0.0:10904 --grpc-address=0.0.0.0:10903 --store=<sidecar>:10901 --store=<store>:10905 --query.replica-label=replica
[Install]
WantedBy=multi-user.target
systemctl daemon-reload && systemctl enable --now thanos-query
B. Docker / docker compose
services:
query:
image: quay.io/thanos/thanos:<version>
command: ["query", "--http-address=0.0.0.0:10904", "--grpc-address=0.0.0.0:10903",
"--store=sidecar:10901", "--store=store:10905", "--query.replica-label=replica"]
ports: ["10904:10904"]
store:
image: quay.io/thanos/thanos:<version>
volumes: ["./bucket.yml:/etc/thanos/bucket.yml:ro", "./store-data:/data"]
command: ["store", "--data-dir=/data", "--objstore.config-file=/etc/thanos/bucket.yml",
"--grpc-address=0.0.0.0:10905", "--http-address=0.0.0.0:10906"]
镜像也可用 Docker Hub 镜像 thanosio/thanos:<version>;生产固定版本 tag,勿用 latest。
C. Kubernetes Helm / Jsonnet
helm repo add bitnami https://charts.bitnami.com/bitnami && helm repo update
helm install thanos bitnami/thanos -n monitoring --create-namespace -f values.yaml
官方另提供 kube-thanos(Jsonnet)用于声明式部署;也可在 kube-prometheus-stack 中启用 Thanos Sidecar。
D. 源码构建
git clone https://github.com/thanos-io/thanos && cd thanos
make build # 需 Go 工具链,产物在 ./tmp/bin 或 $GOPATH/bin,以官方 Makefile 为准
3.3 首次启动与验证
thanos --version
curl -s http://<query>:10904/-/healthy && echo
curl -sG 'http://<query>:10904/api/v1/query' --data-urlencode 'query=up' | head -c 300
# 校验对象存储可读(无块时返回空属正常)
thanos tools bucket ls --objstore.config-file=/etc/thanos/bucket.yml
浏览器打开 http://<query>:10904 可见类 Prometheus 的查询 UI 与 Store 端点状态。
4. 配置文件详解
主配置:Thanos 没有单一主配置文件,各组件用命令行 flag;唯一共享的关键文件是对象存储配置 --objstore.config-file(或内联 --objstore.config)与 Prometheus 侧的 external_labels。
| 配置载体 | 路径 / flag | 说明 |
|---|---|---|
| 对象存储 | --objstore.config-file=/etc/thanos/bucket.yml |
Sidecar / Store / Compactor / Ruler / Receive 共用 |
| Prometheus | prometheus.yml 的 global.external_labels |
必须全局唯一,含 replica 供去重 |
| 组件本地缓存 | --data-dir |
Store 索引缓存、Compactor 临时块、Receive/Ruler 本地 TSDB |
| 规则文件 | Ruler --rule-file=/etc/thanos/rules/*.yml |
语法同 Prometheus rule YAML |
| hashring | Receive --receive.hashrings-file |
多副本软租户/分片路由 |
| 服务发现 | Query --store=dnssrv+_grpc._tcp.<svc> |
静态地址或 DNS SRV 发现 StoreAPI |
对象存储关键项(S3 / MinIO,官方 type: S3)
| 字段 | 含义 | 说明 |
|---|---|---|
type: S3 |
后端类型 | MinIO / Ceph 等 S3 兼容存储同样用此类型 |
config.bucket |
bucket 名 | 必需 |
config.endpoint |
S3 端点 | 必需;AWS 可留空走默认 |
config.access_key / secret_key |
凭证 | 未启用 aws_sdk_auth 时必需,建议用环境变量/密钥文件 |
config.region |
区域 | 部分 S3 兼容后端需要 |
config.insecure |
使用 HTTP | 仅测试环境设为 true |
config.signature_version2 |
签名版本 | AWS 需 false(v4);部分兼容实现需 true |
config.bucket_lookup_type |
寻址风格 | auto / path / virtual-hosted |
prefix |
bucket 内前缀 | 多套 Thanos 共用一个 bucket 时隔离 |
完整可运行最小示例
# /etc/thanos/bucket.yml —— S3 / MinIO 最小配置(至少需 bucket / endpoint / access_key / secret_key)
type: S3
config:
bucket: "<bucket>"
endpoint: "<s3-endpoint>"
access_key: "<ak>"
secret_key: "<sk>"
insecure: false
signature_version2: false
# prometheus.yml —— external_labels 与 lifecycle(Sidecar 依赖后者做热重载)
global:
scrape_interval: 15s
external_labels:
cluster: "cn-east-1"
replica: "A"
thanos sidecar --tsdb.path=/var/prometheus --prometheus.url=http://localhost:9090 \
--objstore.config-file=/etc/thanos/bucket.yml \
--grpc-address=0.0.0.0:10901 --http-address=0.0.0.0:10902
thanos query --http-address=0.0.0.0:10904 --grpc-address=0.0.0.0:10903 \
--store=localhost:10901 --query.replica-label=replica
5. 核心使用方法(各组件启动参数 / CLI / HTTP API / Web UI)
各组件启动参数(对象存储配置见第 4 章)
thanos sidecar --tsdb.path=/var/prometheus --prometheus.url=http://localhost:9090 --objstore.config-file=/etc/thanos/bucket.yml --grpc-address=0.0.0.0:10901 --http-address=0.0.0.0:10902
thanos store --data-dir=/var/lib/thanos/store --objstore.config-file=/etc/thanos/bucket.yml --grpc-address=0.0.0.0:10905 --http-address=0.0.0.0:10906 --index-cache-size=2GB --chunk-pool-size=2GB
thanos query --http-address=0.0.0.0:10904 --grpc-address=0.0.0.0:10903 --store=<sidecar>:10901 --store=<store>:10905 --store=dnssrv+_grpc._tcp.thanos-store.monitoring.svc --query.replica-label=replica
thanos query-frontend --http-address=0.0.0.0:10913 --query-frontend.downstream-url=http://<query>:10904 --query-range.split-interval=24h
thanos compact --data-dir=/var/lib/thanos/compact --objstore.config-file=/etc/thanos/bucket.yml --http-address=0.0.0.0:10912 --compact.concurrency=1 --downsample.concurrency=1 --retention.resolution-raw=30d --retention.resolution-5m=180d --retention.resolution-1h=0d
thanos rule --data-dir=/var/lib/thanos/rule --rule-file=/etc/thanos/rules/*.yml --query=<query>:10904 --alertmanagers.url=http://<am>:9093 --objstore.config-file=/etc/thanos/bucket.yml --label=replica='"A"' --grpc-address=0.0.0.0:10910 --http-address=0.0.0.0:10911 --eval-interval=30s
thanos receive --tsdb.path=/var/lib/thanos/receive --objstore.config-file=/etc/thanos/bucket.yml --label=receive_replica='"0"' --remote-write.address=0.0.0.0:10908 --grpc-address=0.0.0.0:10907 --http-address=0.0.0.0:10909 --receive.replication-factor=1
thanos tools bucket verify --objstore.config-file=/etc/thanos/bucket.yml
thanos tools bucket ls --objstore.config-file=/etc/thanos/bucket.yml
thanos tools bucket inspect --objstore.config-file=/etc/thanos/bucket.yml
thanos tools bucket web --objstore.config-file=/etc/thanos/bucket.yml --http-address=0.0.0.0:10914
端口取官方 getting-started 的单机多组件布局建议(10901~10913);组件自身默认
--grpc-address=0.0.0.0:10901、--http-address=0.0.0.0:10902,多组件同机须显式错开,以thanos <component> --help为准。
HTTP API 与 Web UI(Query 兼容 Prometheus v1 API)
curl -s http://<query>:10904/-/healthy
curl -sG 'http://<query>:10904/api/v1/query' --data-urlencode 'query=up'
curl -sG 'http://<query>:10904/api/v1/query_range' --data-urlencode 'query=rate(http_requests_total[5m])' --data-urlencode 'start=<unix_s>' --data-urlencode 'end=<unix_s>' --data-urlencode 'step=3600s'
curl -sG 'http://<query>:10904/api/v1/label/cluster/values'
curl -s http://<store>:10906/metrics | grep thanos_store
curl -s http://<compact>:10912/metrics | grep thanos_compact
curl -s -X POST http://<rule>:10911/-/reload
curl -s http://<rule>:10911/api/v1/rules
Web UI:
http://<query>:10904(类 Prometheus UI,含 Stores 页查看已发现端点与健康状态);thanos tools bucket web提供 bucket 块浏览。以上共 25+ 条可用命令。
6. 告警与通知
链路:Ruler(基于全局 Query 评估规则)→ Alertmanager(分组/抑制/静默/路由)→ 邮件 / Slack / Webhook。
# /etc/thanos/rules/alert.yml(语法同 Prometheus rule YAML)
groups:
- name: global
rules:
- alert: PrometheusDown
expr: up{job="prometheus"} == 0
for: 5m
labels: {severity: critical}
annotations: {summary: "{{ $labels.cluster }} Prometheus 掉线"}
- record: cluster:up:sum
expr: sum by (cluster) (up)
thanos rule --data-dir=/var/lib/thanos/rule --rule-file=/etc/thanos/rules/*.yml \
--query=<query>:10904 --alertmanagers.url=http://<am>:9093 \
--objstore.config-file=/etc/thanos/bucket.yml --eval-interval=30s \
--grpc-address=0.0.0.0:10910 --http-address=0.0.0.0:10911
| 对接对象 | 方式 |
|---|---|
| Alertmanager | Ruler --alertmanagers.url(支持多副本 / 文件 SD);通知渠道在 Alertmanager receiver 配置 |
| Grafana 查询 | 添加 Prometheus 数据源 → URL 填 http://<query>:10904(或 Query Frontend) |
| Grafana Alerting | 指向 Query 的 Prometheus API;或改用 Ruler 统一评估,避免双份规则 |
| 规则热加载 | POST http://<rule>:10911/-/reload;/api/v1/rules 与 /api/v1/alerts 查看状态 |
| 记录规则回写 | Ruler 配 --objstore.config-file 后把结果块上传对象存储,供全局查询 |
7. 实战场景
场景一:把现有 Prometheus 数据平滑接入 / 迁移到 Thanos(零停机)
目标:不动现有抓取,逐步把历史块上传对象存储并建立全局查询,随时可回退。
# ① 建 bucket 并写 /etc/thanos/bucket.yml(第 4 章),校验连通
thanos tools bucket ls --objstore.config-file=/etc/thanos/bucket.yml
# ② 给源 Prometheus 补 external_labels 并启用 lifecycle(改配置后 reload,勿重启)
curl -s -X POST http://<prom>:9090/-/reload
# ③ 同机启动 Sidecar:先只做查询代理,再开启上传
thanos sidecar --tsdb.path=/var/prometheus --prometheus.url=http://localhost:9090 \
--grpc-address=0.0.0.0:10901 --http-address=0.0.0.0:10902
thanos sidecar --tsdb.path=/var/prometheus --prometheus.url=http://localhost:9090 \
--objstore.config-file=/etc/thanos/bucket.yml \
--grpc-address=0.0.0.0:10901 --http-address=0.0.0.0:10902
# ④ 启动 Store Gateway + Query(历史 + 实时一起查)
thanos store --data-dir=/var/lib/thanos/store --objstore.config-file=/etc/thanos/bucket.yml --grpc-address=0.0.0.0:10905 --http-address=0.0.0.0:10906
thanos query --http-address=0.0.0.0:10904 --grpc-address=0.0.0.0:10903 --store=<sidecar>:10901 --store=<store>:10905 --query.replica-label=replica
# ⑤ 双端对比后把 Grafana 数据源由 Prometheus 切到 http://<query>:10904
curl -sG 'http://<prom>:9090/api/v1/query' --data-urlencode 'query=count(up)'
curl -sG 'http://<query>:10904/api/v1/query' --data-urlencode 'query=count(up)'
# prometheus.yml 追加/修改
global:
external_labels:
cluster: "cn-east-1"
replica: "A"
⚠️ 若 bucket 为空且本地已有大量历史块,可对首次接入的 Sidecar 加 --shipper.upload-compacted 上传已压缩块;若该 bucket 已有其它 Sidecar 上传过块,不要使用,否则产生重叠块冲突(需人工清理)。
验证:Query 的 Stores 页全部端点健康;thanos tools bucket ls 出现新块;count(up) 与源一致;隔日查询仍能命中历史。
回滚:停 Sidecar / Store / Query,Grafana 数据源改回原 Prometheus;bucket 中已上传块保留不删,可随时重接。源 Prometheus 本地 TSDB 全程未动。
场景二:多集群全局查询 + HA 副本去重
目标:两个集群各跑一对 HA Prometheus,Query 出一份不重复的全局视图。
# 集群 A 的 prometheus.yml(B 把 cluster 改 us-west-1,两副本分别 replica: A / B)
global:
external_labels:
cluster: "cn-east-1"
replica: "A"
thanos query --http-address=0.0.0.0:10904 --grpc-address=0.0.0.0:10903 \
--store=<cn-a1>:10901 --store=<cn-a2>:10901 \
--store=<us-b1>:10901 --store=<us-b2>:10901 \
--store=<store>:10905 --query.replica-label=replica --query.auto-downsampling=true
# 验证:同一时刻每序列只返回一份
curl -sG 'http://<query>:10904/api/v1/query' --data-urlencode 'query=count(up) by (cluster)'
验证:count by (cluster) 不随副本数翻倍;Query UI 的 Stores 页 4 个 Sidecar 均 UP。
回滚:去掉 --query.replica-label 即恢复原始多副本视图(会出现重复),或临时把 Grafana 指回单集群 Prometheus。
场景三:Receiver 接管 remote write + Compactor 降采样长期保留
目标:Prometheus 不落本地长期数据,remote write 直接进 Thanos,按分辨率分级留存。
# prometheus.yml
remote_write:
- url: http://<receive>:10908/api/v1/receive
queue_config:
max_samples_per_send: 10000
thanos receive --tsdb.path=/var/lib/thanos/receive --objstore.config-file=/etc/thanos/bucket.yml \
--label=receive_replica='"0"' --remote-write.address=0.0.0.0:10908 \
--grpc-address=0.0.0.0:10907 --http-address=0.0.0.0:10909 --receive.replication-factor=1
thanos compact --data-dir=/var/lib/thanos/compact --objstore.config-file=/etc/thanos/bucket.yml \
--http-address=0.0.0.0:10912 --retention.resolution-raw=30d \
--retention.resolution-5m=180d --retention.resolution-1h=2y
thanos tools bucket verify --objstore.config-file=/etc/thanos/bucket.yml
验证:curl -s http://<receive>:10909/metrics | grep thanos_receive 写入计数增长;bucket 出现块;长范围查询走 5m/1h 降采样(Query 日志/耗时明显下降)。
回滚:remote_write 改回原目标或删除并 reload Prometheus;停 Receive / Compactor,bucket 数据保留;⚠️ 勿同时启第二个写同一 bucket 的 Compactor。
8. 日常运维与故障排查
systemctl status thanos-query thanos-store thanos-compact thanos-sidecar
journalctl -u thanos-query -f --since "10 min ago"
docker logs -f thanos-store 2>&1 | grep -iE 'error|warn'
curl -s http://<query>:10904/-/healthy
curl -s http://<query>:10904/metrics | grep thanos_query
thanos tools bucket verify --objstore.config-file=/etc/thanos/bucket.yml
thanos tools bucket ls --objstore.config-file=/etc/thanos/bucket.yml
| 现象 | 原因 | 处置 |
|---|---|---|
| 历史数据查不到 | Store 未启动 / Query --store 漏配 / bucket 权限不足 |
查 Query Stores 页、thanos tools bucket ls、核对 AK/SK 只读权限 |
| 指标数值翻倍 | 未配 --query.replica-label=replica |
Query 增加 replica 标签去重 |
| 部分集群数据缺失 | external_labels 不唯一被去重/覆盖 | 保证 cluster 全局唯一,HA 对只在 replica 上不同 |
| Sidecar 报 reload 失败 | Prometheus 未加 --web.enable-lifecycle |
启用该 flag 并重启 Prometheus |
| 块重叠 / Compactor 报错 | ⚠️ 多个 Compactor 同时写同一 bucket,或误用 --shipper.upload-compacted |
全局只保留一个 Compactor;清理重叠块(先人工确认) |
| 长周期查询超时 | 未开降采样或 Query Frontend 未拆分 | 启 Compactor 降采样、加 --query-range.split-interval |
| Receive 写入 404 | hashring / --remote-write.address 与 Prometheus URL 不一致 |
核对端口与 hashring 配置 |
9. 性能、容量与高可用
| 项 | 公式 / 经验 |
|---|---|
| 对象存储容量 | bucket ≈ raw_block_per_day × retention_days × replica_factor × (1 + 5m/1h 降采样附加);块为 2 小时不可变单元,须按自身样本实测 |
| 降采样收益 | 5m 约原始点数的 1/10 量级、1h 更小(随采样间隔变化),配合 --retention.resolution-* 分级留存 |
| Compactor 本地盘 | ≈ --compact.concurrency × 最大待合并块大小,需预留临时空间;--wait 表示跑一轮即退出(适合 cron) |
| Store Gateway 内存 | ≈ --index-cache-size + --chunk-pool-size + 并发查询开销,大 bucket 建议接 memcached 缓存桶 |
| Query / Query Frontend | 无状态,按 QPS 水平扩副本;Frontend 做区间拆分与结果缓存 |
| 调优参数 | --query.replica-label、--query.auto-downsampling、--index-cache-size、--chunk-pool-size、--compact.concurrency、--downsample.concurrency、--store.response-timeout(以 --help 为准) |
- 高可用:对象存储自身多副本即数据 HA;Sidecar 与每个 Prometheus 同生命周期;Query / Store / Ruler / Receive 多副本无状态扩展;Compactor 必须单例(⚠️ 同一 bucket 只能一个写入者)。
- 多租户:可用
prefix隔离多套 Thanos、或 Receive 的 hashring +--label做软租户。 - 降采样 / 对象存储:Compactor 生成 5m/1h 块并执行 raw/5m/1h 分级保留;GCS / S3 / Azure / Swift / 腾讯 COS / 阿里 OSS / OCI 等后端,MinIO 等 S3 兼容存储用
type: S3。 - 自监控:每个组件
--http-address暴露/metrics(thanos_*前缀),由独立 Prometheus 抓取;官方仓库提供 alerts/rules 示例与 Grafana dashboard,务必单独监控对象存储与 Compactor 滞后。
10. 安全加固、备份与升级
| 项 | 做法 |
|---|---|
| 对象存储凭证 | ⚠️ 最小权限:Store / Query 只读,Sidecar / Receive / Compactor / Ruler 才需写;用 IAM Role / 短期凭证,勿硬编码进镜像 |
| 传输加密 | bucket.yml 用 HTTPS(insecure: false)+ CA;组件间 gRPC 可配服务端/客户端 TLS(flag 以 --help 为准);Query 前置反代终止 TLS |
| 认证与暴露面 | 组件默认无认证:--http-address / --grpc-address 仅绑内网,用 Ingress/反代加认证;⚠️ 勿把 Compactor / bucket web 暴露公网 |
| 备份 | 对象存储本身即备份,开启 bucket 版本控制与跨区复制;定期 thanos tools bucket verify 校验块完整性 |
| 恢复 | 新环境用同一 bucket.yml 起 Store + Query 即可读回;块不可变,thanos tools bucket inspect 排查损坏块 |
| 升级 | 组件可独立滚动:先 Query / Store / Query Frontend,再 Sidecar / Ruler / Receive,最后 Compactor;先读 CHANGELOG |
| 回滚 | 保留旧二进制/镜像 tag,按反向还原;⚠️ 若新版本写过新格式块,回滚前确认旧版本可读,先在预发验证 |
# 升级前:校验块完整性并留存配置
thanos tools bucket verify --objstore.config-file=/etc/thanos/bucket.yml
cp /etc/thanos/bucket.yml /etc/thanos/bucket.yml.bak
# 逐组件滚动(以 Query 为例)
systemctl stop thanos-query && install -m 0755 thanos /usr/local/bin/thanos && systemctl start thanos-query
curl -s http://<query>:10904/-/healthy
11. 常见坑
| # | 现象 | 原因 | 解决 |
|---|---|---|---|
| 1 | ⚠️ 块损坏 / 重复压缩报错 | 多个 Compactor 同时写同一 bucket | 全局只保留一个 Compactor 实例 |
| 2 | 数据被去重/覆盖、指标缺失 | external_labels 不唯一 | cluster 全局唯一,仅 replica 区分 HA 副本 |
| 3 | Sidecar 无法热重载 | Prometheus 缺 --web.enable-lifecycle |
启用后重启 Prometheus |
| 4 | ⚠️ --shipper.upload-compacted 产生重叠块 |
对已有数据的 bucket 二次上传压缩块 | 仅首次空 bucket 使用,冲突需人工清理 |
| 5 | 查询结果数值翻倍 | 未配 --query.replica-label |
Query 增加该 flag |
| 6 | ⚠️ AK/SK 硬编码进镜像并泄露 | 凭证管理不当 | 用 IAM Role / Secret / 环境变量,权限最小化 |
| 7 | Compactor 跑一轮就退出 | 误加 --wait 当常驻参数 |
常驻模式去掉 --wait,或明确交给 cron |
| 10 | ⚠️ 直接删 bucket 里的块"清数据" | 破坏块引用关系 | 用 --retention.resolution-* 让 Compactor 回收,勿手工删 |
| 11 | Query 只查到部分数据 | --store 漏配或 DNS SRV 不生效 |
核对 Stores 页与 --store 列表 |
| 13 | ⚠️ 生产用 latest 镜像 |
组件版本飘移、StoreAPI 不兼容 | 固定统一版本 tag,全组件同版 |
| 14 | Filesystem 后端上生产 | 官方明确仅测试/演示用 | 换 GCS / S3 等生产级对象存储 |
12. 速查表
| 类别 | 项 | 值 / 命令 |
|---|---|---|
| 子命令 | 全部组件 | sidecar store query query-frontend compact rule receive tools |
| 端口 | Sidecar gRPC / HTTP | 10901 / 10902 |
| 端口 | Query gRPC / HTTP | 10903 / 10904 |
| 端口 | Store gRPC / HTTP | 10905 / 10906 |
| 端口 | Receive gRPC / remote write / HTTP | 10907 / 10908 / 10909 |
| 端口 | Rule gRPC / HTTP | 10910 / 10911 |
| 端口 | Compact HTTP / Query Frontend HTTP | 10912 / 10913 |
| 默认 flag | 组件默认 gRPC / HTTP | 0.0.0.0:10901 / 0.0.0.0:10902 |
| 配置 | 对象存储 | --objstore.config-file=/etc/thanos/bucket.yml(type: S3) |
| 配置 | Prometheus 唯一标识 | global.external_labels(含 replica) |
| 配置 | 本地缓存 / 规则 / hashring | --data-dir / --rule-file / --receive.hashrings-file |
| 参数 | 全局查询去重 | --query.replica-label=replica |
| 参数 | 分级保留 | --retention.resolution-raw / -5m / -1h |
| API | 健康 / 自监控 | GET /-/healthy、GET /metrics |
| API | 查询 / 标签 | GET /api/v1/query、query_range、series、labels、label/<name>/values |
| API | Receive 写入 | POST /api/v1/receive |
| CLI | bucket 运维 | thanos tools bucket {ls|inspect|verify|web} |
| 镜像 | 官方镜像 | quay.io/thanos/thanos:<version>、thanosio/thanos:<version> |
13. 选型建议与同类对比
| 维度 | Thanos | VictoriaMetrics | Prometheus | Grafana Mimir | Cortex |
|---|---|---|---|---|---|
| 定位 | Prometheus 之上的长期存储/全局查询层 | 一体化远程 TSDB + 套件 | 单机抓取 + 本地 TSDB | 大规模多租户后端 | 多租户 Prometheus 后端 |
| 存储核心 | 对象存储(不可变块) | 本地高压缩盘 + 快照 | 本地盘有限 | 对象存储 | 对象存储 |
| 与现有 Prometheus | 无缝叠加(Sidecar) | 需 remote write 改造 | —— | 需 remote write | 需 remote write |
| 全局查询 | Query 扇出 + 去重 | vmselect 原生 |
无 | 原生 | 原生 |
| 降采样 | Compactor 5m/1h 原生 | 记录规则/流聚合 | 无 | 原生 | 部分 |
| 运维复杂度 | 高(7+ 组件) | 低~中 | 最低 | 高 | 高 |
| 查询语言 | PromQL | MetricsQL ⊃ PromQL | PromQL | PromQL | PromQL |
何时选:已有多套 Prometheus / 多集群、必须保留原生架构并叠加无限留存与全局视图;需要对象存储级异地备份与降采样;团队能承担组件化运维。 何时不选:只有一两套 Prometheus 且规模小(原生 Prometheus 或单节点 VictoriaMetrics 更省);不愿引入对象存储;追求最少组件与最低运维成本;核心诉求是日志/链路追踪。
14. 参考链接
- 官网:https://thanos.io/
- 入门(含官方端口布局):https://thanos.io/tip/thanos/getting-started.md/
- 对象存储配置:https://thanos.io/tip/thanos/storage.md/
- GitHub 仓库:https://github.com/thanos-io/thanos
- Releases(下载与版本):https://github.com/thanos-io/thanos/releases
- 官方镜像:https://quay.io/thanos/thanos
- kube-thanos(Jsonnet):https://github.com/thanos-io/kube-thanos
- Bitnami Helm Charts:https://github.com/bitnami/charts