2. Scaling and Scheduling Pods in Kubernetes: All About VPA

2. Scaling and Scheduling Pods in Kubernetes: All About VPA

Vertical Pod Auto-Scaler (VPA)

As I explain in previous blog, when a pod with 100 MB of RAM experiences increased load beyond 100 MB, the Vertical Pod Autoscaler (VPA) will increase the size of the pod rather than creating multiple pods.

Steps to utilize the Vertical Pod Autoscaler (VPA) for pods in Kubernetes:

  1. Create a file named apache-vpa.yml for the Vertical Pod Autoscaler (VPA). This file is similar to the Horizontal Pod Autoscaler (HPA), but there are some differences you need to adjust.
apiVersion: autoscaling.k8s.io/v1
kind: VerticaltalPodAutoscaler
metadata:
  name: apacha-hpa
spec:
  targetRef: # HPA replica target to our deployment, that's why we add apiVersion, kind, name of deployment here.
    apiVersion: apps/v1
    kind: Deployment
    name: apache-deployment
  updatePolicy:
    updateMode: "Auto"   #For Automatic Scaling

Here in VPA, apiVersion should be autoscaling.k8s.io/v1 as mentioned into Kubernetes documentation.

  1. You can apply the changes
kubectl apply -f apache-vpa.yml
  1. It will not apply the changes because it is asking to install CRSs first before applying VPA changes.

CRDs (Custom Resource Definitions) are like plugins for Kubernetes. They let you add new types of resources that Kubernetes doesn’t support by default.

For example:

  • Kubernetes knows about Pods, Services, and Deployments.

  • But if you want to use a VerticalPodAutoscaler, you need to install its CRD first.

Think of CRDs as a way to teach Kubernetes about new things it can manage.

  1. Let's install the CRDs. You can find the installation steps in the repository, or follow the steps below:
cd ../../

git clone https://github.com/kubernetes/autoscaler.git

cd autoscaler/vertical-pod-autoscaler

./hack/vpa-up.sh   #This fill will install all the required things for VPA

Now, verify that the VPA pods are available in our kube-system.

kubectl get pods -n kube-system

So, do you get the idea now? Your cluster didn't know what VPA was.

  1. Now, try to install your apache-vpa.yml, and you will see it is created.
cd /kubestarter/HPA_VPA

kubectl apply -f apache-vpa.yml
  1. Check
kubectl get vpa

  1. You can see the CPU usage is 25 MB, and the application is running smoothly. So, let's try to generate load using BusyBox, as we did in the HPA blog.
kubectl run -i --tty load-generator --image=busybox /bin/sh

#Inside the container, use 'wget' to generate load:

while true; do wget -q -O- http://apache-service.default.svc.cluster.local; done
  1. Now that the load is being generated, you can monitor the VPA in another terminal. As the load increases, you will see how the CPU usage goes up and the memory increases automatically.
watch kubectl get vpa

You can see our application is taking lots of load.

This is how the Vertical Pod Autoscaler operates.


Happy Learning :)

Chetan Mohod ✨

For more DevOps updates, you can follow me on LinkedIn.