3.4. 为核心平台监控配置指标
配置指标集合,以监控集群组件和您自己的工作负载的执行方式。
您可以将最接近的指标发送到远程系统进行长期存储,并将集群 ID 标签添加到指标,以识别来自不同集群的数据。
3.4.1. 配置远程写入存储 复制链接链接已复制到粘贴板!
您可以配置远程写入存储,使 Prometheus 能够将最接近的指标发送到远程系统,以进行长期存储。这样做不会影响 Prometheus 存储指标的方式和时长。
先决条件
-
您可以使用具有
cluster-admin集群角色的用户身份访问集群。 -
您已创建
cluster-monitoring-configConfigMap对象。 -
已安装 OpenShift CLI(
oc)。 您已设置了一个远程写入兼容端点(如 Thanos),并且知道端点 URL。有关与远程写入功能兼容的端点的信息,请参阅 Prometheus 远程端点和存储文档。
重要红帽只提供配置远程写入发送者的信息,而不提供有关配置接收器端点的指导。客户负责设置自己的端点,这些端点与远程写入兼容。端点接收器配置的问题不包括在红帽产品支持中。
您已为远程写入端点在
Secret对象中设置身份验证凭证。您必须在openshift-monitoring命名空间中创建 secret。警告要减少安全风险,请使用 HTTPS 和身份验证向端点发送指标。
流程
编辑
openshift-monitoring项目中的cluster-monitoring-config配置映射:$ oc -n openshift-monitoring edit configmap cluster-monitoring-config在
data/config.yaml/prometheusK8s下添加一个remoteWrite:部分,如下例所示: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 在身份验证凭证后添加 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> writeRelabelConfigs: - <your_write_relabel_configs>1 - 1
- 为您要发送到远程端点的指标添加配置。
转发一个名为
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在
my_namespace命名空间中转发名为my_metric_1和my_metric_2的指标示例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__,namespace] regex: '(my_metric_1|my_metric_2);my_namespace' action: keep- 保存文件以使改变生效。新的配置会被自动应用。
3.4.1.1. 支持的远程写入身份验证设置 复制链接链接已复制到粘贴板!
您可以使用不同的方法通过远程写入端点进行身份验证。目前支持的身份验证方法有 AWS 签名版本 4、基本身份验证、授权、OAuth 2.0 和 TLS 客户端。下表提供有关用于远程写入的受支持身份验证方法的详情。
| 身份验证方法 | 配置映射字段 | 描述 |
|---|---|---|
| AWS 签名版本 4 |
| 此方法使用 AWS 签名版本 4 身份验证为请求签名。您不能搭配授权、OAuth 2.0 或基本身份验证同时使用此方法。 |
| 基本身份验证(Basic authentication) |
| 基本身份验证使用配置的用户名和密码在每个远程写入请求上设置授权标头。 |
| 授权 |
|
授权使用配置的令牌在每个远程写入请求上设置 |
| OAuth 2.0 |
|
OAuth 2.0 配置使用客户端凭据授予类型。Prometheus 使用指定的客户端 ID 和客户端 secret 从 |
| TLS 客户端 |
| TLS 客户端配置指定 CA 证书、客户端证书和客户端密钥文件信息,用于使用 TLS 与远程写入端点服务器进行身份验证。示例配置假定您已创建了 CA 证书文件、客户端证书文件和客户端密钥文件。 |
3.4.1.2. 远程写入身份验证设置示例 复制链接链接已复制到粘贴板!
以下示例展示了可用于连接到远程写入端点的不同身份验证设置。每个示例还演示了如何配置包含身份验证凭据和其他相关设置的对应 Secret 对象。每个示例配置身份验证,以用于 openshift-monitoring 命名空间中的默认平台监控。
3.4.1.2.1. AWS 签名版本 4 验证的 YAML 示例 复制链接链接已复制到粘贴板!
以下显示了 openshift-monitoring 命名空间中名为 sigv4-credentials 的 sigv4 secret 的设置。
apiVersion: v1
kind: Secret
metadata:
name: sigv4-credentials
namespace: openshift-monitoring
stringData:
accessKey: <AWS_access_key>
secretKey: <AWS_secret_key>
type: Opaque
下面显示了一个 AWS Signature Version 4 远程写入身份验证设置示例,它使用一个在 openshift-monitoring 命名空间中的名为 sigv4-credentials 的 Secret 对象:
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>
accessKey:
name: sigv4-credentials
key: accessKey
secretKey:
name: sigv4-credentials
key: secretKey
profile: <AWS_profile_name>
roleArn: <AWS_role_arn>
3.4.1.2.2. 用于基本身份验证的 YAML 示例 复制链接链接已复制到粘贴板!
以下显示了 openshift-monitoring 命名空间中名为 rw-basic-auth 的 Secret 对象基本身份验证设置示例:
apiVersion: v1
kind: Secret
metadata:
name: rw-basic-auth
namespace: openshift-monitoring
stringData:
user: <basic_username>
password: <basic_password>
type: Opaque
以下示例显示了使用 openshift-monitoring 命名空间中名为 rw-basic-auth 的 Secret 对象的 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
key: user
password:
name: rw-basic-auth
key: password
3.4.1.2.3. 使用 Secret 对象通过 bearer 令牌进行身份验证的 YAML 示例 复制链接链接已复制到粘贴板!
以下显示了 openshift-monitoring 命名空间中名为 rw-bearer-auth 的 Secret 对象的 bearer 令牌设置:
apiVersion: v1
kind: Secret
metadata:
name: rw-bearer-auth
namespace: openshift-monitoring
stringData:
token: <authentication_token>
type: Opaque
- 1
- 身份验证令牌。
以下显示了在 openshift-monitoring 命名空间中使用名为 rw-bearer-auth 的 Secret 对象的 bearer 令牌配置映射设置示例:
apiVersion: v1
kind: ConfigMap
metadata:
name: cluster-monitoring-config
namespace: openshift-monitoring
data:
config.yaml: |
prometheusK8s:
remoteWrite:
- url: "https://authorization.example.com/api/write"
authorization:
type: Bearer
credentials:
name: rw-bearer-auth
key: token
3.4.1.2.4. 用于 OAuth 2.0 验证的 YAML 示例 复制链接链接已复制到粘贴板!
以下显示了 openshift-monitoring 命名空间中名为 oauth2-credentials 的 Secret 对象的 OAuth 2.0 设置示例:
apiVersion: v1
kind: Secret
metadata:
name: oauth2-credentials
namespace: openshift-monitoring
stringData:
id: <oauth2_id>
secret: <oauth2_secret>
type: Opaque
下面显示了一个 oauth2 远程写入身份验证示例配置,它使用 openshift-monitoring 命名空间中名为 oauth2-credentials 的 Secret 对象:
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
key: id
clientSecret:
name: oauth2-credentials
key: secret
tokenUrl: https://example.com/oauth2/token
scopes:
- <scope_1>
- <scope_2>
endpointParams:
param1: <parameter_1>
param2: <parameter_2>
3.4.1.2.5. TLS 客户端身份验证的 YAML 示例 复制链接链接已复制到粘贴板!
以下显示了 openshift-monitoring 命名空间中名为 mtls-bundle 的 tls Secret 对象的 TLS 客户端设置示例。
apiVersion: v1
kind: Secret
metadata:
name: mtls-bundle
namespace: openshift-monitoring
data:
ca.crt: <ca_cert>
client.crt: <client_cert>
client.key: <client_key>
type: tls
以下示例显示了使用名为 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
key: ca.crt
cert:
secret:
name: mtls-bundle
key: client.crt
keySecret:
name: mtls-bundle
key: client.key
3.4.1.3. 远程写入队列配置示例 复制链接链接已复制到粘贴板!
您可以使用 queueConfig 对象来远程写入,以调优远程写入队列参数。以下示例显示了 queue 参数,以及它们在 openshift-monitoring 命名空间中默认平台监控的默认值。
使用默认值进行远程写入参数配置示例
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>
queueConfig:
capacity: 10000
minShards: 1
maxShards: 50
maxSamplesPerSend: 2000
batchSendDeadline: 5s
minBackoff: 30ms
maxBackoff: 5s
retryOnRateLimit: false
3.4.1.4. 远程写入指标表 复制链接链接已复制到粘贴板!
下表包含远程写入和远程 write-adjacent 指标,其中包含进一步的描述,以帮助在远程写入配置过程中解决问题。
| 指标 | 描述 |
|---|---|
|
| 显示 Prometheus 存储在任何样本的 write-ahead 日志(WAL)的最新时间戳。 |
|
| 显示远程写入队列成功发送的最新时间戳。 |
|
| 远程写入无法发送的示例数量,必须重新发送到远程存储。如果此指标一直稳定在高速率,则表示网络或远程存储端点的问题。 |
|
| 显示当前为每个远程端点运行多少个分片。 |
|
| 根据当前的写入吞吐量和传入与发送样本的速率显示计算的分片数量。 |
|
| 显示基于当前配置的分片的最大数量。 |
|
| 显示基于当前配置的分片的最小数量。 |
|
| Prometheus 当前正在写入新数据的 WAL 段文件。 |
|
| 每个远程写入实例当前读取的 WAL 段文件。 |
3.4.2. 为指标创建集群 ID 标签 复制链接链接已复制到粘贴板!
您可以通过在 openshift-monitoring 命名空间中为 cluster-monitoring-config 配置映射中添加远程写入存储的 write_relabel 设置来为指标创建集群 ID 标签。
先决条件
-
您可以使用具有
cluster-admin集群角色的用户身份访问集群。 -
您已创建
cluster-monitoring-configConfigMap对象。 -
已安装 OpenShift CLI(
oc)。 - 您已配置了远程写入存储。
流程
编辑
openshift-monitoring项目中的cluster-monitoring-config配置映射:$ oc -n openshift-monitoring edit configmap cluster-monitoring-config在
data/config.yaml/prometheusK8s/remoteWrite中的writeRelabelConfigs:部分添加集群 ID 重新标记配置值: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> writeRelabelConfigs:1 - <relabel_config>2 以下示例演示了如何使用集群 ID 标签
cluster_id转发指标: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: - __tmp_openshift_cluster_id__1 targetLabel: cluster_id2 action: replace3 - 保存文件以使改变生效。新的配置会被自动应用。