Istio

本文档介绍如何配置 Google Kubernetes Engine 部署,以便使用 Google Cloud Managed Service for Prometheus 从 Istio 收集指标。本页面介绍如何完成以下任务:

  • 设置 Istio 以报告指标。
  • 为 Managed Service for Prometheus 配置 PodMonitoring 资源以收集导出的指标。
  • 在 Cloud Monitoring 中访问信息中心以查看指标。
  • 配置提醒规则以监控指标。

以下说明仅在您将代管式收集功能与 Managed Service for Prometheus 搭配使用时适用。 如果您使用的是自行部署的收集功能,请参阅 Istio 的源代码库以了解安装信息。

这些说明仅作为示例提供,应该适用于大多数 Kubernetes 环境。如果由于安全或组织政策的限制,您在安装应用或导出器时遇到问题,我们建议您查看开源文档以获取支持。

如需了解 Istio,请参阅 Istio

前提条件

要使用 Managed Service for Prometheus 和代管式收集功能从 Istio 收集指标,您的部署必须满足以下要求:

  • 您的集群必须运行 Google Kubernetes Engine 1.21.4-gke.300 或更高版本。
  • 您必须运行 Managed Service for Prometheus,并启用代管式收集功能。如需了解详情,请参阅代管式收集功能使用入门

Istio 会自动公开 Prometheus 格式的指标;无需单独安装。您可以运行以下检查,以验证 Istio 代理是否已作为 Sidecar 注入,以及 Istiod、Istio 的控制平面和 Istio 代理是否正在预期端点上发出指标。

  • 如需确定 Istio 代理是否已作为 Sidecar 注入,请运行以下命令,该命令枚举应用的 pod 中运行的容器:

    kubectl get pod -l app=APPLICATION_NAME -n NAMESPACE_NAME -o jsonpath='{.items[0].spec.containers[*].name}'
    

    如果您看到 pod 包含 istio Sidecar 容器,则说明导出器已注入。如果未注入 Sidecar,请按照“Istio:安装 Sidecar”中的说明操作。

  • 如需验证 Istio 代理是否正在发出指标,请运行以下命令,该命令检查指定 pod 上 istio/stats/prometheus 端点:

    kubectl exec POD_NAME -n NAMESPACE_NAME -c istio-proxy -- curl -sS 'localhost:15090/stats/prometheus'
    

    如果您看到原始 istio_*envoy_* Prometheus 指标,则说明系统正在正确发出指标。

  • 如需验证 Istiod 上系统是否正在以类似方式发出指标,请运行以下命令,该命令检查 istiod 部署中某个 pod 上的 Istiod 的 /metrics 端点:

    kubectl exec -n istio-system deployment/istiod -- curl -sS 'localhost:15014/metrics'
    

定义 PodMonitoring 资源

对于目标发现,Managed Service for Prometheus Operator 需要与同一命名空间中的 Istio 导出器对应的 PodMonitoring 资源。

您可以使用以下 PodMonitoring 配置:

# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: monitoring.googleapis.com/v1
kind: PodMonitoring
metadata:
  name: istiod
  namespace: istio-system
  labels:
    app.kubernetes.io/name: istiod
    app.kubernetes.io/part-of: google-cloud-managed-prometheus
spec:
  selector:
    matchLabels:
      app: istiod
  endpoints:
  - port: 15014
    interval: 30s
    path: /metrics
  targetLabels:
    fromPod:
    - from: app
      to: app
---
apiVersion: monitoring.googleapis.com/v1
kind: PodMonitoring
metadata:
  name: istio-proxy
  labels:
    app.kubernetes.io/name: istio-proxy
    app.kubernetes.io/part-of: google-cloud-managed-prometheus
spec:
  selector:
    matchLabels:
  endpoints:
  - port: http-envoy-prom
    scheme: http
    interval: 30s
    path: /stats/prometheus
Istio 需要两个单独的 PodMonitoring 资源:一个用于监控 Istiod,另一个用于监控 Istio 代理 Sidecar 以及入站流量和出站流量网关。如需同时监控集群中所有命名空间的 Istio 代理指标,请将 istio-proxy PodMonitoring 应用于每个命名空间,或者按命名空间设置 ClusterPodMonitoring 资源(而不是 PodMonitoring 资源)。

如果您计划使用 Istio 提供的 Grafana 信息中心,则除了本文档中所述的 PodMonitoring 资源之外,请确保您还配置了 cAdvisor 和 Kubelet 爬取

如需从本地文件应用配置更改,请运行以下命令:

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

您还可以使用 Terraform 管理您的配置。

定义规则和提醒

您可以使用以下 Rules 配置来定义 Istio 指标提醒:

# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: monitoring.googleapis.com/v1
kind: Rules
metadata:
  name: istio-rules
  labels:
    app.kubernetes.io/component: rules
    app.kubernetes.io/name: istio-rules
    app.kubernetes.io/part-of: google-cloud-managed-prometheus
