0
0
Kubernetesdevops~5 mins

Vertical Pod Autoscaler concept in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your app needs more or less CPU and memory depending on how busy it is. Vertical Pod Autoscaler helps by automatically changing these resources for your app's pods so they run smoothly without wasting resources.
When your app's CPU or memory needs change over time and you want Kubernetes to adjust resources automatically.
When you notice pods crashing or slowing down because they don't have enough memory or CPU.
When you want to save money by not allocating too many resources to your pods all the time.
When you want to improve app performance by giving pods just the right amount of resources.
When you want to avoid manual updates to pod resource requests and limits as workload changes.
Config File - vpa.yaml
vpa.yaml
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: my-app-vpa
  namespace: default
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: my-app
  updatePolicy:
    updateMode: "Auto"

This file tells Kubernetes to watch the Deployment named my-app in the default namespace.

The updateMode: Auto means Kubernetes will automatically adjust CPU and memory requests for pods in this deployment based on usage.

Commands
This command creates the Vertical Pod Autoscaler resource in Kubernetes to start monitoring and adjusting resources for the my-app deployment.
Terminal
kubectl apply -f vpa.yaml
Expected OutputExpected
verticalpodautoscaler.autoscaling.k8s.io/my-app-vpa created
This command shows the current state and recommendations of the Vertical Pod Autoscaler for the my-app deployment.
Terminal
kubectl get vpa my-app-vpa -o yaml
Expected OutputExpected
apiVersion: autoscaling.k8s.io/v1 kind: VerticalPodAutoscaler metadata: name: my-app-vpa namespace: default spec: targetRef: apiVersion: apps/v1 kind: Deployment name: my-app updatePolicy: updateMode: Auto status: recommendation: containerRecommendations: - containerName: my-app-container lowerBound: cpu: 100m memory: 128Mi target: cpu: 200m memory: 256Mi upperBound: cpu: 500m memory: 512Mi
-o yaml - Shows detailed information in YAML format
This command lists the pods of the my-app deployment to check their status after VPA adjusts resources.
Terminal
kubectl get pods -l app=my-app
Expected OutputExpected
NAME READY STATUS RESTARTS AGE my-app-5d7f9f7d9b-abc12 1/1 Running 0 3m
-l app=my-app - Filters pods by label app=my-app
Key Concept

If you remember nothing else from this pattern, remember: Vertical Pod Autoscaler automatically adjusts pod CPU and memory requests to match real usage, improving app stability and resource efficiency.

Common Mistakes
Not creating the Vertical Pod Autoscaler resource before expecting resource adjustments.
Without the VPA resource, Kubernetes does not know to monitor or adjust pod resources automatically.
Always apply the VPA configuration file with kubectl before expecting autoscaling to work.
Setting updateMode to "Off" or "Initial" and expecting automatic ongoing adjustments.
"Off" disables updates and "Initial" only sets resources at pod creation, so no continuous adjustment happens.
Use updateMode: "Auto" for continuous automatic resource updates.
Not labeling pods or deployments properly and then failing to see pods listed with kubectl get pods -l.
Without correct labels, filtering pods by label does not work and you cannot verify pod status easily.
Ensure your deployment and pods have consistent labels like app=my-app for easy management.
Summary
Create a Vertical Pod Autoscaler resource to monitor and adjust pod CPU and memory automatically.
Use kubectl apply to deploy the VPA configuration and kubectl get to check its recommendations and pod status.
Set updateMode to Auto for continuous automatic resource adjustments based on real usage.