
A HorizontalPodAutoscaler (HPA) automatically updates a workload resource (like a Deployment) to match demand.
The HPA is a core component of Kubernetes that allows applications to respond to traffic spikes without manual intervention and to save resources during quiet periods by scaling back down. In this walkthrough, we will deploy a test application and then apply a load to watch the HPA automatically increase and decrease the number of running pods.
This lab requires a running K3s cluster managed by Rancher, as configured in a previous lab
Based on https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/
For CPU-based autoscaling to work, our pods must declare how much CPU they request. This allows the HPA to calculate utilization as a percentage of the requested amount. We will deploy a sample php-apache application and configure its resources using the Rancher UI.
local cluster.hpa-demophp-apachehpa-demo from the dropdown.php-apache Container Image: registry.k8s.io/hpa-exampleCluster IP and Port to 80 200 (m stands for millicores; 200m is 0.2 of a CPU core).500 This tells Kubernetes to guarantee 200m CPU for this pod and to not let it use more than 500m. The HPA will use the 200m request as its baseline.You now have a deployment with one pod and an internal service to access it.


Now, let's create the HPA resource that will watch our php-apache deployment.
php-apachehpa-demoTarget Reference and then php-apache from the dropdowns.110ResourceCPUAverage Utilization50This configures the HPA to scale up when the average CPU usage across all pods exceeds 50% of their requested 200m (i.e., 100m).
Pods11030 This will make the HPA more reactive, so we wait less during testing
To trigger the autoscaler, we need to send traffic to our php-apache service. We'll run a simple busybox pod in a continuous loop to generate this load.

$ kubectl run -n hpa-demo -i --tty load-generator --rm --image=busybox:1.28 --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://php-apache; done"
It will be automatically removed (--rm) when you stop the command.
350.php-apache deployment. You will see the number of pods start to increase.50 target. It may stabilize at around 7 pods.

Take a screenshot that shows two things side-by-side:
php-apache Pods page showing the corresponding number of running pods.Upload the screenshot to complete the lab.
Now we'll stop the load and watch the HPA scale the application back down to its minimum size.
Ctrl + C to stop wget Followed by Ctrl + D to exit the container. the command. The load-generator pod will be terminated automatically.Ctrl + C twice or the pods still exist in an error/terminated state manually delete it before trying to generate a load command again.0.php-apache deployment decrease and stabilize back at the configured minimum of 1.To remove all the resources created in this lab you can delete its namespace:
hpa-demo namespace, click the three-dot menu on the right, and select Delete.