从工作负载中收集指标

借助 Google Distributed Cloud (GDC) 网闸隔离配置,您可以持续抓取和收集工作负载中的指标,以监控应用性能并保持数据可观测性。如需启动指标收集,您需要将 MonitoringTarget 自定义资源部署到 Management API 服务器上的项目命名空间。

MonitoringTarget 自定义资源会指示监控流水线抓取以 Prometheus 公开格式(例如 OpenMetrics)公开 HTTP 端点的指定 Pod。您可以为目标 Pod 自定义爬取频率、端点设置、标签和注释。项目的 Grafana 实例会显示收集的指标,以便您分析应用的运行状态。

准备工作

如需管理 MonitoringRule 自定义资源,请向管理员申请必要的权限。

所需 IAM 角色

请与您的 IAM 项目管理员联系,申请获得以下角色:

  • MonitoringRule Creator (monitoringrule-creator):在项目命名空间中创建 MonitoringRule 自定义资源。
  • MonitoringRule Editor (monitoringrule-editor):修改项目命名空间中的 MonitoringRule 自定义资源。
  • MonitoringRule Viewer (monitoringrule-viewer):查看项目命名空间中的 MonitoringRule 自定义资源。

配置 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:Management API 服务器的 kubeconfig 文件的路径。
    • MONITORING_TARGET_NAME:MonitoringTarget 定义文件的名称。

监控平台开始收集指标。

注释

如果每个 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:Management API 服务器的 kubeconfig 文件的路径。
    • MONITORING_TARGET_NAME:MonitoringTarget 定义文件的名称。

监控平台开始收集指标。

如需了解其他字段和选项,请参阅完整的 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_NAME:MonitoringTarget 定义文件的名称。