更新时间:2023-07-07 GMT+08:00

Prometheus-Adapter安装

使用自定义指标的HPA,需要提供社区标准的Custom Metric API,当前大部分用户使用Prometheus-Adapter来提供Custom Metric API,其主要功能是将接受到custom metric api转换成普罗的请求,从普罗中查询数据返回给API Server,具体的代码仓库地址是https://github.com/DirectXMan12/k8s-prometheus-adapter

前提条件

确保集群中API聚合层已打开,具体操作请参考配置聚合层

安装步骤

使用kubernetes charts仓库中prometheus-adapter的chart包进行安装,Chart包详细介绍可以从https://github.com/prometheus-community/helm-charts/tree/main/charts/prometheus-adapter查看。

  1. 通过kubectl连接集群。
  2. 安装helm3。

    wget https://storage.googleapis.com/kubernetes-helm/helm-v3.6.1-linux-amd64.tar.gz --no-check-certificate
    解压文件,将helm二进制放置到/usr/local/bin目录下。
    tar zxvf helm-v3.6.1-linux-amd64.tar.gz
    mv linux-amd64/helm /usr/local/bin/helm

  3. 添加helm镜像仓库。

    helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
    helm repo update

    添加成功后,可使用helm repo list命令查看:

    NAME                    URL                                               
    prometheus-community    https://prometheus-community.github.io/helm-charts

  4. 确认配置的参数,写入到value.yaml中。

    • adapter连接的prometheus的信息,主要是url和端口,默认值是http://prometheus.default.svc,端口是9090,请根据实际的prometheus server设置。如果按照Prometheus安装安装的prometheus,则请使用如下推荐配置。
      创建value.yaml,配置安装参数。
      vi value.yaml
      prometheus:
        url: http://prometheus-server.monitoring.svc
        port: 80
        path: ""
    • 自定义规则配置,主要是配置指标的发现规则,指标与kubernetes资源的对应关系,在api中指标的暴露的名称以及对应的prometheus查询的语句,详细的配置可以查看https://github.com/DirectXMan12/k8s-prometheus-adapter/blob/master/docs/config.md

  5. 执行如下命令安装Prometheus-Adapter:

    helm install prometheus-adapter prometheus-community/prometheus-adapter --values value.yaml --namespace monitoring
    • --namespace:指定Prometheus-Adapter安装的命名空间,需要提前手动创建。
    • --values:指定4中Prometheus-Adapter的安装参数。
    安装成功后,可以通过下面命令查询所有的自定义指标。
    kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1"