本页面简要介绍了从 Google Distributed Cloud (GDC) 气隙环境中的工作负载抓取指标的过程,以方便监控和数据可观测性。
您可以抓取和收集组件随时间生成的指标。监控平台提供自定义 API,用于从 Distributed Cloud 云项目命名空间内运行的工作负载抓取指标。如需抓取指标,您需要在 Management API 服务器中将 MonitoringTarget 自定义资源部署到项目命名空间。部署此资源后,监控平台会启动数据收集。
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配置中明确声明端点(端口、 路径、方案)。 注解:系统会从容器的
Deployment文件中的 注解检索 pod 指标端点信息。如果每个 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定义文件的名称。