Updated on 2023-07-07 GMT+08:00

Installing Prometheus Adapter

The Custom Metric API in Kubernetes is required if HPA is implemented based on custom metrics. Currently, most users use the Prometheus Adapter to provide the Custom Metric API. The Prometheus Adapter converts the received custom metric APIs to Prometheus requests and returns data queried from Prometheus to the Custom Metric API Server. The Prometheus Adapter code repository is available at https://github.com/DirectXMan12/k8s-prometheus-adapter.

Prerequisites

The API aggregation layer in the cluster has been enabled. For details, see Configure the Aggregation Layer.

Procedure

Use the Prometheus Adapter chart in the Kubernetes chart repository to install the Prometheus Adapter. For details about the chart, see https://github.com/prometheus-community/helm-charts/tree/main/charts/prometheus-adapter.

  1. Connect to the cluster using kubectl.
  2. Install Helm3.

    wget https://storage.googleapis.com/kubernetes-helm/helm-v3.6.1-linux-amd64.tar.gz --no-check-certificate
    Decompress the file and save the helm binary file to the /usr/local/bin directory.
    tar zxvf helm-v3.6.1-linux-amd64.tar.gz
    mv linux-amd64/helm /usr/local/bin/helm

  3. Add a helm image repository.

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

    Run the helm repo list command to view the added helm image repository.

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

  4. Check configurations in the value.yaml file.

    • Information used by the Prometheus Adapter to connect to the Prometheus server includes the URL and port number. By default, the URL is http://prometheus.default.svc and the port number is 9090. Set the URL and port number based on the actual Prometheus server. If Prometheus is installed by following the procedure provided in Installing Prometheus, use the following recommended configurations:
      Create a value.yaml file and set installation parameters.
      vi value.yaml
      prometheus:
        url: http://prometheus-server.monitoring.svc
        port: 80
        path: ""
    • Customized rule configuration covers metric discovery rules, mapping between metrics and Kubernetes resources, exposed metric names in APIs, and corresponding Prometheus query statements. For details, see https://github.com/DirectXMan12/k8s-prometheus-adapter/blob/master/docs/config.md.

  5. Install the Prometheus Adapter.

    helm install prometheus-adapter prometheus-community/prometheus-adapter --values value.yaml --namespace monitoring
    • --namespace: Specify a namespace for installing the Prometheus Adapter. You need to manually create it in advance.
    • --values: Specify the installation parameters of the Prometheus Adapter as described in 4.
    After the installation is complete, run the following command to query all custom metrics:
    kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1"