如何开启 Istio 的 Prometheus 远程写入到 Grafana Cloud?

解读

面试官想验证三件事:

  1. 你是否理解 Istio 内置 Prometheus 的“remoteWrite”机制;
  2. 你是否熟悉 Grafana Cloud 的身份认证与端点格式(国内网络环境下还需考虑链路加速);
  3. 你是否能把“改配置→发数据→验数据→做高可用”四步闭环讲清楚,而不是只背文档。

知识点

  • Istio 安装时通过 istio-system/prometheus ConfigMap 注入 Prometheus 配置;
  • Grafana Cloud 提供 HTTP Basic Auth 的远程写入地址,格式为 https://<user>:<token>@prometheus-blocks-prod-us-central1.grafana.net/api/prom/push;
  • 国内集群需给 Prometheus 容器注入 HTTPS_PROXY 或自建 Grafana Agent 做边缘转发,否则 443 端口易被限速;
  • 远程写入会放大内存,需调 max_samples_per_send、capacity、queue_config 防止 OOM;
  • 验证链路是否通,用 Grafana Cloud 的 Explore 界面查 up{job="istio-mesh"};
  • 生产环境应开启 pprof 与 wal 快照,并给 Prometheus 加 Thanos Sidecar 做长期落盘,避免只依赖云端存储。

答案

分五步落地:

  1. 在 Grafana Cloud 控制台创建 “Metrics” 实例,复制 User ID 与 API Key(权限选 MetricsPublisher)。
  2. 回到集群,执行
    kubectl -n istio-system edit configmap prometheus
    在 prometheus.yml 末尾追加:
    remoteWrite:
    - url: "https://prometheus-blocks-prod-us-central1.grafana.net/api/prom/push"
      basic_auth:
        username: <User ID>
        password: <API Key>
      queue_config:
        max_samples_per_send: 1000
        capacity: 25000
        max_shards: 50
      write_relabel_configs:
      - source_labels: [__name__]
        regex: 'istio_(.*)|envoy_(.*)|up|go_.*'
        action: keep
    
    只上传 Istio 相关指标,节省流量。
  3. 国内集群若出现 TLS handshake timeout,给 Prometheus 的 StatefulSet 注入环境变量
    HTTPS_PROXY=http://internal-squid:3128
    或使用 Grafana Agent 做本地聚合再转推,降低跨境延迟。
  4. 重启 Prometheus Pod 后,在 Grafana Cloud 的 Explore 输入 up{job="istio-mesh"},能看到值 1 即成功。
  5. 最后把 Prometheus 本地 retention 降到 1d,只用作短时容错,长期存储完全依赖 Grafana Cloud,实现 “轻本地、重云端” 的 SRE 策略。

拓展思考

  • 如果 Istio 版本 ≥1.16,官方已废弃内置 Prometheus,推荐 istio-samples/addons/prometheus 或 kube-prometheus-stack,此时 remoteWrite 配置应写到 PrometheusRule CRD 的 additionalScrapeConfig 与 remoteWrite 字段,并通过 Helm values 管理,方便 GitOps 审计。
  • 多集群场景下,可在每个集群部署 Grafana Agent Operator,统一 remote_write 到同一个 Grafana Cloud 实例,用 external_labels 标记 cluster_id,实现 “一次看图,多集群对比”。
  • 国内金融客户常因合规要求禁止公网出口,此时可在本地 VPC 搭建 Grafana Enterprise Metrics (GEM) 作为中转,再经 专线或 VPN 把压缩块上传到 Grafana Cloud,既满足监管又保留云原生弹性。