本页面概述了从 Google Distributed Cloud (GDC) 网闸隔离配置环境中的工作负载中抓取指标的过程,以便于监控和数据可观测性。
您可以抓取并收集组件随时间推移生成的指标。监控平台提供了一个自定义 API,用于从 Distributed Cloud 云项目命名空间内运行的工作负载中抓取指标。如需爬取指标,您需要将 MonitoringTarget 自定义资源部署到 Management API 服务器中的项目命名空间。部署此资源后,监控平台会启动数据收集。
MonitoringTarget 自定义资源会指示监控流水线抓取项目中的指定 Pod。这些 Pod 必须公开一个 HTTP 端点,该端点以 Prometheus 公开格式(例如 OpenMetrics)提供指标。然后,爬取的指标会显示在项目的 Grafana 实例中,让您能够深入了解应用的运行状态。
如需配置 MonitoringTarget 自定义资源,您必须指定项目命名空间中用于指标收集的 Pod。您可以自定义各种设置,包括抓取频率、Pod 的指标端点、标签和注解。
准备工作
如需管理 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 中的指标:
确定要从中收集指标以进行监控的 Distributed Cloud 云项目。
在 Pod 的规范中,在
containerPort字段中声明提供指标的端口。以下示例展示了如何在 Pod 的规范中声明端口2112:# ... spec: template: spec: containers: - name: your-container-name ports: - containerPort: 2112 # ...在
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将
MonitoringTarget配置应用于与目标 Pod 位于同一命名空间内的 Management API 服务器:kubectl --kubeconfig KUBECONFIG_PATH apply -f MONITORING_TARGET_NAME.yaml替换以下内容:
KUBECONFIG_PATH:Management API 服务器的 kubeconfig 文件的路径。MONITORING_TARGET_NAME:MonitoringTarget定义文件的名称。
监控平台开始收集指标。
注释
如果每个 Pod 具有不同的端点,请按照以下步骤使用注解公开指标:
确定要从中收集指标以进行监控的 Distributed Cloud 云项目。
将以下注解添加到容器的
Deployment文件的annotations部分:prometheus.io/pathprometheus.io/portprometheus.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在
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将
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定义文件的名称。