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 示例:
inirate(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 上传速率
scssrate(thanos_sidecar_upload_duration_seconds_count[5m])
-
Thanos Store 读取速率
scssrate(thanos_store_series_fetch_duration_seconds_count[5m])
-
Thanos Query 请求速率
inirate(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 具备高可用、长期存储、跨集群查询能力 ,适用于 大规模监控场景!🚀