Kubernetes
CPU Utilization
HPA
Autoscaling
Container Orchestration

How Kubernetes computes CPU utilization for HPA?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Kubernetes' Horizontal Pod Autoscaler (HPA) is an integral part of the Kubernetes ecosystem, offering the capability to automatically adjust the number of pod replicas based on CPU utilization or other select metrics. This auto-scaling functionality ensures resource efficiency and optimal performance of applications running in a Kubernetes cluster. Understanding how Kubernetes computes CPU utilization for HPA involves delving into the mechanisms of resource monitoring, metric collection, and the logic behind scaling decisions.

Understanding Resource Requests and Limits

In Kubernetes, each container in a pod can have resource requests and limits defined for CPU and memory. These parameters determine the minimum and maximum resources that a given container can consume.

CPU Requests: This is the minimum amount of CPU the container is guaranteed. • CPU Limits: This caps the maximum CPU a container is allowed to use.

CPU resources in Kubernetes are measured in CPU units. One CPU unit in Kubernetes corresponds to one virtual CPU core (for example, one vcore in GCP, or one vCPU in AWS).

Metrics Collection via Metrics Server

The Kubernetes Metrics Server plays a crucial role in collecting resource utilization data for the HPA. It provides resource utilization data such as CPU and memory usage across nodes and pods.

Metrics Server: This is a cluster-wide aggregator of monitoring data. It collects metrics from the `kubelet` (the node agent) and exposes them via the Kubernetes API so that they can be consumed by controllers like the HPA.

The Role of the Horizontal Pod Autoscaler (HPA)

The HPA scales the number of pod replicas based on observed CPU utilization compared to a target utilization specified by the user. The scaling decision is made based on the arithmetic mean of the CPU utilization percentage of all the pods controlled by the target resource.

Steps to Compute CPU Utilization for HPA

  1. Fetch Metrics: The HPA controller queries the Metrics Server at regular intervals (typically every 30 seconds) for the current CPU usage of each pod.
  2. Calculate Current Utilization: For each pod, the CPU usage is compared to the `cpu request` value, and a utilization percentage is calculated. For example:
    Utilization=Current CPU UsageCPU Requests×100\text{Utilization} = \frac{\text{Current CPU Usage}}{\text{CPU Requests}} \times 100
  3. Determine Target Utilization: The user specifies a target utilization, often as a percentage. In HPA configurations, this target is specified in the form of a percentage of the requested resources. For instance, if a pod has a CPU request of 500m (0.5 CPU), and the target utilization is 50%, the target is 250m.
  4. Compute Arithmetic Mean: The mean of the utilization percentages of all pods is calculated:
    Mean Utilization=i=1NUtilizationpodiN\text{Mean Utilization} = \frac{\sum_{i=1}^N \text{Utilization}_{\text{pod}_i}}{N}
    where NN is the number of pods.
  5. Decide Scaling Actions: If the mean utilization exceeds the target, the HPA increases the number of replicas; if it's below, it may reduce the count, provided it doesn't go below the minimum number of replicas specified.

Example Calculation

Consider a deployment with 3 pods, each with CPU requests set to 100m, and a target utilization of 80%. If their CPU usages are as follows:

• Pod 1: 90m • Pod 2: 85m • Pod 3: 95m

Calculate the utilization and determine the scaling decision.

• Utilization Pod 1 = 90100×100=90%\frac{90}{100} \times 100 = 90\% • Utilization Pod 2 = 85100×100=85%\frac{85}{100} \times 100 = 85\% • Utilization Pod 3 = 95100×100=95%\frac{95}{100} \times 100 = 95\%

Mean Utilization = 90+85+953=90%\frac{90 + 85 + 95}{3} = 90\%

Since 90% (mean utilization) > 80% (target), the HPA will decide to increase the number of replicas.

Summary Table

ComponentDescription
CPU RequestsMinimum guaranteed CPU for a container.
CPU LimitsMaximum allowable CPU for a container.
Metrics ServerAggregates and exposes resource usage metrics.
Target UtilizationDesired CPU usage percentage point for scaling.
Mean UtilizationAverage CPU utilization across pods, triggering scaling decisions.

Additional Considerations

Overhead in Metrics Collection: There can be a slight delay in metrics collection which might lead to outdated utilization information. • Cooldown Period: To prevent thrashing (rapid up and down scaling), a cooldown period can be configured in HPA to smooth out rapid fluctuations in load. • Custom Metrics: While CPU utilization is often used, Kubernetes HPA also supports custom metrics through the metrics API, allowing more complex autoscaling strategies.

Understanding the CPU utilization computation helps in making informed scaling decisions and efficiently configuring HPA to match the specific needs of your application workloads.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.