12.3. 使用 Prometheus 的联邦端点查询指标

在 OpenShift Container Platform 4.10.17 中,您可以使用联邦端点从集群外的网络位置提取平台和用户定义的指标。为此,请通过 OpenShift Container Platform 路由访问集群的 Prometheus /federate 端点。

警告

使用联邦时检索指标数据的延迟。这个延迟可能会影响提取指标的准确性和时间表。

使用联邦端点也可以降低集群的性能和可扩展性,特别是在使用联邦端点来获取大量指标数据时。要避免这些问题,请遵循以下建议:

  • 不要尝试通过联邦端点检索所有指标数据。只有在您要检索有限、聚合的数据集时才会查询。例如,检索每个请求数量少于 1,000 个样本,有助于最大程度降低性能下降的风险。
  • 避免频繁查询联邦端点。将查询限制为每 30 秒最多一个。

如果您需要在集群外转发大量数据,请使用远程写入。如需更多信息,请参阅配置远程写入存储部分。

先决条件

  • 已安装 OpenShift CLI(oc)。
  • 已获得 OpenShift Container Platform 路由的主机 URL。
  • 您可以使用具有 cluster-monitoring-view 集群角色的用户,或者获取了对 命名空间 资源具有 get 权限的 bearer 令牌来访问集群。

    注意

    您只能使用 bearer 令牌身份验证来访问联邦端点。

流程

  1. 检索 bearer 令牌:

    $ token=`oc whoami -t`
  2. 查询 /federate 路由的指标。以下示例查询 up 指标 :

    $ curl -G -s -k -H "Authorization: Bearer $token" \
        'https:/<federation_host>/federate' \ 1
        --data-urlencode 'match[]=up'
    1
    对于 <federation_host>,替换为路由的主机 URL。

    输出示例

    # TYPE up untyped
    up{apiserver="kube-apiserver",endpoint="https",instance="10.0.143.148:6443",job="apiserver",namespace="default",service="kubernetes",prometheus="openshift-monitoring/k8s",prometheus_replica="prometheus-k8s-0"} 1 1657035322214
    up{apiserver="kube-apiserver",endpoint="https",instance="10.0.148.166:6443",job="apiserver",namespace="default",service="kubernetes",prometheus="openshift-monitoring/k8s",prometheus_replica="prometheus-k8s-0"} 1 1657035338597
    up{apiserver="kube-apiserver",endpoint="https",instance="10.0.173.16:6443",job="apiserver",namespace="default",service="kubernetes",prometheus="openshift-monitoring/k8s",prometheus_replica="prometheus-k8s-0"} 1 1657035343834
    ...