Skip to content

Horizontal Pod Autoscaling

DevOpsByMK edited this page Jan 30, 2022 · 2 revisions

Horizontal Pod Autoscaling

  • In Kubernetes, a HorizontalPodAutoscaler automatically updates a workload resource (such as a Deployment or StatefulSet), with the aim of automatically scaling the workload to match demand.

  • Horizontal scaling means that the response to increased load is to deploy more Pods. This is different from vertical scaling, which for Kubernetes would mean assigning more resources (for example: memory or CPU) to the Pods that are already running for the workload.

  • If the load decreases, and the number of Pods is above the configured minimum, the HorizontalPodAutoscaler instructs the workload resource (the Deployment, StatefulSet, or other similar resource) to scale back down.

  • Horizontal pod autoscaling does not apply to objects that can't be scaled (for example: a DaemonSet.)

I have implemented the above concept with the below example.

Follow the below steps to implement above concept:

  1. Create a namespace(optional)

    kubectl create ns hpa-demo

  2. Deploy any image into the namespace (I have taken grafana here)

    kubectl create deployment grafana-demo --image=grafana/grafana -n hpa-demo

  3. Set the resources to your deployment (Optional.. Reference link: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/)

    kubectl set resources deploy grafana-demo --requests=cpu=200m -n hpa-demo

  4. Expose your deployment so that it can be accessible from outside.

    kubectl expose deploy grafana-demo --port 3000 -n hpa-demo

  5. List all the activities you performed.

    kubectl get all -n hpa-demo

  6. Now execute the below command to autoscale the deployment. (To check I kept cpu-percent as 25% but in realtime you can put actual values)

    kubectl autoscale deployment grafana-demo --cpu-percent=25 --min=1 --max=10 -n hpa-demo

    To increase load on the pod,

    • you can login to the cluster and do wget the deployment using clusterip and port.

    • Or you can do port forward and do wget from any linux machine.

  7. To implement port forward execute the below command.

    kubectl port-forward pod/grafana-demo-d4cd8bc67-fq2xl 3000:3000 -n hpa-demo

    Note: pod/grafana-demo-d4cd8bc67-fq2xl is name of the pod.

  8. Below command is to increase load on pod

    Cluster IP scenario: while true; do wget -q -O - http://10.43.89.149:3000/?orgId=1; done

    Port forward scenario: while true; do wget -q -O - http://127.0.0.1:3000/?orgId=1; done

  9. To watch your HPA open another terminal and execute the below command

    kubectl get hpa -w -n hpa-demo

Note : Cluster IP will change but loopback IP is constant.

Open multiple terminals and execute load command so that load will get increase and pods will be auto deployed.

To come out type CTRL+C so that you can come out and load will be reduced.

After some time you execute kubectl get all -n hpa-demo you will find only one pod, one deployment and one replicaset.


Clone this wiki locally