从 Kubernetes 收集特定的 Prometheus 指标

本文档向运维人员介绍了如何配置 Google Cloud Managed Service for Prometheus ,以过滤和收集 Google Kubernetes Engine (GKE) 集群和工作负载中的特定 Kubernetes 指标。

您应该已经熟悉以下内容:

GKE 中的指标收集

GKE 集群中的系统组件会发出并公开 Prometheus 指标。默认情况下,所有集群都会注入特定于 GKE 的系统指标。此外,还有内置指标 软件包可用于 注入各种组件的精选指标集合。

除了内置的 GKE 指标软件包之外,您还可以手动配置 Google Cloud Managed Service for Prometheus 以注入特定的 Prometheus 指标。这种自定义配置非常适合以下用例:

  • 监控集群和工作负载中的特定信号。
  • 停用不需要的指标软件包并仅提取部分指标,从而优化费用。
  • 收集组件发出的但未包含在指标软件包中的指标。

对于运维人员来说,节点 指标(每个节点上的 kubelet 进程发出)可能有助于监控工作负载和节点中的特定信号。

指标收集自定义资源

如需过滤和注入特定指标,您需要配置 Prometheus 重新标记规则 在以下某个自定义资源中:

指标重复

如果您过滤的指标也包含在集群的活跃指标软件包中,则该指标会在 Google Cloud Managed Service for Prometheus 中多次显示。例如,如果您的集群启用了 KUBELET指标软件包,并且您配置了 kubelet_running_containers指标的收集,则会看到重复的值。

为避免重复,请验证您收集的指标是否包含在活跃指标软件包中。如需详细了解包含的指标,请参阅以下指标软件包:

准备工作

在开始之前,请确保您已完成以下任务:

  • 启用 Google Kubernetes Engine API。
  • 启用 Google Kubernetes Engine API
  • 如需使用 Google Cloud CLI 执行此任务, 请安装初始化 gcloud CLI。如果您之前安装了 gcloud CLI,请通过运行 gcloud components update 命令来获取最新 版本。较早版本的 gcloud CLI 可能不支持运行本文档中的命令。

所需的角色

如需获得收集和查看指标所需的权限,请让您的管理员向您授予项目的以下 IAM 角色:

如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

您也可以通过自定义 角色或其他预定义 角色来获取所需的权限。

启用 Google Cloud Managed Service for Prometheus

默认情况下,所有 GKE 集群都会启用 Google Cloud Managed Service for Prometheus,并且无法在 Autopilot 集群中停用。如果您使用的是 Autopilot 集群,请跳至 配置指标收集部分。

如需验证您的 Standard 集群中是否启用了 Google Cloud Managed Service for Prometheus,请选择以下选项之一:

控制台

  1. 在 Google Cloud 控制台中,前往 Kubernetes 集群 页面。

    转到 Kubernetes 集群

  2. 在要检查的集群对应的行中,依次点击 操作 >  修改。随即会打开集群详情 页面。

  3. 功能 部分中,检查Managed Service for Prometheus 字段中的值。如果值为已启用, 请跳至配置指标收集部分。如果值为 已停用,请启用 Google Cloud Managed Service for Prometheus:

    1. 点击 修改 Managed Service for Prometheus。随即会打开修改 Managed Service for Prometheus 对话框。
    2. 选中启用 Managed Service for Prometheus 复选框。
    3. 点击保存更改

gcloud

检查集群中是否启用了 Google Cloud Managed Service for Prometheus:

gcloud container clusters describe CLUSTER_NAME \
    --location=CONTROL_PLANE_LOCATION \
    --format='value(monitoringConfig.managedPrometheusConfig.enabled)'

替换以下内容:

  • CLUSTER_NAME:Standard 集群的名称。
  • CONTROL_PLANE_LOCATION:集群控制平面的区域或可用区 ,例如 us-central1us-central1-a

如果输出为 True,则表示已启用 Google Cloud Managed Service for Prometheus。 如果输出为 False,请启用 Google Cloud Managed Service for Prometheus:

gcloud container clusters update CLUSTER_NAME \
    --location=CONTROL_PLANE_LOCATION \
    --enable-managed-prometheus

配置指标收集

如需配置 Google Cloud Managed Service for Prometheus 以注入特定发出的指标,您需要了解该指标的以下信息:

  • 指标名称,例如 kubelet_working_pods
  • 公开指标的端点,例如 /metrics/cadvisor/metrics

