搜索

3.8. 配置远程写入存储

download PDF

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

先决条件

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

    重要

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

  • 您已为远程写入端点在 Secret 对象中设置身份验证凭证。您必须在 openshift-user-workload-monitoring 命名空间中创建 secret。

    警告

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

流程

  1. 编辑 ConfigMap 对象:

    1. openshift-user-workload-monitoring 项目中编辑 user-workload-monitoring-config ConfigMap 对象:

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

      apiVersion: v1
      kind: ConfigMap
      metadata:
        name: user-workload-monitoring-config
        namespace: openshift-user-workload-monitoring
      data:
        config.yaml: |
          prometheus:
            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: user-workload-monitoring-config
        namespace: openshift-user-workload-monitoring
      data:
        config.yaml: |
          prometheus:
            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: user-workload-monitoring-config
        namespace: openshift-user-workload-monitoring
      data:
        config.yaml: |
          prometheus:
            remoteWrite:
            - url: "https://remote-write-endpoint.example.com"
              writeRelabelConfigs:
              - sourceLabels: [__name__]
                regex: 'my_metric'
                action: keep

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

  2. 保存文件以使改变生效。新的配置会被自动应用。

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

您可以使用不同的方法通过远程写入端点进行身份验证。目前支持的身份验证方法有 AWS 签名版本 4、基本身份验证、授权、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 证书文件、客户端证书文件和客户端密钥文件。

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

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

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

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

apiVersion: v1
kind: Secret
metadata:
  name: sigv4-credentials
  namespace: openshift-user-workload-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-user-workload-monitoring 命名空间中名为 sigv4-credentialsSecret 对象:

apiVersion: v1
kind: ConfigMap
metadata:
  name: user-workload-monitoring-config
  namespace: openshift-user-workload-monitoring
data:
  config.yaml: |
    prometheus:
      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)的唯一标识符。

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

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

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

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

apiVersion: v1
kind: ConfigMap
metadata:
  name: user-workload-monitoring-config
  namespace: openshift-user-workload-monitoring
data:
  config.yaml: |
    prometheus:
      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 对象中包含密码的密钥。

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

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

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

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

apiVersion: v1
kind: ConfigMap
metadata:
  name: user-workload-monitoring-config
  namespace: openshift-user-workload-monitoring
data:
  config.yaml: |
    enableUserWorkload: true
    prometheus:
      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 对象中包含身份验证令牌的密钥。

3.8.2.4. 用于 OAuth 2.0 验证的 YAML 示例

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

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

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

apiVersion: v1
kind: ConfigMap
metadata:
  name: user-workload-monitoring-config
  namespace: openshift-user-workload-monitoring
data:
  config.yaml: |
    prometheus:
      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 授权请求参数。

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

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

apiVersion: v1
kind: Secret
metadata:
  name: mtls-bundle
  namespace: openshift-user-workload-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: user-workload-monitoring-config
  namespace: openshift-user-workload-monitoring
data:
  config.yaml: |
    prometheus:
      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 对象中的密钥。

3.8.3. 远程写入队列配置示例

您可以使用 queueConfig 对象来远程写入,以调优远程写入队列参数。以下示例显示了 queue 参数,以及它们用于监控 openshift-user-workload-monitoring 命名空间中的用户定义的项目的默认值。

使用默认值进行远程写入参数配置示例

apiVersion: v1
kind: ConfigMap
metadata:
  name: user-workload-monitoring-config
  namespace: openshift-user-workload-monitoring
data:
  config.yaml: |
    prometheus:
      remoteWrite:
      - url: "https://remote-write-endpoint.example.com"
        <endpoint_authentication_credentials>
        queueConfig:
          capacity: 10000 1
          minShards: 1 2
          maxShards: 50 3
          maxSamplesPerSend: 2000 4
          batchSendDeadline: 5s 5
          minBackoff: 30ms 6
          maxBackoff: 5s 7
          retryOnRateLimit: false 8
          sampleAgeLimit: 0s 9

1
从队列丢弃每个分片前每个分片要缓冲的示例数量。
2
最小分片数量。
3
分片的最大数量。
4
每个发送的最大样本数。
5
在缓冲区中等待示例的最长时间。
6
在重试失败的请求前等待的初始时间。每次重试到 maxbackoff 时间的时间都会加倍。
7
在重试失败的请求前等待的最长时间。
8
将此参数设为 true,以在从远程写入存储接收 429 状态代码后重试请求。
9
早于 sampleAgeLimit 限制的示例会从队列中丢弃。如果值未定义,或设置为 0s,则忽略该参数。

其他资源

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

© 2024 Red Hat, Inc.