Kubernetes Horizontal Pod Autoscaling
Introduction
Kubernetes is a powerful container orchestration platform that has taken the world of enterprise computing by storm. It simplifies the deployment, scaling, and management of container-based applications. One of the most useful features of Kubernetes is its ability to scale applications horizontally through a feature called Horizontal Pod Autoscaling (HPA). HPA is an important tool for ensuring that applications are able to meet the demands of a changing workload. In this blog, we will discuss what HPA is, how it works, and how it can be used with a lab example.
What is Kubernetes Horizontal Pod Autoscaling (HPA)?
HPA is a feature of Kubernetes that allows applications to scale automatically based on CPU or memory utilization. When an application's resource utilization exceeds a predefined threshold, HPA will spin up additional pods to handle the extra load. HPA will also scale down the number of pods when the resource utilization drops below the threshold. This ensures that applications are able to handle sudden changes in workload without manual intervention.
How does Kubernetes Horizontal Pod Autoscaling work?
HPA works by monitoring the resource utilization of a Kubernetes cluster and spinning up or down additional pods to meet the demands of the application. HPA is able to do this by using custom metrics, such as CPU and memory utilization, as well as custom metrics provided by external services. HPA will also consider the current number of replicas when scaling, so that it doesn't spin up too many or too few pods.
HPA works by scaling the number of replicas up or down based on the current resource utilization. To do this, HPA uses a control loop that monitors the resource utilization, calculates a desired number of replicas, and then adjusts the number of replicas in the deployment.
HPA will also consider the current number of replicas when scaling, so that it doesn't spin up too many or too few pods.
Lab Example
In this lab, we will set up a Kubernetes deployment with Horizontal Pod Autoscaling enabled. We will be using the Kubernetes dashboard to configure HPA and will be using an example application that generates CPU load.
Step 1: Setting up the Kubernetes Cluster
The first step is to set up a Kubernetes cluster. We will be using the Kubernetes Dashboard to set up the cluster.
apiVersion: v1
kind: Pod
metadata:
name: default-cpu-demo-2
spec:
containers:
- name: default-cpu-demo-2-ctr
image: nginx
resources:
limits:
cpu: "1"
apiVersion: v1
kind: Pod
metadata:
name: default-cpu-demo-3
spec:
containers:
- name: default-cpu-demo-3-ctr
image: nginx
resources:
requests:
cpu: "0.75"
Step 2: Deploying the Application
Once the Kubernetes cluster is set up, we need to deploy the application. We will be using a sample application that generates CPU load. We can deploy the application using the Kubernetes Dashboard.
kind: Deployment
apiVersion: apps/v1
metadata:
name: mydeploy
spec:
replicas: 1
selector:
matchLabels:
name: deployment
template:
metadata:
name: testpod8
labels:
name: deployment
spec:
containers:
- name: c00
image: httpd
ports:
- containerPort: 80
resources:
limits:
cpu: 500m
requests:
cpu: 200m
Step 3: Configuring HPA
Once the application is deployed, we need to configure HPA. We can do this by navigating to the Horizontal Pod Autoscaling section of the Kubernetes Dashboard. Here, we can enable HPA and set the target CPU utilization that we want the application to reach.
apiVersion: v1
kind: LimitRange
metadata:
name: mem-min-max-demo-lr
spec:
limits:
- max:
memory: 1Gi
min:
memory: 500Mi
type: Container
Step 4: Testing the HPA
Once HPA is enabled, we can generate some CPU load to test that it is working properly. We can do this by running the application on the Kubernetes cluster. We should see that the number of replicas is increasing as the CPU utilization increases.
apiVersion: v1
kind: Pod
metadata:
name: constraints-mem-demo
spec:
containers:
- name: constraints-mem-demo-ctr
image: nginx
resources:
limits:
memory: "800Mi"
requests:
memory: "600Mi"
Conclusion:
In conclusion, Kubernetes Horizontal Pod Autoscaling (HPA) is a powerful tool for ensuring that applications are able to meet the demands of a changing workload. HPA works by monitoring the resource utilization of a Kubernetes cluster and spinning up or down additional pods to meet the demands of the application. We demonstrated how to set up HPA on a Kubernetes cluster and how to test it using a sample application. HPA is an important tool for ensuring that applications are able to handle sudden changes in workload without manual intervention.
Subscribe to my newsletter
Read articles from Shishir Tambe directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Shishir Tambe
Shishir Tambe
Newbie to IT/CS