搭建 Prometheus 高可用与长期存储

适用场景

单机 Prometheus 存在数据丢失、查询瓶颈和容量上限。需要多副本采集、长期存储、跨集群统一查询,或要求 Prometheus 重启后历史数据仍可查。

前置条件

步骤

  1. 安装 kube-prometheus-stack,先确认副本数:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install monitoring prometheus-community/kube-prometheus-stack \
  --namespace monitoring --create-namespace \
  --set prometheus.replicas=2 \
  --set alertmanager.replicas=3
  1. 为 Prometheus 配置外部标签,保证 Thanos 能区分副本:
--set prometheus.prometheusSpec.externalLabels.cluster=prod
--set prometheus.prometheusSpec.replicaExternalLabelName=prometheus_replica
  1. 部署 Thanos Sidecar 或 Prometheus Agent + Remote Write。Sidecar 模式适合保留本地查询能力:
--set prometheus.prometheusSpec.thanos.objectStorageConfig.secretName=thanos-objstore
--set prometheus.prometheusSpec.thanos.objectStorageConfig.key=objstore.yml
  1. 配置对象存储:
cat > objstore.yml <<'EOF'
type: S3
config:
  bucket: thanos
  endpoint: minio.storage:9000
  access_key: minio
  secret_key: minio123
  insecure: true
EOF
kubectl -n monitoring create secret generic thanos-objstore --from-file=objstore.yml
  1. 部署 Thanos Query、Store Gateway、Compactor 和 Query Frontend:
helm repo add bitnami https://charts.bitnami.com/bitnami
helm install thanos bitnami/thanos \
  --namespace monitoring \
  --set query.replicaCount=2 \
  --set storegateway.enabled=true \
  --set compactor.enabled=true \
  --set objstoreConfig="$(cat objstore.yml)"
  1. 让 Grafana 数据源指向 Thanos Query,而不是单个 Prometheus。查询时按 cluster 和 prometheus_replica 去重。

  2. Alertmanager 组成集群,配置相同的 --cluster.peer 和静默规则,避免重复告警。

验证

回滚

常见坑

相关命令