Horizontal Pod autoscaling – Managing Advanced Kubernetes Resources-2

Now, let’s autoscale this deployment. The Deployment resource needs at least 1 pod replica and can have a maximum of 5 pod replicas while maintaining an average CPU utilization of 25%. Use the following command to create a HorizontalPodAutoscaler resource:

$ kubectl autoscale deployment nginx –cpu-percent=25 –min=1 –max=5

Now that we have the HorizontalPodAutoscaler resource created, we can load test the application using the hey load testing utility preinstalled in Google Cloud Shell. But before you fire the load test, open a duplicate shell session and watch the Deployment resource using the following command:

$ kubectl get deployment nginx -w

Open another duplicate shell session and watch the HorizontalPodAutoscaler resource using the following command:

$ kubectl get hpa nginx -w

Now, in the original window, run the following command to fire a load test:

$ hey -z 120s -c 100 http://34.123.234.57

It will start a load test for 2 minutes, with 10 concurrent users continuously hammering the Service. You will see the following output if you open the window where you’re watching the HorizontalPodAutoscaler resource. As soon as we start firing the load test, the average utilization reaches 46%. The HorizontalPodAutoscaler resource waits for some time, then it increases the replicas, first to 2, then to 4, and finally to 5. When the test is complete, the utilization drops quickly to 27%, 25%, and finally, 0%. When the utilization goes to 0%, the HorizontalPodAutoscaler resource spins down the replicas from 5 to 1 gradually:

$ kubectl get hpa nginx -w     
NAMEREFERENCETARGETSMINPODSMAXPODSREPLICASAGE
nginxdeployment/nginx<unknown>/25%15132s
nginxdeployment/nginx46%/25%15171s
nginxdeployment/nginx46%/25%15292s
nginxdeployment/nginx92%/25%1542m2s
nginxdeployment/nginx66%/25%1552m32s
nginxdeployment/nginx57%/25%1552m41s
nginxdeployment/nginx27%/25%1553m11s
nginxdeployment/nginx23%/25%1553m41s
nginxdeployment/nginx0%/25%1544m23s
nginxdeployment/nginx0%/25%1525m53s
nginxdeployment/nginx0%/25%1516m30s

Likewise, we will see the replicas of the Deployment changing when the HorizontalPodAutoscaler resource actions the changes:

$ kubectl get deployment nginx -w 
NAMEREADYUP-TO-DATEAVAILABLEAGE
nginx1/11118s
nginx1/21177s
nginx2/22279s
nginx2/422107s
nginx3/443108s
nginx4/444109s
nginx4/5442m17s
nginx5/5552m19s
nginx4/4444m23s
nginx2/2225m53s
nginx1/1116m30s

Besides CPU and memory, you can use other parameters to scale your workloads, such as network

traffic. You can also use external metrics such as latency and other factors that you can use to decide when to scale your traffic.

Tip

While you should use the HorizontalPodAutoscaler resource with CPU and memory, you should also consider scaling on external metrics such as response time and network latency. That will ensure better reliability as they directly impact customer experience and are crucial to your business.

Till now, we have been dealing with stateless workloads. However, pragmatically speaking, some applications need to save the state. Let’s look at some considerations for managing stateful applications.

Related Posts

Static provisioning – Managing Advanced Kubernetes Resources-2

As the Service resource is created, we can create a StatefulSet resource that uses the created PersistentVolume and Service resources. The StatefulSet resource manifest, nginx-manual-statefulset.yaml, looks like…

Static provisioning – Managing Advanced Kubernetes Resources-1

Static provisioning is the traditional method of provisioning volumes. It requires someone (typically an administrator) to manually provision a disk and create a PersistentVolume resource using the…

StatefulSet resources – Managing Advanced Kubernetes Resources

StatefulSet resources help manage stateful applications. They are similar to Deployment resources, but unlike a Deployment resource, they also keep track of state and require Volume and…

Managing stateful applications – Managing Advanced Kubernetes Resources

Imagine you’re a librarian in a magical library. You have a bunch of enchanted books that store valuable knowledge. Each book has a unique story and is…

Horizontal Pod autoscaling – Managing Advanced Kubernetes Resources-1

Imagine you’re the manager of a snack bar at a park. On a sunny day, lots of people come to enjoy the park, and they all want…

Name-based routing – Managing Advanced Kubernetes Resources

Name-based or FQDN-based routing relies on the host header we pass while making an HTTP request. The Ingress resource can route based on the header. For example,…

Leave a Reply

Your email address will not be published. Required fields are marked *