Lab: Horizontal Pod Autoscaler
Introduction
The Horizontal Pod Autoscaler automatically scales the number of pods in a replication controller,
deployment, replica set or stateful set based on observed CPU utilization. The Horizontal Pod
Autoscaler is implemented as a Kubernetes API resource and a controller. The resource determines
the behavior of the controller. The controller periodically adjusts the number of replicas in a
replication controller or deployment to match the observed average CPU utilization to the target
specified by user.
In this Lab, you will learn below items:
Objectives:
• Create a nginx deployment
• Create horizontalpodautoscaler
• Install siege – http load simulator
• Watch auto scale up
• Watch auto scale down
Note: Ensure you have running cluster deployed & Metrics Server deployed and configured.
1. Ensure that you have logged-in as root user with password as linux on kube-master node.
1.1 Let us clone the git repository which contains manifests required for this exercise, by
executing the below command.
# git clone https://github.com/EyesOnCloud/k8s-hpa.git
Output:
Student Material – Do Not Re-distribute. For any queries contact:
[email protected] or https://www.linkedin.com/in/naushadpasha/
1.2 Let us view the manifest to create a deployment for nginx, by executing the below
command.
# cat -n ~/k8s-hpa/nginx-deployment.yaml
Output:
Note: The cpu resource is limited to 100m; this will help us to scale based on cpu utilization.
1.3 Let us create the deployment, by executing the below command.
# kubectl create -f ~/k8s-hpa/nginx-deployment.yaml
Output:
Student Material – Do Not Re-distribute. For any queries contact:
[email protected] or https://www.linkedin.com/in/naushadpasha/
1.4 Let us verify the deployment, by executing the below command.
# kubectl get all -l app=nginx
Output:
1.5 Let us create a service of NodePort type to access our pod.
# kubectl expose deploy nginx --port=80 --type=NodePort
Output:
1.6 Let capture the nodeport by executing the below command.
# kubectl get svc nginx
Output:
1.7 Let verify if we are able to access the pod, by executing the below command.
# curl kube-node1:32019
Output:
Student Material – Do Not Re-distribute. For any queries contact:
[email protected] or https://www.linkedin.com/in/naushadpasha/
1.8 Let us view the manifest for creating hpa, by executing the below command.
# cat -n ~/k8s-hpa/nginx-hpa.yaml
Output:
1.9 Let us create the hpa, by executing the below command.
# kubectl create -f ~/k8s-hpa/nginx-hpa.yaml
Output:
Student Material – Do Not Re-distribute. For any queries contact:
[email protected] or https://www.linkedin.com/in/naushadpasha/
1.10 Let's verify hpa details, by executing the below command.
# kubectl get hpa nginx
Output:
Note: Be patient, it takes 5 minutes to display the target percentage.
Let us now simiulate the load to increase the cpu utlization and watch the hpa work. We will use siege –
which is an powerful HTTP load testing and benchmarking utility.
1.11 Let's us install epel repository and install siege utility, by executing below commands.
# yum install epel-release -y
# yum install siege.x86_64 -y
Output:
Student Material – Do Not Re-distribute. For any queries contact:
[email protected] or https://www.linkedin.com/in/naushadpasha/
1.12 Let us verify if siege is installed correctly, by executing the below command.
# siege -V
Output:
Note: Open another terminal – and run the below command
1.13 Let us watch the deployment, by executing the below command.
On Terminal 2:
# watch kubectl get all
Output:
1.14 Let us increase the load by running the siege utility.
On Terminal-1:
# siege -q -c 5 -t 2m http://kube-node1:32019
Output:
Note: -q = quiet mode, -c = concurrent users (we are setting it to 5), -t = time (we are setting it
to 2 minutes) and accessing one of the nodes and using the NodePort of nginx app.
Student Material – Do Not Re-distribute. For any queries contact:
[email protected] or https://www.linkedin.com/in/naushadpasha/
The controller checks the metrics every 15 seconds, as the load gradually increases, the pod
will begin to autoscale-up. Continue to watch the Terminal-2 to watch the pods autoscale
1.15 Let us continue to watch the pods, by executing the below command.
Terminal-2
# watch kubectl get all
Output:
Note: The siege command will complete after 2 minutes and the load will reduce after that
1.16 Let us continue to watch the pods, by executing the below command.
Terminal-2
# watch kubectl get all
Output:
Student Material – Do Not Re-distribute. For any queries contact:
[email protected] or https://www.linkedin.com/in/naushadpasha/
Note: As soon as the load is reduced, the cpu % is back to zero, but the pods will not autoscale-
down immediately. Instead it will wait for stabilizationWindowSeconds: which by default is 300
seconds and then scale-down.
After 5-7 minutes:
Output:
Output:
1.17 Let us cleanup the hpa, by executing the below commands.
# kubectl delete -f ~/k8s-hpa/
Output:
Student Material – Do Not Re-distribute. For any queries contact:
[email protected] or https://www.linkedin.com/in/naushadpasha/
1.18 Let us cleanup the service, by executing the below commands.
# kubectl delete service nginx
Output:
Student Material – Do Not Re-distribute. For any queries contact:
[email protected] or https://www.linkedin.com/in/naushadpasha/