跳至内容

在 Knative 中收集指标

Knative 支持不同的流行工具来收集指标

Grafana 仪表盘可用于直接使用 Prometheus 收集的指标。

您还可以设置 OpenTelemetry 收集器以接收来自 Knative 组件的指标并将它们分发到支持 OpenTelemetry 的其他指标提供商。

警告

您不能同时使用 OpenTelemetry 收集器和 Prometheus。默认指标后端是 Prometheus。您需要从 config-observability ConfigMap 中删除 metrics.backend-destinationmetrics.request-metrics-backend-destination 键以启用 Prometheus 指标。

关于 Prometheus 堆栈

Prometheus 是一款开源工具,用于收集、聚合时间序列指标并发出警报。它还可以用于抓取下面演示的 OpenTelemetry 收集器,当使用 Prometheus 时。

Grafana 是一款开源数据分析和可视化平台,使用户能够创建可自定义的仪表盘,用于监控和分析来自各种数据源的指标。

Prometheus 堆栈 是一组预先配置的 Kubernetes 清单、Grafana 仪表盘和 Prometheus 规则,结合起来使用 Prometheus 操作符提供使用 Prometheus 对 Kubernetes 集群进行端到端监控。该堆栈默认包含一些 Prometheus 包和 Grafana。

设置 Prometheus 堆栈

  1. 使用 Helm 安装 Prometheus 堆栈

    helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
    helm repo update
    helm install prometheus prometheus-community/kube-prometheus-stack -n default -f values.yaml
    # values.yaml contains at minimum the configuration below
    

    注意

    您需要确保 helm 图表已配置以下值,否则 ServiceMonitor/PodMonitor 将无法工作。

    kube-state-metrics:
      metricLabelsAllowlist:
        - pods=[*]
        - deployments=[app.kubernetes.io/name,app.kubernetes.io/component,app.kubernetes.io/instance]
    prometheus:
      prometheusSpec:
        serviceMonitorSelectorNilUsesHelmValues: false
        podMonitorSelectorNilUsesHelmValues: false
    

  2. 将 ServiceMonitor/PodMonitor 应用于收集来自 Knative 的指标。

    kubectl apply -f https://raw.githubusercontent.com/knative-extensions/monitoring/main/servicemonitor.yaml
    

在本地访问 Prometheus 实例

默认情况下,Prometheus 实例仅在名为 prometheus-kube-prometheus-prometheus 的私有服务上公开。

要在 Web 浏览器中访问控制台

  1. 输入命令

    kubectl port-forward -n default svc/prometheus-kube-prometheus-prometheus 9090:9090
    
  2. 通过 https://127.0.0.1:9090 在浏览器中访问控制台。

在本地访问 Grafana 实例

默认情况下,Grafana 实例仅在名为 prometheus-grafana 的私有服务上公开。

要在 Web 浏览器中访问仪表盘

  1. 输入命令

    kubectl port-forward -n default svc/prometheus-grafana 3000:80
    
  2. 通过 https://127.0.0.1:3000 在浏览器中访问仪表盘。

  3. 使用默认凭据登录

    username: admin
    password: prom-operator
    

导入 Grafana 仪表盘

  1. Grafana 仪表盘可以从 monitoring 仓库 导入。

  2. 如果您使用的是启用了仪表盘侧车的 Grafana Helm 图表,则可以通过应用以下 ConfigMap 来加载仪表盘。

    kubectl apply -f https://raw.githubusercontent.com/knative-extensions/monitoring/main/grafana/dashboards.yaml
    

    注意

    您需要确保 helm 图表已配置以下值,否则仪表盘加载将无法工作。

    grafana:
      sidecar:
        dashboards:
          enabled: true
          searchNamespace: ALL
    
    如果您有现有的 ConfigMap 并且仪表盘加载不起作用,请在 searchNamespace: ALL 声明之后向 helm 图表添加 labelValue: true 属性。

关于 OpenTelemetry

OpenTelemetry 是一个 CNCF 可观测性框架,用于云原生软件,它提供了一组工具、API 和 SDK。

您可以使用 OpenTelemetry 对 Knative 组件进行检测、生成、收集和导出遥测数据。此数据包括指标、日志和跟踪,您可以分析这些数据以了解 Knative 组件的性能和行为。

OpenTelemetry 允许您轻松地将指标导出到多个监控服务,而无需重新构建或重新配置 Knative 二进制文件。

了解收集器

收集器提供了一个位置,各种 Knative 组件可以将指标推送到该位置,以便监控服务保留和收集这些指标。

在以下示例中,您可以使用 ConfigMap 和 Deployment 配置单个收集器实例。

提示

对于更复杂的部署,您可以使用 OpenTelemetry 操作符 自动执行其中一些步骤。

注意

https://github.com/knative-extensions/monitoring/tree/main/grafana 中的 Grafana 仪表盘不适用于从 OpenTelemetry 收集器抓取的指标。

Diagram of components reporting to collector, which is scraped by Prometheus

设置收集器

  1. 通过输入以下命令为收集器创建一个运行的命名空间

    kubectl create namespace metrics
    
    下一步使用 metrics 命名空间来创建收集器。

  2. 通过输入以下命令为收集器创建 Deployment、Service 和 ConfigMap

    kubectl apply -f https://raw.githubusercontent.com/knative/docs/main/docs/serving/observability/metrics/collector.yaml
    
  3. 更新 Knative Serving 和 Eventing 命名空间中的 config-observability ConfigMap,通过输入以下命令

    kubectl patch --namespace knative-serving configmap/config-observability \
      --type merge \
      --patch '{"data":{"metrics.backend-destination":"opencensus","metrics.request-metrics-backend-destination":"opencensus","metrics.opencensus-address":"otel-collector.metrics:55678"}}'
    kubectl patch --namespace knative-eventing configmap/config-observability \
      --type merge \
      --patch '{"data":{"metrics.backend-destination":"opencensus","metrics.opencensus-address":"otel-collector.metrics:55678"}}'
    

验证收集器设置

  1. 您可以通过加载收集器上的 Prometheus 导出端口来检查指标是否正在转发,通过输入以下命令

    kubectl port-forward --namespace metrics deployment/otel-collector 8889
    
  2. 获取 https://127.0.0.1:8889/metrics 以查看导出的指标。

我们使用分析和 Cookie 来了解网站流量。有关您使用我们网站的信息将与 Google 共享,用于该目的。了解更多。