Prometheus

Prometheus 是一个开源监控系统和时间序列数据库。您可以将 Prometheus 与 Istio 一起使用来记录跟踪 Istio 和服务网格中应用程序运行状况的指标。您可以使用 GrafanaKiali 等工具可视化指标。

安装

选项 1:快速入门

Istio 提供了一个基本的示例安装,以便快速启动并运行 Prometheus

$ kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.24/samples/addons/prometheus.yaml

这会将 Prometheus 部署到您的集群中。这仅用于演示,未针对性能或安全性进行调整。

选项 2:可自定义的安装

请参阅Prometheus 文档以开始将 Prometheus 部署到您的环境中。请参阅配置以获取有关配置 Prometheus 以抓取 Istio 部署的更多信息。

配置

在 Istio 服务网格中,每个组件都会公开一个发出指标的端点。Prometheus 通过抓取这些端点并收集结果来工作。这通过Prometheus 配置文件进行配置,该文件控制要查询哪些端点、查询的端口和路径、TLS 设置等。

要收集整个服务网格的指标,请配置 Prometheus 以抓取

  1. 控制平面 (istiod 部署)
  2. 入口和出口网关
  3. Envoy 侧车
  4. 用户应用程序(如果它们公开 Prometheus 指标)

为了简化指标配置,Istio 提供了两种操作模式。

选项 1:指标合并

为了简化配置,Istio 能够完全通过prometheus.io注释控制抓取。这允许 Istio 抓取与标准配置(例如 Helm stable/prometheus 图表提供的配置)开箱即用。

此选项默认启用,但可以通过在安装期间传递--set meshConfig.enablePrometheusMerge=false来禁用。启用后,将向所有数据平面 Pod 添加相应的prometheus.io注释以设置抓取。如果这些注释已存在,则将被覆盖。使用此选项,Envoy 侧车将把 Istio 的指标与应用程序指标合并。合并后的指标将从:15020/stats/prometheus抓取。

此选项以纯文本形式公开所有指标。

在以下情况下,此功能可能不适合您的需求

  • 您需要使用 TLS 抓取指标。
  • 您的应用程序公开了与 Istio 指标名称相同的指标。例如,您的应用程序指标公开了istio_requests_total指标。如果应用程序本身正在运行 Envoy,则可能会发生这种情况。
  • 您的 Prometheus 部署未配置为根据标准prometheus.io注释进行抓取。

如果需要,可以通过在 Pod 上添加prometheus.istio.io/merge-metrics: "false"注释来禁用每个工作负载的此功能。

选项 2:自定义抓取配置

要配置现有的 Prometheus 实例以抓取 Istio 生成的统计信息,需要添加多个作业。

  • 要抓取Istiod统计信息,可以将以下示例作业添加到其http-monitoring端口的抓取中
- job_name: 'istiod'
  kubernetes_sd_configs:
  - role: endpoints
    namespaces:
      names:
      - istio-system
  relabel_configs:
  - source_labels: [__meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
    action: keep
    regex: istiod;http-monitoring
  • 要抓取 Envoy 统计信息(包括侧车代理和网关代理),可以将以下作业添加到抓取以端口结尾为-envoy-prom的端口
- job_name: 'envoy-stats'
  metrics_path: /stats/prometheus
  kubernetes_sd_configs:
  - role: pod

  relabel_configs:
  - source_labels: [__meta_kubernetes_pod_container_port_name]
    action: keep
    regex: '.*-envoy-prom'

TLS 设置

控制平面、网关和 Envoy 侧车指标都将通过明文抓取。但是,应用程序指标将遵循为工作负载配置的任何Istio 身份验证策略

  • 如果您使用STRICT模式,则需要将 Prometheus 配置为使用 Istio 证书进行抓取,如下所述。
  • 如果您使用PERMISSIVE模式,则工作负载通常接受 TLS 和明文。但是,Prometheus 无法发送 Istio 需要的特殊 TLS 变体以用于PERMISSIVE模式。因此,您不能在 Prometheus 中配置 TLS。
  • 如果您使用DISABLE模式,则 Prometheus 不需要 TLS 配置。

为 Prometheus 配置 Istio 证书的一种方法是注入一个侧车,该侧车将轮换 SDS 证书并将它们输出到可以与 Prometheus 共享的卷中。但是,侧车不应拦截 Prometheus 的请求,因为 Prometheus 的直接端点访问模型与 Istio 的侧车代理模型不兼容。

为此,请在 Prometheus 服务器容器上配置证书卷挂载

containers:
  - name: prometheus-server
    ...
    volumeMounts:
      mountPath: /etc/prom-certs/
      name: istio-certs
volumes:
  - emptyDir:
      medium: Memory
    name: istio-certs

然后将以下注释添加到 Prometheus 部署 Pod 模板,并使用侧车注入进行部署。这将配置侧车将证书写入共享卷,但无需配置流量重定向

spec:
  template:
    metadata:
      annotations:
        traffic.sidecar.istio.io/includeInboundPorts: ""   # do not intercept any inbound ports
        traffic.sidecar.istio.io/includeOutboundIPRanges: ""  # do not intercept any outbound traffic
        proxy.istio.io/config: |  # configure an env variable `OUTPUT_CERTS` to write certificates to the given folder
          proxyMetadata:
            OUTPUT_CERTS: /etc/istio-output-certs
        sidecar.istio.io/userVolumeMount: '[{"name": "istio-certs", "mountPath": "/etc/istio-output-certs"}]' # mount the shared volume at sidecar proxy

最后,按如下方式设置抓取作业 TLS 上下文

scheme: https
tls_config:
  ca_file: /etc/prom-certs/root-cert.pem
  cert_file: /etc/prom-certs/cert-chain.pem
  key_file: /etc/prom-certs/key.pem
  insecure_skip_verify: true  # Prometheus does not support Istio security naming, thus skip verifying target pod certificate

最佳实践

对于较大的服务网格,高级配置可能有助于 Prometheus 扩展。请参阅使用 Prometheus 进行生产规模监控以获取更多信息。

这些信息有用吗?
您是否有任何改进建议?

感谢您的反馈!