k8s
HPA
Kubernetes
CPU scaling
memory scaling

How to make k8s cpu and memory HPA work together?

System Design practice on Codemia

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

Practice system design

Overview

In a Kubernetes environment, applications can experience fluctuating demand and loads. To ensure they scale properly and maintain optimal performance, Kubernetes offers the Horizontal Pod Autoscaler (HPA), which is designed to adjust the number of pod replicas dynamically. HPA traditionally scales pods based on either CPU or memory usage metrics, but by combining these metrics, it can improve resource utilization and cost-effectiveness.

Understanding HPA

The Horizontal Pod Autoscaler watches the metrics specified in its configuration (CPU, memory, custom metrics, etc.) and scales the workloads in and out accordingly. Configuring HPA to handle both CPU and memory metrics concurrently ensures applications are allocated resources as per demand, preventing scenarios of CPU starvation or memory exhaustion.

Prerequisites

To make the HPA function effectively together for CPU and memory, you'll need:

  • A running Kubernetes cluster (at least version 1.6 for custom metrics support).
  • The Metrics Server deployed in the cluster to provide the necessary CPU and memory metrics.
  • kubectl command-line tool configured to communicate with your cluster.
  • Custom Metrics API if non-standard metrics are to be used.

HPA Configuration Combines CPU and Memory

Creating an HPA that considers both CPU and memory requires a balanced configuration that prioritizes the app's needs. Below is an example configuration:

  • type: Resource
  • type: Resource
  • **scaleTargetRef **: Points to the application deployment that the HPA will monitor and scale.
  • **minReplicas & maxReplicas **: Define the minimum and maximum number of replicas respectively.
  • **metrics **: Lists the resource metrics which HPA will monitor—both CPU and memory in this case.
  • **averageUtilization **: A target value that indicates the desired average utilization after scaling.
  • Balanced Thresholds: Ensure that neither CPU nor memory utilization threshold is disproportionately high or low, which might skew the scaling behavior.
  • Monitor & Tune: Continuously monitor the application performance after implementing HPA and fine-tune the average utilization thresholds for optimal performance.
  • Handling Overprovisioning: Be cautious about setting thresholds too low, which can cause unnecessary replica proliferation and resource wastage.
  • Application Characteristics: Understand if the application is more CPU or memory-intensive and adjust thresholds accordingly.
  • Prometheus: Set up a Prometheus instance in your cluster to scrape metrics from the Metrics Server.
  • Grafana: Use Grafana dashboards to visualize CPU, memory usage, and other application-specific metrics.

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.