跳至內容

在 Knative 中收集指標

Knative 支援不同的熱門工具來收集指標

Grafana 儀表板可用於直接使用 Prometheus 收集的指標。

您也可以設定 OpenTelemetry Collector 從 Knative 元件接收指標,並將它們分發給其他支援 OpenTelemetry 的指標提供者。

警告

您不能同時使用 OpenTelemetry Collector 和 Prometheus。預設指標後端是 Prometheus。您需要從 config-observability Configmap 中移除 metrics.backend-destinationmetrics.request-metrics-backend-destination 金鑰才能啟用 Prometheus 指標。

關於 Prometheus Stack

Prometheus 是一個用於收集、匯總時間序列指標和警報的開源工具。當使用 Prometheus 時,它也可以用於抓取下面展示的 OpenTelemetry Collector。

Grafana 是一個用於資料分析和視覺化的開源平台,讓使用者可以建立可自訂的儀表板,以監控和分析來自各種資料來源的指標。

Prometheus Stack 是一個預先設定的 Kubernetes 清單、Grafana 儀表板和 Prometheus 規則的集合,結合使用 Prometheus Operator 提供端對端的 Kubernetes 叢集監控。該堆疊預設包含一些 Prometheus 套件和 Grafana。

設定 Prometheus Stack

  1. 使用 Helm 安裝 Prometheus Stack

    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 圖表已設定以下值,否則 ServiceMonitors/Podmonitors 將無法運作。

    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. 應用 ServiceMonitors/PodMonitors 從 Knative 收集指標。

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

在本地存取 Prometheus 執行個體

預設情況下,Prometheus 執行個體僅在名為 prometheus-kube-prometheus-prometheus 的私有服務上公開。

在您的網頁瀏覽器中存取主控台

  1. 輸入命令

    kubectl port-forward -n default svc/prometheus-kube-prometheus-prometheus 9090:9090
    
  2. 透過 https://#:9090 在您的瀏覽器中存取主控台。

在本地存取 Grafana 執行個體

預設情況下,Grafana 執行個體僅在名為 prometheus-grafana 的私有服務上公開。

在您的網頁瀏覽器中存取儀表板

  1. 輸入命令

    kubectl port-forward -n default svc/prometheus-grafana 3000:80
    
  2. 透過 https://#:3000 在您的瀏覽器中存取儀表板。

  3. 使用預設認證登入

    username: admin
    password: prom-operator
    

匯入 Grafana 儀表板

  1. Grafana 儀表板可以從 monitoring 儲存庫匯入。

  2. 如果您正在使用已啟用儀表板 Sidecar 的 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 宣告之後將 labelValue: true 屬性新增至 helm 圖表。

關於 OpenTelemetry

OpenTelemetry 是一個用於雲端原生軟體的 CNCF 可觀察性架構,它提供了一系列工具、API 和 SDK。

您可以使用 OpenTelemetry 來檢測、產生、收集和匯出遙測資料。此資料包括指標、記錄和追蹤,您可以分析這些資料來了解 Knative 元件的效能和行為。

OpenTelemetry 允許您輕鬆地將指標匯出到多個監控服務,而無需重新建置或重新設定 Knative 二進位檔案。

了解收集器

收集器提供了一個位置,各種 Knative 元件可以將指標推送至該位置,以便由監控服務保留和收集。

在以下範例中,您可以使用 ConfigMap 和 Deployment 來設定單個收集器執行個體。

提示

對於更複雜的部署,您可以使用 OpenTelemetry Operator 來自動執行其中一些步驟。

注意

位於 https://github.com/knative-extensions/monitoring/tree/main/grafana 的 Grafana 儀表板不適用於從 OpenTelemetry Collector 抓取的指標。

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://#:8889/metrics 以查看匯出的指標。

我們使用分析和 Cookie 來了解網站流量。您使用我們網站的相關資訊會與 Google 分享,用於該目的。了解更多。