从工作负载中收集指标

本页面简要介绍了从 Google Distributed Cloud (GDC) 网闸隔离环境中的工作负载抓取指标的过程,以方便进行监控和数据观测。

您可以抓取并收集组件随时间推移生成的指标。监控平台提供了一个自定义 API,用于从分布式云项目命名空间内运行的工作负载中抓取指标。如需抓取指标,您需要在管理 API 服务器中将 MonitoringTarget 自定义资源部署到项目命名空间。部署此资源后,监控平台会开始收集数据。

MonitoringTarget 自定义资源会指示监控流水线抓取项目中的指定 pod。这些 pod 必须公开一个 HTTP 端点,该端点以 Prometheus 指标公开格式(例如 OpenMetrics)传送指标。然后,抓取的指标会显示在项目的 Grafana 实例中,从而让您深入了解应用的运行状态。

如需配置 MonitoringTarget 自定义资源,您必须指定项目命名空间内的 pod 以进行指标收集。您可以自定义各种设置,包括抓取频率、Pod 的指标端点、标签和注释。

准备工作

如需获得管理MonitoringTarget自定义资源所需的权限,请让组织 IAM 管理员或项目 IAM 管理员向您授予关联的MonitoringTarget角色之一。

根据您需要的访问权限级别,您可以在组织或项目中获得此资源的创建者、编辑者或查看者角色。如需了解详情,请参阅准备 IAM 权限

配置 MonitoringTarget 自定义资源

MonitoringTarget 自定义资源用于告知监控平台从何处收集指标。您可以指定要收集指标的 pod、这些 pod 的指标端点、抓取频率以及任何其他设置。

此资源定义了以下配置:

  • 目标:项目中公开指标的 pod 及其端点。
  • 抓取间隔:您希望从所选端点拉取指标的频率。
  • 标签自定义:可选规则,包含对指标的任何标签修改。

选择以下方法之一,在 MonitoringTarget 自定义资源中指定指标端点:

  • 静态端点:您可以在 MonitoringTarget 配置中明确声明端点(端口、路径、方案)。
  • 注释:Pod 指标端点信息是从容器的 Deployment 文件中的注释检索的。如果每个 pod 都有不同的端点,此方法可提供更高的灵活性。

静态端点

请按照以下步骤操作,在静态定义的端点上公开所选 Pod 的指标:

  1. 确定要从中收集指标以进行监控的 Distributed Cloud 项目。

  2. 在 Pod 的规范中,在 containerPort 字段中声明提供指标的端口。以下示例展示了如何在 pod 的规范中声明端口 2112

    # ...
    spec:
      template:
        spec:
          containers:
          - name: your-container-name
            ports:
            - containerPort: 2112
    # ...
    
  3. MonitoringTarget 配置中,在 podMetricsEndpoints 部分中指定端点详细信息(端口、路径、方案),以匹配您在 pod 规范中公开的端口。

    以下 YAML 文件展示了一个 MonitoringTarget 配置示例,其中每个选定的 pod 都必须在同一端点 http://your-container-name:2112/metrics 上公开指标:

    apiVersion: monitoring.gdc.goog/v1
    kind: MonitoringTarget
    metadata:
      # Choose the same namespace as the workload pods.
      namespace: your-project-namespace
      name: your-container-name
    spec:
      selector:
          # Choose pod labels to consider for this job.
          # Optional: Map of key-value pairs.
          # Default: No filtering by label.
          # To consider every pod in the project namespace, remove selector fields.
        matchLabels:
          app: your-app-label
      podMetricsEndpoints:
        port:
          value: 2112
        path:
          # Choose any value for your endpoint.
          # The /metrics value is an example.
          value: /metrics
        scheme:
          value: http
    
  4. MonitoringTarget 配置应用于与目标 pod 位于同一命名空间内的 Management API 服务器:

    kubectl --kubeconfig KUBECONFIG_PATH apply -f MONITORING_TARGET_NAME.yaml
    

    替换以下内容:

    • KUBECONFIG_PATH:管理 API 服务器的 kubeconfig 文件的路径。
    • MONITORING_TARGET_NAMEMonitoringTarget 定义文件的名称。

监控平台开始收集指标。

注释

