2.11. 配置远程写入存储

您可以配置远程写入存储,使 Prometheus 能够将最接近的指标发送到远程系统,以进行长期存储。这样做不会影响 Prometheus 存储指标的方式和时长。

先决条件

  • 如果要配置 OpenShift Container Platform 核心监控组件

    • 您可以使用具有 cluster-admin 集群角色的用户身份访问集群。
    • 您已创建 cluster-monitoring-config ConfigMap 对象。
  • 如果您要配置用于监控用户定义的项目的组件:

    • 您可以使用具有 cluster-admin 集群角色或具有 openshift-user-workload-monitoring 项目中的 user-workload-monitoring-config-edit 角色的用户访问集群。
    • 您已创建了 user-workload-monitoring-config ConfigMap 对象。
  • 已安装 OpenShift CLI(oc)。
  • 您已设置了一个远程写入兼容端点(如 Thanos),并且知道端点 URL。有关与远程写入功能兼容的端点的信息,请参阅 Prometheus 远程端点和存储文档

    重要

    红帽只提供配置远程写入发送者的信息,而不提供有关配置接收器端点的指导。客户负责设置自己的端点,这些端点与远程写入兼容。端点接收器配置的问题不包括在红帽产品支持中。

  • 您已为远程写入端点在 Secret 对象中设置身份验证凭证。您必须在与配置远程写入的 Prometheus 对象相同的命名空间中创建 secret:用于默认平台监控的 openshift-monitoring 命名空间或 openshift-user-workload-monitoring 命名空间用于用户工作负载监控。

    警告

    要减少安全风险,请使用 HTTPS 和身份验证向端点发送指标。

流程

按照以下步骤,在 openshift-monitoring 命名空间中的 cluster-monitoring-config 配置映射中为默认平台监控配置远程写入。

注意

如果您为监控用户定义的项目的 Prometheus 实例配置远程写入,请在 openshift-user-workload-monitoring 命名空间中对 user-workload-monitoring-config 配置映射进行类似的编辑。请注意,Prometheus 配置映射组件在 user-workload-monitoring-config ConfigMap 对象中称为 prometheus,而不是 cluster-monitoring-config ConfigMap 对象中的 prometheusK8s

  1. 编辑 openshift-monitoring 项目中的 cluster-monitoring-config ConfigMap 对象:

    $ oc -n openshift-monitoring edit configmap cluster-monitoring-config
  2. data/config.yaml/prometheusK8s 下添加一个 remoteWrite: 部分。
  3. 在本节中添加端点 URL 和身份验证凭证:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: cluster-monitoring-config
      namespace: openshift-monitoring
    data:
      config.yaml: |
        prometheusK8s:
          remoteWrite:
          - url: "https://remote-write-endpoint.example.com" 1
            <endpoint_authentication_credentials> 2
    1
    远程写入端点的 URL。
    2
    端点的身份验证方法和凭据。目前支持的身份验证方法有 AWS 签名版本 4,使用 HTTP Authorization 请求标头、基本身份验证、OAuth 2.0 和 TLS 客户端进行身份验证。有关支持的身份验证方法示例配置,请参阅支持的远程写入身份验证设置
  4. 在身份验证凭证后添加 write relabel 配置值:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: cluster-monitoring-config
      namespace: openshift-monitoring
    data:
      config.yaml: |
        prometheusK8s:
          remoteWrite:
          - url: "https://remote-write-endpoint.example.com"
            <endpoint_authentication_credentials>
            <your_write_relabel_configs> 1
    1
    写入重新标记配置设置。

    对于 <your_write_relabel_configs>,请替换您要发送到远程端点的指标写入重新标记配置列表。

    以下示例演示了如何转发名为 my_metric 的单个指标:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: cluster-monitoring-config
      namespace: openshift-monitoring
    data:
      config.yaml: |
        prometheusK8s:
          remoteWrite:
          - url: "https://remote-write-endpoint.example.com"
            writeRelabelConfigs:
            - sourceLabels: [__name__]
              regex: 'my_metric'
              action: keep

    有关写入重新标记配置选项的详情,请查看 Prometheus relabel_config 文档

  5. 保存文件以将更改应用到 ConfigMap 对象。受新配置重启影响的 Pod 会自动重启。

    注意

    除非集群管理员为用户定义的项目启用了监控,否则应用到 user-workload-monitoring-config ConfigMap 的配置不会被激活。

    警告

    保存对监控 ConfigMap 对象的更改可能会重新部署相关项目中的 pod 和其他资源。保存更改还可能在该项目中重新启动正在运行的监控进程。

2.11.1. 支持的远程写入身份验证设置

您可以使用不同的方法通过远程写入端点进行身份验证。目前支持的身份验证方法有 AWS 签名版本 4,使用 HTTP Authorization 请求标头、基本身份验证、OAuth 2.0 和 TLS 客户端进行身份验证。下表提供有关用于远程写入的受支持身份验证方法的详情。