如需查找指标并配置收集,请按以下步骤操作:

  1. 在特定组件的参考文档中查找指标。例如,您可以使用以下资源查找节点指标:

  2. 检查集群的活跃指标软件包:

    gcloud container clusters describe CLUSTER_NAME \
        --location=CONTROL_PLANE_LOCATION \
        --flatten=monitoringConfig.componentConfig \
        --format='value(enableComponents)'
    

    输出类似于以下内容:

    SYSTEM_COMPONENTS;STORAGE;POD;DEPLOYMENT;STATEFULSET;DAEMONSET;HPA;JOBSET;CADVISOR;KUBELET;DCGM
    
  3. 使用可用指标中的信息,检查您要收集的指标是否位于这些活跃 软件包中。如果您的指标包含在活跃指标软件包中,则 Monitoring 中可能会显示重复的指标。如果您的指标不在活跃指标软件包中,请继续执行下一步。

  4. 可选:如需验证指标是否在指标端点可用, 请使用 kubectl get --raw 命令查询该端点。例如,以下命令查询 /metrics 端点的 kubelet_working_pods 指标:

    kubectl get --raw "/api/v1/nodes/NODE_NAME/proxy/metrics" | grep "kubelet_working_pods"
    

    NODE_NAME 替换为要查询的节点的名称。

    输出类似于以下内容,表示该指标可用:

    # HELP kubelet_working_pods [ALPHA] Number of pods the kubelet is actually running, broken down by lifecycle phase, whether the pod is desired, orphaned, or runtime only (also orphaned), and whether the pod is static. An orphaned pod has been removed from local configuration or force deleted in the API and consumes resources that are not otherwise visible.
    # TYPE kubelet_working_pods gauge
    kubelet_working_pods{config="desired",lifecycle="sync",static=""} 8
    kubelet_working_pods{config="desired",lifecycle="sync",static="true"} 1
    kubelet_working_pods{config="desired",lifecycle="terminated",static=""} 0
    kubelet_working_pods{config="desired",lifecycle="terminated",static="true"} 0
    kubelet_working_pods{config="desired",lifecycle="terminating",static=""} 0
    kubelet_working_pods{config="desired",lifecycle="terminating",static="true"} 0
    
  5. 确定要收集的指标后,请在 PodMonitoring、ClusterPodMonitoring 或 ClusterNodeMonitoring 自定义资源中使用 Prometheus 重新标记规则来过滤该指标。您使用的自定义 资源取决于指标的属性,如 在指标收集自定义资源 部分中所述。

    例如,请查看以下 ClusterNodeMonitoring 清单:

    apiVersion: monitoring.googleapis.com/v1
    kind: ClusterNodeMonitoring
    metadata:
      name: kubelet-pod-counts
    spec:
      selector:
        matchLabels: {}
      endpoints:
      - path: "/metrics"
        scheme: "https"
        interval: "30s"
        tls:
          insecureSkipVerify: true
        # metricRelabeling specifies the metrics to ingest. The metrics must be
        # available at the specified endpoint.
        metricRelabeling:
        - action: keep
          sourceLabels: [__name__]
          regex: kubelet_(active|working)_pods
    

    此清单具有以下属性:

    • selector.matchLabels 字段是一个空集 ({}),与所有节点匹配。
    • endpoints.path 字段标识要抓取的指标端点。
    • metricRelabeling 字段标识要收集的特定指标。在此示例中,Google Cloud Managed Service for Prometheus 从端点收集名称为 kubelet_active_podskubelet_working_pods 的指标。

部署自定义资源后,Google Cloud Managed Service for Prometheus 会提取与您指定的配置匹配的指标。您可以使用与其他指标相同的方法(例如 Cloud Monitoring)查看和查询这些指标。

验证示例工作负载的指标收集

以下步骤向您展示了如何配置 Google Cloud Managed Service for Prometheus 以从 cAdvisor 收集 压力停滞信息 (PSI) 指标。在这些示例步骤中,您将创建一个 ClusterNodeMonitoring(因为 cAdvisor 是一个节点组件),创建一个触发 PSI 指标的测试 Pod,然后验证这些指标是否显示在 Monitoring 中。如需收集此示例中的 PSI 指标,您的集群必须运行 GKE 1.35 版或更高版本。

  1. 创建一个触发 cAdvisor 中的 PSI 指标的测试 Pod:

    1. 将以下 Pod 保存为 psi-pod.yaml

      apiVersion: v1
      kind: Pod
      metadata:
        name: cpu-pressure-pod
      spec:
        restartPolicy: Never
        containers:
        - name: cpu-stress
          image: registry.k8s.io/e2e-test-images/agnhost:2.47
          args:
          - "stress"
          - "--cpus"
          - "1"
          resources:
            limits:
              cpu: "500m"
            requests:
              cpu: "500m"
      
    2. 创建 Pod:

      kubectl apply -f psi-pod.yaml
      
  2. 验证 Monitoring 中是否已存在 PSI 指标:

    1. 在 Google Cloud 控制台中,前往 Metrics Explorer 页面。

      转到 Metrics Explorer

    2. 查询 部分的指标 字段中,尝试选择 prometheus/container_pressure_cpu_stalled_seconds_total/counter 指标。如果您没有看到搜索结果,则表示未收集 PSI 指标。

  3. 创建一个 ClusterNodeMonitoring 以过滤 PSI 指标:

    1. 将以下 ClusterNodeMonitoring 清单保存为 psi-prometheus-collector.yaml

      apiVersion: monitoring.googleapis.com/v1
      kind: ClusterNodeMonitoring
      metadata:
        name: psi-collector
      spec:
        selector:
          matchLabels: {}
        endpoints:
        - path: /metrics/cadvisor
          scheme: https
          interval: 30s
          tls:
            insecureSkipVerify: true
          metricRelabeling:
          - action: keep
            sourceLabels: [__name__]
            regex: container_pressure_(cpu|memory|io)_(waiting|stalled)_seconds_total
      
    2. 创建 ClusterNodeMonitoring:

      kubectl apply -f psi-prometheus-collector.yaml
      

    该指标可能需要最多 10 分钟才会显示在 Monitoring 中。

  4. 在 Google Cloud 控制台中,验证 PSI 指标是否显示在 Monitoring 中:

    1. 前往 Metrics Explorer 页面。

      转到 Metrics Explorer

    2. 查询 部分的指标 字段中,选择 prometheus/container_pressure_cpu_stalled_seconds_total/counter 指标。

    3. 可选:在过滤条件 下拉菜单中,选择您的集群。

    结果 部分会显示一个图表,其中包含收集的 PSI 指标。

后续步骤