搜索

9.2. 为用户定义的项目设置指标集合

download PDF

您可以创建一个 ServiceMonitor 资源,从用户定义的项目中的服务端点提取指标。这假设您的应用程序使用 Prometheus 客户端库向 /metrics 规范名称公开指标。

本节介绍了如何在用户定义的项目中部署示例服务,然后创建一个 ServiceMonitor 资源来定义应该如何监控该服务。

9.2.1. 部署示例服务

要为用户定义的项目中服务测试监控,您可以部署示例服务。

流程

  1. 为服务配置创建 YAML 文件。在本例中,该文件名为 prometheus-example-app.yaml
  2. 在该文件中添加以下部署和服务配置详情:

    apiVersion: v1
    kind: Namespace
    metadata:
      name: ns1
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: prometheus-example-app
      name: prometheus-example-app
      namespace: ns1
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: prometheus-example-app
      template:
        metadata:
          labels:
            app: prometheus-example-app
        spec:
          containers:
          - image: ghcr.io/rhobs/prometheus-example-app:0.4.2
            imagePullPolicy: IfNotPresent
            name: prometheus-example-app
    ---
    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: prometheus-example-app
      name: prometheus-example-app
      namespace: ns1
    spec:
      ports:
      - port: 8080
        protocol: TCP
        targetPort: 8080
        name: web
      selector:
        app: prometheus-example-app
      type: ClusterIP

    此配置会在用户定义的 ns1 项目中部署名为 prometheus-example-app 的服务。此服务会公开自定义 version 指标。

  3. 将配置应用到集群:

    $ oc apply -f prometheus-example-app.yaml

    部署该服务需要一些时间。

  4. 您可以检查该 Pod 是否正在运行:

    $ oc -n ns1 get pod

    输出示例

    NAME                                      READY     STATUS    RESTARTS   AGE
    prometheus-example-app-7857545cb7-sbgwq   1/1       Running   0          81m

9.2.2. 指定如何监控服务

要使用服务公开的指标,您必须将 OpenShift Dedicated 监控配置为从 /metrics 端点中提取指标。您可以使用一个 ServiceMonitor 自定义资源定义(CRD)应该如何监控服务,或使用一个 PodMonitor CRD 指定应该如何监控 pod。前者需要 Service 对象,而后者则不需要,允许 Prometheus 直接从 Pod 公开的指标端点中提取指标。

此流程演示了如何为用户定义的项目中的服务创建 ServiceMonitor 资源。

前提条件

  • 您可以使用具有 dedicated-admin 角色或 monitoring-edit 角色的用户访问集群。
  • 在本例中,您已在 ns1 项目中部署了 prometheus-example-app 示例服务。

    注意

    prometheus-example-app 示例服务不支持 TLS 身份验证。

流程

  1. 创建名为 example-app-service-monitor.yaml 的新 YAML 配置文件。
  2. ServiceMonitor 资源添加到 YAML 文件中。以下示例创建一个名为 prometheus-example-monitor 的服务监控器,用于提取 ns1 命名空间中的 prometheus-example-app 服务公开的指标:

    apiVersion: monitoring.coreos.com/v1
    kind: ServiceMonitor
    metadata:
      name: prometheus-example-monitor
      namespace: ns1 1
    spec:
      endpoints:
      - interval: 30s
        port: web 2
        scheme: http
      selector: 3
        matchLabels:
          app: prometheus-example-app
    1
    指定服务运行的用户定义的命名空间。
    2
    指定要由 Prometheus 提取的端点端口。
    3
    配置选择器,根据其元数据标签匹配您的服务。
    注意

    用户定义的命名空间中的 ServiceMonitor 资源只能发现同一命名空间中的服务。也就是说,ServiceMonitor 资源的 namespaceSelector 字段总是被忽略。

  3. 将配置应用到集群:

    $ oc apply -f example-app-service-monitor.yaml

    部署 ServiceMonitor 资源需要一些时间。

  4. 验证 ServiceMonitor 资源是否正在运行:

    $ oc -n <namespace> get servicemonitor

    输出示例

    NAME                         AGE
    prometheus-example-monitor   81m

