跳到主要内容

Prometheus 高可用与长期存储:Thanos 实战指南

这篇要解决什么问题

  • Prometheus 数据 15 天就滚掉,历史指标查不到。
  • 单个 Prometheus 宕机或节点重建时监控空窗。
  • 多集群/多实例 Prometheus 想要统一查询和告警。

Thanos 能在不大改现有 Prometheus 的前提下提供持久化和全局视图。下面用最少的配置跑通 Demo,再给出生产化的要点。

准备条件

  • Kubernetes >= 1.24,已运行至少一个 Prometheus(假设命名空间 monitoring,服务名 prometheus-k8s)。
  • 访问对象存储的密钥(本示例用 MinIO 说明配置格式)。

步骤一:准备对象存储(MinIO 示例,可换 S3/GCS/COS)

  1. 启动一个简单的 MinIO(演示用):
kubectl create namespace thanos --dry-run=client -o yaml | kubectl apply -f -
kubectl -n thanos apply -f https://raw.githubusercontent.com/minio/operator/master/examples/tenant-lite.yaml

生产请使用现有对象存储,并创建桶(如 thanos)。

  1. 创建访问凭证 Secret(示例值请替换):
kubectl -n thanos create secret generic minio-credentials \
--from-literal=access_key=minio \
--from-literal=secret_key=minio123

步骤二:部署 Thanos Sidecar 绑定现有 Prometheus

在 Prometheus Deployment/StatefulSet 增加一个 sidecar 容器:

# 仅示意关键部分
- name: thanos-sidecar
image: quay.io/thanos/thanos:v0.35.0
args:
- sidecar
- --tsdb.path=/prometheus
- --prometheus.url=http://127.0.0.1:9090
- --objstore.config=$(OBJSTORE_CONFIG)
- --http-address=0.0.0.0:19191
- --grpc-address=0.0.0.0:10901
env:
- name: OBJSTORE_CONFIG
value: |
type: s3
config:
bucket: thanos
endpoint: minio.thanos.svc:9000
access_key: minio
secret_key: minio123
insecure: true
ports:
- containerPort: 10901
name: grpc
- containerPort: 19191
name: http
volumeMounts:
- name: prometheus-storage
mountPath: /prometheus

记得打开 Service 以暴露 gRPC 端口给 Thanos Querier/StoreGateway 使用。

步骤三:部署 Thanos Query + Store Gateway + Compactor

# thanos.yaml(核心组件示例)
apiVersion: apps/v1
kind: Deployment
metadata:
name: thanos-query
namespace: thanos
spec:
replicas: 1
selector:
matchLabels: {app: thanos-query}
template:
metadata:
labels: {app: thanos-query}
spec:
containers:
- name: query
image: quay.io/thanos/thanos:v0.35.0
args:
- query
- --store=prometheus-k8s.monitoring.svc:10901
- --store=thanos-storegateway.thanos.svc:10901
- --query.replica-label=prometheus_replica
ports:
- containerPort: 10902
name: http
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: thanos-storegateway
namespace: thanos
spec:
replicas: 1
selector:
matchLabels: {app: thanos-storegateway}
template:
metadata:
labels: {app: thanos-storegateway}
spec:
containers:
- name: store
image: quay.io/thanos/thanos:v0.35.0
args:
- store
- --objstore.config=$(OBJSTORE_CONFIG)
- --http-address=0.0.0.0:19191
- --grpc-address=0.0.0.0:10901
env:
- name: OBJSTORE_CONFIG
value: |
type: s3
config:
bucket: thanos
endpoint: minio.thanos.svc:9000
access_key: minio
secret_key: minio123
insecure: true
ports:
- containerPort: 10901
name: grpc
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: thanos-compactor
namespace: thanos
spec:
replicas: 1
selector:
matchLabels: {app: thanos-compactor}
template:
metadata:
labels: {app: thanos-compactor}
spec:
containers:
- name: compactor
image: quay.io/thanos/thanos:v0.35.0
args:
- compact
- --objstore.config=$(OBJSTORE_CONFIG)
- --http-address=0.0.0.0:19191
env:
- name: OBJSTORE_CONFIG
valueFrom:
secretKeyRef:
name: minio-credentials
key: objstore-config
ports:
- containerPort: 19191
name: http

应用:

kubectl apply -f thanos.yaml
kubectl -n thanos get pods

为了简化示例,Compactor 直接用同样的 objstore 配置。生产环境应加入 --retention.resolution-* 参数控制保留策略,并做好持久化卷。

步骤四:验证全局查询和持久化

  1. 端口转发 Thanos Query:
kubectl -n thanos port-forward deploy/thanos-query 10902:10902

浏览器打开 http://localhost:10902,在 UI 中可看到已注册的 Stores(sidecar + storegateway)。

  1. PromQL 测试:在 Thanos Query UI 执行 up,应能返回 Prometheus 的数据;删除 Prometheus Pod 后 Sidecar 会重启,但历史数据仍可通过 StoreGateway 读取。

  2. 对象存储检查:登录 MinIO,确认桶内出现 block 目录,证实数据已上传。

排障清单

  • Store/Sidecar 未连上 Query:检查 gRPC 端口是否暴露、Service 名称是否写对;查看 Pod 日志是否有 store node is not ready
  • 对象存储 403/超时:确认 endpoint/密钥正确,是否需要 insecure: true;网络安全组是否放行 9000/443。
  • Query 无数据:确认 Sidecar 参数 --prometheus.url 指向本地 Prometheus;Prometheus 数据目录挂载与 Sidecar 一致。
  • Compactor 卡住:检查磁盘不足或对象存储配额;适当调低并发,添加 PVC 作为临时工作目录。

生产落地要点

  • Sidecar 与 Prometheus 同节点亲和,避免跨节点访问本地存储。
  • 使用外部对象存储(S3/COS/GCS),配置版本化与生命周期管理控制成本。
  • 配置多副本 Prometheus(ReplicaLabel),Thanos Query 设置 --query.replica-label 去重。
  • 为 StoreGateway/Query/Compactor 配置持久化和资源限制,必要时加 HPA。
  • 将所有 YAML/Helm values 纳入 GitOps,发布前 kubectl apply --server-dry-run 校验。

总结

Thanos 让 Prometheus 拥有高可用和长期存储能力。按上面的步骤部署 Sidecar、对象存储、Query/Store/Compactor,就能在本地试出全局查询和数据留存效果。把存储换成云厂商对象存储、加上多副本 Prometheus 与保留策略,你就能把它带到生产环境,告别监控空窗和历史数据缺失。