Skip to main content

Implementing Vertical Pod Autoscaling

1. Installing VPA Components

Example: Install VPA using Helm

helm repo add vpa https://charts.fairwinds.com/stable
helm install vpa vpa/vpa --namespace vpa-system --create-namespace
Component Description Installation Method Status
VPA Admission Controller Validates and modifies pod specs Helm Chart NEW
VPA Recommender Collects resource usage data and makes recommendations Helm Chart NEW
VPA Updater Applies recommended resource limits to pods Helm Chart NEW
Note: Ensure the namespace vpa-system exists before installing.
Warning: VPA requires cluster-admin permissions to function properly.

2. Creating VPA Resource

Example: Basic VPA Configuration

apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: vpa-example
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: my-app
  updatePolicy:
    updateMode: "Auto"
Field Description Values
apiVersion API version of the VPA resource autoscaling.k8s.io/v1
kind Kind of the resource VerticalPodAutoscaler
metadata.name Name of the VPA resource String
spec.targetRef Reference to the target resource (Deployment, StatefulSet, etc.) apiVersion, kind, name
spec.updatePolicy.updateMode How VPA updates the resource Off, Initial, Auto, Recreate
Note: The VPA resource must be in the same namespace as the target resource.
Warning: Ensure that the target resource has sufficient permissions to allow VPA to make changes.

3. Setting Update Mode

Mode Description Behavior Notes
Off Vertical Pod Autoscaler (VPA) does not make any changes to pod resource requests. No updates are made to pods. Useful for testing or when you want to observe VPA behavior without applying changes.
Initial VPA sets resource requests only on newly created pods. Existing pods are not modified; new pods get updated resource requests. Helps in avoiding disruption to running workloads while allowing new pods to benefit from optimized resources.
Recreate VPA updates resource requests and recreates pods to apply new settings. Pods are terminated and recreated with updated resource requests. Useful when you want to ensure all pods are running with optimized resources, but may cause downtime.
Auto VPA updates resource requests and attempts to update pods without recreating them. Pods may be updated in-place if possible, otherwise recreated. Best for minimizing disruption while still optimizing resource usage.

Example: Setting Update Mode in VPA Spec

apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: vpa-example
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: my-deployment
  updatePolicy:
    updateMode: "Recreate"

4. Configuring Resource Policy

Policy Description Values Notes
resourcePolicy Defines how the VPA should handle resource requests and limits minChangePercentage, maxChangePercentage Controls how aggressively VPA can change resources
minChangePercentage Minimum percentage change in resource allocation 10, 25 Prevents small, frequent adjustments
maxChangePercentage Maximum percentage change in resource allocation 50, 100 Prevents drastic changes that may destabilize pods

Example: VPA Resource Policy Configuration

apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: vpa-policy-example
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: my-app
  resourcePolicy:
    containerPolicies:
    - containerName: "app-container"
      minChangePercentage: 20
      maxChangePercentage: 50
Note: The resourcePolicy field is optional and defaults to no restrictions on changes. It's recommended to set minChangePercentage to prevent excessive updates.
Warning: Setting maxChangePercentage too high can lead to resource starvation or over-allocation. Ensure that changes align with your application's stability requirements.

5. Setting Min Allowed Resources

Resource Minimum Allowed Description
cpu 100m Minimum CPU allocation for the pod
memory 128Mi Minimum memory allocation for the pod

Example: VPA Configuration with Min Allowed Resources

apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: vpa-example
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: example-deployment
  resourcePolicy:
    containerPolicies:
    - containerName: "*"
      minAllowed:
        cpu: 100m
        memory: 128Mi

6. Setting Max Allowed Resources

Resource Property Value Type Description
maxAllowed cpu string Maximum CPU allocation allowed for a pod (e.g., 2, 2000m)
maxAllowed memory string Maximum memory allocation allowed for a pod (e.g., 4Gi, 4096Mi)

Example: Configuring Max Allowed Resources in VPA

apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: vpa-example
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: my-deployment
  resourcePolicy:
    containerPolicies:
    - containerName: "app"
      maxAllowed:
        cpu: "2"
        memory: "4Gi"
Note: Setting maxAllowed prevents VPA from requesting more resources than specified, ensuring resource limits are respected.

7. Viewing VPA Recommendations

Resource Recommendation Type Values Notes
vpa.name string The name of the VPA resource Used to identify the VPA in Kubernetes API
vpa.namespace string The namespace where VPA is defined Used to scope the VPA to a specific namespace
vpa.spec.targetRef object Reference to the target resource (Deployment, StatefulSet, etc.) Points to the workload that VPA will manage
vpa.status.conditions array List of conditions indicating VPA status Includes Ready, Initializing, and Recommending
vpa.status.recommendations object Recommended resource requests and limits Includes container, requests, and limits

Example: Viewing VPA Recommendations via kubectl

kubectl get vpa my-vpa -n my-namespace -o yaml

Example: Viewing VPA Status

kubectl describe vpa my-vpa -n my-namespace
Note: VPA recommendations are updated every few minutes based on historical usage patterns.
Warning: VPA does not recommend limits for containers; it only recommends requests.

8. Understanding VPA Components

Component Description Role
VPA Controller Core component that manages VPA objects and updates Pod resource requests Manages VPA lifecycle, calculates recommendations, and applies changes to Pods
VPA Recommender Component that analyzes historical resource usage and generates recommendations Collects metrics from Prometheus or Heapster, analyzes usage patterns, and provides resource suggestions
VPA Updater Component that modifies Pod resource requests based on VPA recommendations Applies updated resource limits and requests to existing Pods in the cluster

Example: VPA Controller Configuration

apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: vpa-example
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: my-app
  updatePolicy:
    updateMode: "Auto"
  resourcePolicy:
    containerPolicies:
    - containerName: "app"
      minAllowed:
        cpu: "100m"
        memory: "128Mi"
      maxAllowed:
        cpu: "2"
        memory: "1Gi"
Note: VPA components work together to provide intelligent resource scaling based on historical usage patterns.

9. Using VPA with HPA

Vertical Pod Autoscaler (VPA) vs Horizontal Pod Autoscaler (HPA)

  • VPA adjusts resource requests/limits of existing pods, while HPA scales the number of pods
  • VPA focuses on pod resource allocation; HPA focuses on pod count
  • Both can be used together for comprehensive autoscaling strategies
Feature VPA HPA
Resource Adjustment Modifies CPU/Memory requests/limits Adjusts pod replica count
Trigger Condition Based on resource usage (CPU/Mem) Based on metrics like CPU, memory, or custom
Pod Impact Updates existing pods Creates/destroys pods
Configuration Uses v1 API, VerticalPodAutoscaler kind Uses v2 API, HorizontalPodAutoscaler kind

Example: VPA Configuration

apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: vpa-example
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: my-app
  updatePolicy:
    updateMode: "Auto"

Example: HPA Configuration

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: hpa-example
spec:
  scaleTargetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: my-app
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70

Best Practices for Using VPA with HPA

  • Deploy both VPA and HPA in the same namespace for better visibility
  • Use VPA to optimize resource allocation and HPA to scale based on demand
  • Monitor both autoscalers for potential conflicts or over-optimization

10. Deleting VPA Resources

Resource Type Description Command
VerticalPodAutoscaler Represents the VPA resource to be deleted kubectl delete vpa <vpa-name>

Example: Deleting a VPA Resource

kubectl delete vpa my-app-vpa
Note: Ensure that the VPA resource is not in use by any pods before deletion to avoid unexpected behavior.

Was this article helpful?