如果每个 pod 都有不同的端点,请按以下步骤操作,使用注释公开指标:

  1. 确定要从中收集指标以进行监控的 Distributed Cloud 项目。

  2. 将以下注释添加到容器的 Deployment 文件的 annotations 部分:

    • prometheus.io/path
    • prometheus.io/port
    • prometheus.io/scheme

    以下示例展示了端口 2112 上指标的注释:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: your-container-name
      namespace: your-project-namespace
      labels:
        app: your-app-label
      annotations:
        # These annotations are not required. They demonstrate selecting
        # pod metric endpoints through annotations.
        prometheus.io/path: /metrics
        prometheus.io/port: \"2112\"
        prometheus.io/scheme: http
    
  3. MonitoringTarget 配置中,于 podMetricsEndpoints 部分指定您添加到容器 Deployment 文件中的注释。此规范会指示自定义资源从所选 Pod 的注释中收集指标端点信息。

    以下 YAML 文件展示了使用注释的 MonitoringTarget 配置示例:

    apiVersion: monitoring.gdc.goog/v1
    kind: MonitoringTarget
    metadata:
    metadata:
      # Choose the same namespace as the workload pods.
      namespace: your-project-namespace
      name: your-container-name
    spec:
      selector:
        matchLabels:
          app: your-app-label
      podMetricsEndpoints:
        port:
          annotation: prometheus.io/port
        path:
          annotation: prometheus.io/path
        scheme:
          annotation: prometheus.io/scheme
    
  4. MonitoringTarget 配置应用于与目标 pod 位于同一命名空间内的 Management API 服务器:

    kubectl --kubeconfig KUBECONFIG_PATH apply -f MONITORING_TARGET_NAME.yaml
    

    替换以下内容:

    • KUBECONFIG_PATH:管理 API 服务器的 kubeconfig 文件的路径。
    • MONITORING_TARGET_NAMEMonitoringTarget 定义文件的名称。

监控平台开始收集指标。

如需了解其他字段和选项,请参阅完整的 MonitoringTarget 规范API 参考文档

完整 MonitoringTarget 规范

以下 YAML 文件展示了如何完整指定 MonitoringTarget 自定义资源。如需了解详情和字段的完整说明,请参阅 API 参考文档

apiVersion: monitoring.gdc.goog/v1
kind: MonitoringTarget
metadata:
  # Choose the same namespace as the workload pods.
  namespace: PROJECT_NAMESPACE
  name: MONITORING_TARGET_NAME
spec:
  # Choose matching pattern that identifies pods for this job.
  # Optional
  # Relationship between different selectors: AND
  selector:
    # Choose clusters to consider for this job.
    # Optional: List
    # Default: All clusters applicable to this project.
    # Relationship between different list elements: OR
    matchClusters:
    - string

    # Choose pod labels to consider for this job.
    # Optional: Map of key-value pairs.
    # Default: No filtering by label.
    # Relationship between different pairs: AND
    matchLabels:
      key1: value1

    # Choose annotations to consider for this job.
    # Optional: Map of key-value pairs
    # Default: No filtering by annotation
    # Relationship between different pairs: AND
    matchAnnotations:
      key1: value1

  # Configure the endpoint exposed for this job.
  podMetricsEndpoints:
    # Choose a port either through static value or annotation.
    # Optional
    # Annotation takes priority.
    # Default: static port 80
    port:
      value: integer
      annotation: string

    # Choose a path either through static value or annotation.
    # Optional
    # Annotation takes priority
    # Default: static path /metrics
    path:
      value: string
      annotation: string

    # Choose a scheme either through a static value (http or https) or annotation.
    # Optional
    # Annotation takes priority
    # Default: static scheme http
    scheme:
      value: string
      annotation: string

    # Choose the frequency to scrape the metrics endpoint defined in podMetricsEndpoints
    # Optional
    # Default: 60s
    scrapeInterval: string

    # Dynamically rewrite the label set of a target before it gets scraped.
    # https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config
    # Optional
    # Default: No filtering by label
    metricsRelabelings:
    - sourceLabels:
      - string
      separator: string
      regex: string
      action: string
      targetLabel: string
      replacement: string

替换以下内容:

  • PROJECT_NAMESPACE:您的项目命名空间。
  • MONITORING_TARGET_NAMEMonitoringTarget 定义文件的名称。