身份验证方法配置映射字段描述

AWS 签名版本 4

sigv4

此方法使用 AWS 签名版本 4 身份验证为请求签名。您不能搭配授权、OAuth 2.0 或基本身份验证同时使用此方法。

基本身份验证(Basic authentication)

basicAuth

基本身份验证使用配置的用户名和密码在每个远程写入请求上设置授权标头。

授权

授权

授权使用配置的令牌在每个远程写入请求上设置 Authorization 标头。

OAuth 2.0

oauth2

OAuth 2.0 配置使用客户端凭据授予类型。Prometheus 使用指定的客户端 ID 和客户端 secret 从 tokenUrl 获取访问令牌来访问远程写入端点。您不能与授权、AWS 签名版本 4 或基本身份验证同时使用此方法。

TLS 客户端

tlsConfig

TLS 客户端配置指定 CA 证书、客户端证书和客户端密钥文件信息,用于使用 TLS 与远程写入端点服务器进行身份验证。示例配置假定您已创建了 CA 证书文件、客户端证书文件和客户端密钥文件。

2.11.1.1. 身份验证设置的配置映射位置

以下显示了 ConfigMap 中用于默认平台监控的身份验证配置的位置。

apiVersion: v1
kind: ConfigMap
metadata:
  name: cluster-monitoring-config
  namespace: openshift-monitoring
data:
  config.yaml: |
    prometheusK8s:
      remoteWrite:
      - url: "https://remote-write-endpoint.example.com" 1
        <endpoint_authentication_details> 2
1
远程写入端点的 URL。
2
端点验证方法所需的配置详情。目前支持的身份验证方法有 Amazon Web Services (AWS) Signature 版本 4,使用 HTTP Authorization 请求标头、基本身份验证、OAuth 2.0 和 TLS 客户端进行身份验证。
注意

如果您为监控用户定义的项目的 Prometheus 实例配置远程写入,请在 openshift-user-workload-monitoring 命名空间中编辑 user-workload-monitoring-config 配置映射。请注意,Prometheus 配置映射组件在 user-workload-monitoring-config ConfigMap 对象中称为 prometheus,而不是 cluster-monitoring-config ConfigMap 对象中的 prometheusK8s

2.11.1.2. 远程写入身份验证设置示例

以下示例展示了可用于连接到远程写入端点的不同身份验证设置。每个示例还演示了如何配置包含身份验证凭据和其他相关设置的对应 Secret 对象。每个示例配置身份验证,以用于 openshift-monitoring 命名空间中的默认平台监控。

AWS 签名版本 4 验证的 YAML 示例

以下显示了 openshift-monitoring 命名空间中名为 sigv4-credentialssigv4 secret 的设置。

apiVersion: v1
kind: Secret
metadata:
  name: sigv4-credentials
  namespace: openshift-monitoring
stringData:
  accessKey: <AWS_access_key> 1
  secretKey: <AWS_secret_key> 2
type: Opaque
1
AWS API 访问密钥。
2
AWS API secret 密钥。

下面显示了一个 AWS Signature Version 4 远程写入身份验证设置示例,它使用一个在 openshift-monitoring 命名空间中的名为 sigv4-credentialsSecret 对象:

apiVersion: v1
kind: ConfigMap
metadata:
  name: cluster-monitoring-config
  namespace: openshift-monitoring
data:
  config.yaml: |
    prometheusK8s:
      remoteWrite:
      - url: "https://authorization.example.com/api/write"
        sigv4:
          region: <AWS_region> 1
          accessKey:
            name: sigv4-credentials 2
            key: accessKey 3
          secretKey:
            name: sigv4-credentials 4
            key: secretKey 5
          profile: <AWS_profile_name> 6
          roleArn: <AWS_role_arn> 7
1
AWS 区域。
2 4
包含 AWS API 访问凭证的 Secret 对象的名称。
3
在指定 Secret 对象中包含 AWS API 访问密钥的密钥。
5
在指定的 Secret 对象中包含 AWS API secret 键的密钥。
6
用于验证的 AWS 配置集的名称。
7
分配给角色的 Amazon 资源名称(ARN)的唯一标识符。

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

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

apiVersion: v1
kind: Secret
metadata:
  name: rw-basic-auth
  namespace: openshift-monitoring
stringData:
  user: <basic_username> 1
  password: <basic_password> 2
type: Opaque
1
用户名。
2
密码。

以下示例显示了使用 openshift-monitoring 命名空间中名为 rw-basic-authSecret 对象的 basicAuth 远程写入配置。它假设您已为端点设置了身份验证凭据。

apiVersion: v1
kind: ConfigMap
metadata:
  name: cluster-monitoring-config
  namespace: openshift-monitoring
data:
  config.yaml: |
    prometheusK8s:
      remoteWrite:
      - url: "https://basicauth.example.com/api/write"
        basicAuth:
          username:
            name: rw-basic-auth 1
            key: user 2
          password:
            name: rw-basic-auth 3
            key: password 4
