2.9. 配置远程写入存储
您可以配置远程写入存储,使 Prometheus 能够将最接近的指标发送到远程系统,以进行长期存储。这样做不会影响 Prometheus 存储指标的方式和时长。
先决条件
如果要配置 OpenShift Container Platform 核心监控组件
-
您可以使用具有
cluster-admin角色的用户访问集群。 -
您已创建
cluster-monitoring-configConfigMap对象。
-
您可以使用具有
如果您要配置用于监控用户定义的项目的组件:
-
您可以使用具有
cluster-admin角色的用户访问集群,也可以使用在openshift-user-workload-monitoring项目中具有user-workload-monitoring-config-edit角色的用户访问集群。 -
您已创建了
user-workload-monitoring-configConfigMap对象。
-
您可以使用具有
-
已安装 OpenShift CLI(
oc)。 - 您已设置了一个远程写入兼容端点(如 Thanos),并且知道端点 URL。有关与远程写入功能兼容的端点的信息,请参阅 Prometheus 远程端点和存储文档。
您已为远程写入端点设置了身份验证凭证。
Important要降低安全风险,请避免在不使用加密 HTTP 的情况下通过未加密的 HTTP 向端点发送指标。
流程
编辑
openshift-monitoring项目中的cluster-monitoring-configConfigMap对象:$ oc -n openshift-monitoring edit configmap cluster-monitoring-config-
在
data/config.yaml/prometheusK8s下添加一个remoteWrite:部分。 在本节中添加端点 URL 和身份验证凭证:
apiVersion: v1 kind: ConfigMap metadata: name: cluster-monitoring-config namespace: openshift-monitoring data: config.yaml: | prometheusK8s: remoteWrite: - url: "https://remote-write.endpoint" <endpoint_authentication_credentials>对于
endpoint_authentication_credentials,请替换端点的凭据。目前支持的身份验证方法是基本身份验证 (basicAuth) 和客户端 TLS (tlsConfig) 身份验证。以下示例配置基本身份验证:
basicAuth: username: <usernameSecret> password: <passwordSecret>相应地替换
<usernameSecret>和<passwordSecret>。以下示例显示了基本的身份验证配置,
remoteWriteAuth是name值,user和password是key值。这些值包含端点身份验证凭证:apiVersion: v1 kind: ConfigMap metadata: name: cluster-monitoring-config namespace: openshift-monitoring data: config.yaml: | prometheusK8s: remoteWrite: - url: "https://remote-write.endpoint" basicAuth: username: name: remoteWriteAuth key: user password: name: remoteWriteAuth key: password以下示例配置客户端 TLS 身份验证:
tlsConfig: ca: <caSecret> cert: <certSecret> keySecret: <keySecret>相应地替换
<caSecret>、<certSecret>和<keySecret>。以下示例显示了一个 TLS 验证配合,使用
selfsigned-mtls-bundle作为name值,ca.crt为cakey值,client.crt为certkey值,client.key为keySecretkey值:apiVersion: v1 kind: ConfigMap metadata: name: cluster-monitoring-config namespace: openshift-monitoring data: config.yaml: | prometheusK8s: remoteWrite: - url: "https://remote-write.endpoint" tlsConfig: ca: secret: name: selfsigned-mtls-bundle key: ca.crt cert: secret: name: selfsigned-mtls-bundle key: client.crt keySecret: name: selfsigned-mtls-bundle key: client.key
在身份验证凭证后添加 write relabel 配置值:
apiVersion: v1 kind: ConfigMap metadata: name: cluster-monitoring-config namespace: openshift-monitoring data: config.yaml: | prometheusK8s: remoteWrite: - url: "https://remote-write.endpoint" <endpoint_authentication_credentials> <write_relabel_configs>对于
<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" writeRelabelConfigs: - sourceLabels: [__name__] regex: 'my_metric' action: keep有关写入重新标记配置选项的详情,请查看 Prometheus relabel_config 文档。
如果需要,通过更改
nameandnamespacemetadata值,为监控用户定义的项目的 Prometheus 实例配置远程写入,如下所示: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" <endpoint_authentication_credentials> <write_relabel_configs>注意Prometheus 配置映射组件在
cluster-monitoring-configConfigMap对象中被称为prometheusK8s,在user-workload-monitoring-configConfigMap对象中称为prometheus。保存文件以将更改应用到
ConfigMap对象。受新配置重启影响的 Pod 会自动重启。注意除非集群管理员为用户定义的项目启用了监控,否则应用到
user-workload-monitoring-configConfigMap的配置不会被激活。警告保存对监控
ConfigMap对象的更改可能会重新部署相关项目中的 pod 和其他资源。保存更改还可能在该项目中重新启动正在运行的监控进程。