你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 CRD 自定义集合(服务和 Pod 监视器)

注意

托管 Prometheus 的 CRD 支持目前以预览版提供。

启用托管 Prometheus 会自动为 Pod 监视器服务监视器部署自定义资源定义 (CRD)。 这些自定义资源定义与 Prometheus 的 OSS Pod 监视器OSS 服务监视器的自定义资源定义 (CRD) 相同,只是组名称有变化。 如果你的群集上存在现有的 Prometheus CRD 和自定义资源,则这些 CRD 不会与加载项创建的 CRD 冲突。 同时,托管 Prometheus 加载项不会拾取针对 OSS Prometheus 创建的 CRD。 这种分离是有意而为的,目的是隔离抓取作业。

创建 Pod 或服务监视器

使用 Pod 和服务监视器模板并遵循 API 规范来创建自定义资源(Pod 监视器服务监视器)。 请注意,要使托管 Prometheus 能够拾取现有的 OSS CR(自定义资源),只需对 API 组进行更改 - azmonitoring.coreos.com/v1

注意 - 请务必使用模板中指定的 labelLimit、labelNameLengthLimit 和 labelValueLengthLimit,以避免在处理过程中将它们删除

Pod 和服务监视器如以下示例所示:

Pod 监视器示例

# Note the API version is azmonitoring.coreos.com/v1 instead of monitoring.coreos.com/v1
apiVersion: azmonitoring.coreos.com/v1
kind: PodMonitor

# Can be deployed in any namespace
metadata:
  name: reference-app
  namespace: app-namespace
spec:
  labelLimit: 63
  labelNameLengthLimit: 511
  labelValueLengthLimit: 1023

  # The selector specifies which pods to filter for
  selector:

    # Filter by pod labels
    matchLabels:
      environment: test
    matchExpressions:
      - key: app
        operator: In
        values: [app-frontend, app-backend]

    # [Optional] Filter by pod namespace
    namespaceSelector:
      matchNames: [app-frontend, app-backend]

  # [Optional] Labels on the pod with these keys will be added as labels to each metric scraped
  podTargetLabels: [app, region, environment]

  # Multiple pod endpoints can be specified. Port requires a named port.
  podMetricsEndpoints:
    - port: metrics

服务监视器示例

# Note the API version is azmonitoring.coreos.com/v1 instead of monitoring.coreos.com/v1
apiVersion: azmonitoring.coreos.com/v1
kind: ServiceMonitor

# Can be deployed in any namespace
metadata:
  name: reference-app
  namespace: app-namespace
spec:
  labelLimit: 63
  labelNameLengthLimit: 511
  labelValueLengthLimit: 1023

  # The selector filters endpoints by service labels.
  selector:
    matchLabels:
      app: reference-app

  # Multiple endpoints can be specified. Port requires a named port.
  endpoints:
  - port: metrics

部署 Pod 或服务监视器

然后,可以使用 kubectl apply 部署 Pod 或服务监视器。

应用后,自定义资源中的任何错误都会显示出来,并且 Pod 或服务监视器应该无法应用。
成功创建 Pod 监视器后,会返回如下所示的结果 -

podmonitor.azmonitoring.coreos.com/my-pod-monitor created

示例

创建一个简单的应用程序

部署一个示例应用程序,用于公开 Pod/服务监视器所要配置的 Prometheus 指标。

kubectl apply -f https://github.com/Azure/prometheus-collector/blob/main/internal/referenceapp/prometheus-reference-app.yaml

创建 Pod 监视器和/或服务监视器以抓取指标

部署一个 Pod 监视器,该监视器配置为从上一步骤所述的示例应用程序中抓取指标。

Pod 监视器
kubectl apply -f https://github.com/Azure/prometheus-collector/blob/main/otelcollector/deploy/example-custom-resources/pod-monitor/pod-monitor-reference-app.yaml
服务监视器
kubectl apply -f https://github.com/Azure/prometheus-collector/blob/main/otelcollector/deploy/example-custom-resources/service-monitor/service-monitor-reference-app.yaml

故障排除

成功应用 Pod 或服务监视器后,加载项应自动开始从目标收集指标。 若要确认这一点,请按照此处的说明对自定义资源进行一般性的故障排除,并确保目标显示在 127.0.0.1/targets 中。

显示 Pod/服务监视器目标的屏幕截图

后续步骤