9.2.3. 服务端点身份验证设置示例

您可以使用 ServiceMonitorPodMonitor 自定义资源定义(CRD)为用户定义的项目监控配置服务端点身份验证。

以下示例显示了 ServiceMonitor 资源的不同身份验证设置。每个示例演示了如何配置包含身份验证凭据和其他相关设置的对应 Secret 对象。

9.2.3.1. 使用 bearer 令牌的 YAML 身份验证示例

以下示例显示了 ns1 命名空间中名为 example-bearer-authSecret 对象的 bearer 令牌设置:

bearer 令牌 secret 示例

apiVersion: v1
kind: Secret
metadata:
  name: example-bearer-auth
  namespace: ns1
stringData:
  token: <authentication_token> 1

1
指定身份验证令牌。

以下示例显示了 ServiceMonitor CRD 的 bearer 令牌身份验证设置。这个示例使用名为 example-bearer-authSecret 对象:

bearer 令牌身份验证设置示例

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: prometheus-example-monitor
  namespace: ns1
spec:
  endpoints:
  - authorization:
      credentials:
        key: token 1
        name: example-bearer-auth 2
    port: web
  selector:
    matchLabels:
      app: prometheus-example-app

1
在指定的 Secret 对象中包含身份验证令牌的密钥。
2
包含身份验证凭据的 Secret 对象的名称。
重要

不要使用 bearerTokenFile 来配置 bearer 令牌。如果使用 bearerTokenFile 配置,ServiceMonitor 资源将被拒绝。

9.2.3.2. 用于基本身份验证的 YAML 示例

以下示例显示了 ns1 命名空间中名为 example-basic-authSecret 对象基本身份验证设置:

基本身份验证 secret 示例

apiVersion: v1
kind: Secret
metadata:
  name: example-basic-auth
  namespace: ns1
stringData:
  user: <basic_username> 1
  password: <basic_password>  2

1
指定用于身份验证的用户名。
2
指定用于身份验证的密码。

以下示例显示了 ServiceMonitor CRD 的基本身份验证设置。这个示例使用名为 example-basic-authSecret 对象:

基本身份验证设置示例

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: prometheus-example-monitor
  namespace: ns1
spec:
  endpoints:
  - basicAuth:
      username:
        key: user 1
        name: example-basic-auth 2
      password:
        key: password 3
        name: example-basic-auth 4
    port: web
  selector:
    matchLabels:
      app: prometheus-example-app

1
在指定的 Secret 对象中包含用户名的密钥。
2 4
包含基本身份验证的 Secret 对象的名称。
3
在指定 Secret 对象中包含密码的密钥。

9.2.3.3. 使用 OAuth 2.0 的 YAML 身份验证示例

以下示例显示了 ns1 命名空间中名为 example-oauth2Secret 对象的 OAuth 2.0 设置:

OAuth 2.0 secret 示例

apiVersion: v1
kind: Secret
metadata:
  name: example-oauth2
  namespace: ns1
stringData:
  id: <oauth2_id> 1
  secret: <oauth2_secret> 2

1
指定 Oauth 2.0 ID。
2
指定 Oauth 2.0 secret。

以下示例显示了 ServiceMonitor CRD 的 OAuth 2.0 身份验证设置。这个示例使用名为 example-oauth2Secret 对象:

OAuth 2.0 身份验证设置示例

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: prometheus-example-monitor
  namespace: ns1
spec:
  endpoints:
  - oauth2:
      clientId:
        secret:
          key: id 1
          name: example-oauth2 2
      clientSecret:
        key: secret 3
        name: example-oauth2 4
      tokenUrl: https://example.com/oauth2/token 5
    port: web
  selector:
    matchLabels:
      app: prometheus-example-app

1
在指定 Secret 对象中包含 OAuth 2.0 ID 的密钥。
2 4
包含 OAuth 2.0 凭证的 Secret 对象的名称。
3
在指定 Secret 对象中包含 OAuth 2.0 secret 的密钥。
5
用于通过指定的 clientIdclientSecret 获取令牌的 URL。
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.