spec:
  groups:
  - name: istio
    interval: 30s
    rules:
    - alert: IstioHighTotalRequestRate
      expr: sum(rate(istio_requests_total{reporter="destination"}[5m])) > 1000
      for: 2m
      labels:
        severity: warning
      annotations:
        summary: Istio high total request rate (instance {{ $labels.instance }})
        description: |-
          Global request rate in the service mesh is unusually high.
            VALUE = {{ $value }}
            LABELS = {{ $labels }}
    - alert: IstioLowTotalRequestRate
      expr: sum(rate(istio_requests_total{reporter="destination"}[5m])) < 100
      for: 2m
      labels:
        severity: warning
      annotations:
        summary: Istio low total request rate (instance {{ $labels.instance }})
        description: |-
          Global request rate in the service mesh is unusually low.
            VALUE = {{ $value }}
            LABELS = {{ $labels }}
    - alert: IstioHigh4xxErrorRate
      expr: sum(rate(istio_requests_total{reporter="destination", response_code=~"4.*"}[5m])) / sum(rate(istio_requests_total{reporter="destination"}[5m])) * 100 > 5
      for: 1m
      labels:
        severity: warning
      annotations:
        summary: Istio high 4xx error rate (instance {{ $labels.instance }})
        description: |-
          High percentage of HTTP 5xx responses in Istio (> 5%).
            VALUE = {{ $value }}
            LABELS = {{ $labels }}
    - alert: IstioHigh5xxErrorRate
      expr: sum(rate(istio_requests_total{reporter="destination", response_code=~"5.*"}[5m])) / sum(rate(istio_requests_total{reporter="destination"}[5m])) * 100 > 5
      for: 1m
      labels:
        severity: warning
      annotations:
        summary: Istio high 5xx error rate (instance {{ $labels.instance }})
        description: |-
          High percentage of HTTP 5xx responses in Istio (> 5%).
            VALUE = {{ $value }}
            LABELS = {{ $labels }}
    - alert: IstioHighRequestLatency
      expr: rate(istio_request_duration_milliseconds_sum{reporter="destination"}[1m]) / rate(istio_request_duration_milliseconds_count{reporter="destination"}[1m]) > 100
      for: 1m
      labels:
        severity: warning
      annotations:
        summary: Istio high request latency (instance {{ $labels.instance }})
        description: |-
          Istio average requests execution is longer than 100ms.
            VALUE = {{ $value }}
            LABELS = {{ $labels }}
    - alert: IstioLatency99Percentile
      expr: histogram_quantile(0.99, sum(rate(istio_request_duration_milliseconds_bucket[1m])) by (destination_canonical_service, destination_workload_namespace, source_canonical_service, source_workload_namespace, le)) > 1
      for: 1m
      labels:
        severity: warning
      annotations:
        summary: Istio latency 99 percentile (instance {{ $labels.instance }})
        description: |-
          Istio 1% slowest requests are longer than 1s.
            VALUE = {{ $value }}
            LABELS = {{ $labels }}

如需从本地文件应用配置更改,请运行以下命令:

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

您还可以使用 Terraform 管理您的配置。

如需详细了解如何将规则应用于您的集群,请参阅代管式规则评估和提醒

Rules 配置改编自 Awesome Prometheus 提醒提供的 Istio 规则。您可以根据您的应用调整提醒阈值。

验证配置

您可以使用 Metrics Explorer 验证是否正确配置了 Istio 导出器。Cloud Monitoring 可能需要一两分钟时间来注入您的指标。

要验证指标是否已注入,请执行以下操作:

  1. 在 Google Cloud 控制台中,转到 Metrics Explorer 页面:

    进入 Metrics Explorer

    如果您使用搜索栏查找此页面,请选择子标题为监控的结果。

  2. 在查询构建器窗格的工具栏中,选择名为  MQL PromQL 的按钮。
  3. 验证已在语言切换开关中选择 PromQL。语言切换开关位于同一工具栏中,用于设置查询的格式。
  4. 输入并运行以下查询:
    sum(istio_build{cluster="CLUSTER_NAME"}) by (component)
    

查看信息中心

Cloud Monitoring 集成包括 Istio Envoy Prometheus 概览信息中心。当您配置集成时,系统会自动安装信息中心。您还可以在不安装集成的情况下查看信息中心的静态预览。

如需查看已安装的信息中心,请执行以下操作:

  1. 在 Google Cloud 控制台中,转到 信息中心页面:

    前往信息中心

    如果您使用搜索栏查找此页面,请选择子标题为监控的结果。

  2. 选择信息中心列表标签页。
  3. 选择集成类别。
  4. 点击信息中心的名称,例如 Istio Envoy Prometheus 概览

如需查看信息中心的静态预览,请执行以下操作:

  1. 在 Google Cloud 控制台中,转到 集成页面:

    前往集成

    如果您使用搜索栏查找此页面,请选择子标题为监控的结果。

  2. 点击 Kubernetes Engine 部署平台过滤条件。
  3. 找到 Istio 集成,然后点击查看详情
  4. 选择信息中心标签页。

问题排查

如需了解如何排查指标注入问题,请参阅排查注入端问题中的从导出器收集的问题