Monitoring Kubernetes HPA's Current and Target CPU Utilization using Prometheus

Kubernetes, an open-source platform designed to automate deploying, scaling, and operating application containers, has become a go-to solution for managing containerized applications. One of its powerful features is the Horizontal Pod Autoscaler (HPA), which automatically scales the number of pods in a replication controller, deployment, replica set, or stateful set based on observed CPU utilization. However, monitoring the HPA’s current and target CPU utilization can be a challenge. This is where Prometheus, an open-source systems monitoring and alerting toolkit, comes in handy.

Monitoring Kubernetes HPA’s Current and Target CPU Utilization using Prometheus

Kubernetes, an open-source platform designed to automate deploying, scaling, and operating application containers, has become a go-to solution for managing containerized applications. One of its powerful features is the Horizontal Pod Autoscaler (HPA), which automatically scales the number of pods in a replication controller, deployment, replica set, or stateful set based on observed CPU utilization. However, monitoring the HPA’s current and target CPU utilization can be a challenge. This is where Prometheus, an open-source systems monitoring and alerting toolkit, comes in handy.

In this blog post, we’ll guide you through the process of monitoring Kubernetes HPA’s current and target CPU utilization using Prometheus.

Prerequisites

Before we dive in, make sure you have the following:

  • A Kubernetes cluster up and running
  • Basic understanding of Kubernetes and HPA
  • Prometheus and Grafana installed in your cluster

Step 1: Install Metrics Server

Metrics Server is a scalable, efficient source of container resource metrics for Kubernetes built-in autoscaling pipelines. It collects resource metrics from Kubelets and exposes them in Kubernetes API server through Metrics API.

To install Metrics Server in your cluster, run the following command:

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

Step 2: Deploy Sample Application and HPA

Next, we’ll deploy a sample application and HPA. For this example, we’ll use a simple CPU-intensive application and an HPA that scales based on CPU utilization.

kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/controllers/hpa/php-apache.yaml

Step 3: Install Prometheus Adapter

Prometheus Adapter is an extension for Kubernetes that provides custom metrics, system metrics, and external metrics for Kubernetes HPA.

To install Prometheus Adapter, use the following command:

kubectl apply -f https://raw.githubusercontent.com/DirectXMan12/k8s-prometheus-adapter/master/deploy/manifests/custom-metrics-config-map.yaml

Step 4: Configure Prometheus to Scrape Metrics

Now, we need to configure Prometheus to scrape metrics from the Metrics Server and Prometheus Adapter. Add the following scrape configs to your prometheus.yml:

- job_name: 'kubernetes-nodes'
  kubernetes_sd_configs:
  - role: node
  relabel_configs:
  - target_label: __address__
    replacement: kubernetes.default.svc:443
  - source_labels: [__meta_kubernetes_node_name]
    regex: (.+)
    target_label: __metrics_path__
    replacement: /api/v1/nodes/${1}/proxy/metrics

- job_name: 'kubernetes-apiservers'
  kubernetes_sd_configs:
  - role: endpoints
  scheme: https
  tls_config:
    ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
  bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
  relabel_configs:
  - source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
    action: keep
    regex: default;kubernetes;https

Step 5: Monitor HPA’s Current and Target CPU Utilization

Finally, you can monitor the HPA’s current and target CPU utilization using Prometheus and Grafana.

In Grafana, create a new dashboard and add the following PromQL queries:

  • Current CPU Utilization: kube_hpa_status_current_cpu_utilization_percentage
  • Target CPU Utilization: kube_hpa_spec_target_cpu_utilization_percentage

You can visualize these metrics in a graph to observe the scaling behavior of your HPA.

Conclusion

Monitoring Kubernetes HPA’s current and target CPU utilization is crucial for understanding how your applications are scaling and performing. With Prometheus and Grafana, you can easily visualize these metrics and make informed decisions about your application scaling strategies.

Remember, the key to successful application scaling is not only having the right tools but also understanding how to use them effectively. So, keep exploring and learning!

Keywords

  • Kubernetes
  • HPA
  • Prometheus
  • Grafana
  • Monitoring
  • CPU Utilization
  • Metrics Server
  • Prometheus Adapter
  • Scaling
  • Application Performance

About Saturn Cloud

Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.