1 3
包含身份验证凭据的 Secret 对象的名称。
2
在指定的 Secret 对象中包含用户名的密钥。
4
在指定 Secret 对象中包含密码的密钥。

使用 Secret 对象通过 bearer 令牌进行身份验证的 YAML 示例

以下显示了 openshift-monitoring 命名空间中名为 rw-bearer-authSecret 对象的 bearer 令牌设置:

apiVersion: v1
kind: Secret
metadata:
  name: rw-bearer-auth
  namespace: openshift-monitoring
stringData:
  token: <authentication_token> 1
type: Opaque
1
身份验证令牌。

以下显示了在 openshift-monitoring 命名空间中使用名为 rw-bearer-authSecret 对象的 bearer 令牌配置映射设置示例:

apiVersion: v1
kind: ConfigMap
metadata:
  name: cluster-monitoring-config
  namespace: openshift-monitoring
data:
  config.yaml: |
    enableUserWorkload: true
    prometheusK8s:
      remoteWrite:
      - url: "https://authorization.example.com/api/write"
        authorization:
          type: Bearer 1
          credentials:
            name: rw-bearer-auth 2
            key: token 3
1
请求的验证类型。默认值为 Bearer
2
包含身份验证凭据的 Secret 对象的名称。
3
在指定的 Secret 对象中包含身份验证令牌的密钥。

用于 OAuth 2.0 验证的 YAML 示例

以下显示了 openshift-monitoring 命名空间中名为 oauth2-credentialsSecret 对象的 OAuth 2.0 设置示例:

apiVersion: v1
kind: Secret
metadata:
  name: oauth2-credentials
  namespace: openshift-monitoring
stringData:
  id: <oauth2_id> 1
  secret: <oauth2_secret> 2
  token: <oauth2_authentication_token> 3
type: Opaque
1
Oauth 2.0 ID。
2
OAuth 2.0 secret。
3
OAuth 2.0 令牌。

下面显示了一个 oauth2 远程写入身份验证示例配置,它使用 openshift-monitoring 命名空间中名为 oauth2-credentialsSecret 对象:

apiVersion: v1
kind: ConfigMap
metadata:
  name: cluster-monitoring-config
  namespace: openshift-monitoring
data:
  config.yaml: |
    prometheusK8s:
      remoteWrite:
      - url: "https://test.example.com/api/write"
        oauth2:
          clientId:
            secret:
              name: oauth2-credentials 1
              key: id 2
          clientSecret:
            name: oauth2-credentials 3
            key: secret 4
          tokenUrl: https://example.com/oauth2/token 5
          scopes: 6
          - <scope_1>
          - <scope_2>
          endpointParams: 7
            param1: <parameter_1>
            param2: <parameter_2>
1 3
对应的 Secret 对象的名称。请注意,ClientId 可以引用 ConfigMap 对象,但 clientSecret 必须引用 Secret 对象。
2 4
在指定 Secret 对象中包含 OAuth 2.0 凭证的密钥。
5
用于通过指定的 clientIdclientSecret 获取令牌的 URL。
6
授权请求的 OAuth 2.0 范围。这些范围限制了令牌可以访问的数据。
7
授权服务器所需的 OAuth 2.0 授权请求参数。

TLS 客户端身份验证的 YAML 示例

以下显示了 openshift-monitoring 命名空间中名为 mtls-bundletls Secret 对象的 TLS 客户端设置示例。

apiVersion: v1
kind: Secret
metadata:
  name: mtls-bundle
  namespace: openshift-monitoring
data:
  ca.crt: <ca_cert> 1
  client.crt: <client_cert> 2
  client.key: <client_key> 3
type: tls
1
Prometheus 容器中用于验证服务器证书的 CA 证书。
2
用于与服务器进行身份验证的客户端证书。
3
客户端密钥。

以下示例显示了使用名为 mtls-bundle 的 TLS Secret 对象的 tlsConfig 远程写入身份验证配置。

apiVersion: v1
kind: ConfigMap
metadata:
  name: cluster-monitoring-config
  namespace: openshift-monitoring
data:
  config.yaml: |
    prometheusK8s:
      remoteWrite:
      - url: "https://remote-write-endpoint.example.com"
        tlsConfig:
          ca:
            secret:
              name: mtls-bundle 1
              key: ca.crt 2
          cert:
            secret:
              name: mtls-bundle 3
              key: client.crt 4
          keySecret:
            name: mtls-bundle 5
            key: client.key 6
1 3 5
包含 TLS 身份验证凭证的对应 Secret 对象的名称。请注意,cacert 可以引用 ConfigMap 对象,但 keySecret 必须引用 Secret 对象。
2
指定 Secret 对象中的键,其中包含端点的 CA 证书。
4
指定 Secret 对象中的键,其中包含端点的客户端证书。
6
包含客户端密钥 secret 的指定 Secret 对象中的密钥。

其他资源