系统学习 演练学习路径:35 个 lab 的难度排序、4 周基础计划与复盘方法

搭建 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 和静默规则,避免重复告警。

验证

回滚

常见坑

相关命令

参考链接

读完自测

合上页面,先回答,再展开对照。

  1. 两个 Prometheus 副本使用相同的 external labels 会怎样?
  2. 对象存储权限缺少 list 会导致什么后果?
  3. 只做了采集高可用、没有做 Alertmanager 高可用,会留下什么问题?
答案
  1. Thanos 会把它们当成重复序列。
  2. Store Gateway 启动失败。
  3. 告警仍然是单点。
微信小程序码:扫码打开命令速查小程序

小程序版

微信扫码,随时查命令