How to Auto-Scale Todo Apps on Kubernetes for Better Performance
This blog will show how to auto-scale the todo application in Kubernetes.
Pre-requisite
Kindly refer to the following blogs, where we deploy the Todo application in the Kubernetes cluster using CRD, custom controller, init container, probes, resources limit, etc.
Blog: https://minex.hashnode.dev/how-to-deploy-todo-apps-with-kubernetes-a-step-by-step-guide
About Auto-scaling
Here, we will use Horizontal pod autoscaling for our application. Whenever the application usage or load increases, it auto-scales the application, which means it spins up more pods to maintain the resource utilization that we have defined in the manifest file.
They use metrics to observe the resource utilization of Pod, and the HPA controller will scale the target up or down.
Install metrics server
We have to use a metrics server to check and monitor the pod utilization.
For minikube cluster:minikube addons enable metrics-server
For self-managed cluster:kubectl apply -f
https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
How to do Load Testing?
To demonstrate the Horizontal Pod Autoscaling (HPA), we have to create the testing setup which will increase the load on the application and ultimately scale-ups.
Here, we are going to use Grafana K6 for load testing.
Install K6 on the server
gpg -k
gpg --no-default-keyring --keyring /usr/share/keyrings/k6-archive-keyring.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69
echo "deb [signed-by=/usr/share/keyrings/k6-archive-keyring.gpg] https://dl.k6.io/deb stable main" | tee /etc/apt/sources.list.d/k6.list
apt-get update
apt-get install k6
Application setup
Deploy the Todo application to do the further testing:
k8s-manifests/ ├── app │ ├── appDeployment.yaml │ └── appService.yaml └── database ├── postgresCluster.yaml ├── postgresConfigMap.yaml ├── postgresInitConfigMap.yaml └── postgresSecret.yaml
Refer to the above steps to deploy all the required objects
https://minex.hashnode.dev/how-to-deploy-todo-apps-with-kubernetes-a-step-by-step-guide#heading-final-deploymentDeploy HPA manifest:
k8s-manifests/ └── auto-scaling └── horizontalPodAutoScaler.yaml
kubectl apply -f horizontalPodAutoScaler.yaml
Verify:
kubectl get hpa
Once, the HPA is deployed, then we can move for load testing.
Increase the load on the application:
tests/ └── load-testing.js
k6 run -e BASE_URL=
http://192.168.59.100:30008/
load-testing.js
Here, BASE_URL is the variable, which we use to pass the application URL.load-testing.js, the script that hits the endpoint.
Now, you will see that the pod has scaled, and the pod replication has increased.kubectl get pods
Access application
To access the application whether it's working seamlessly or not, use the following link for the steps.
Link: https://minex.hashnode.dev/how-to-deploy-todo-apps-with-kubernetes-a-step-by-step-guide#heading-access-application