Setting up the Vertical Pod Autoscaler

Kubernetes vpa Pod cluster autoscaler Karpenter

3 min read | by Jordi Prats

To be able take advantage of using a Cluster Autoscaler (same applies to AWS Karpenter) we need make sure we properly set the resources any scheduled Pod is requesting to Kubernetes:

  • If we are requesting too much resources, we will be wasting resources
  • If we are requesting too few, the application might end up on a node where it needs to constantly share resources with other Pods

When we are not use the resources a given Pod or container is going to use, we can use the Vertical Pod Autoscaler to help us define them

The Vertical Pod Autoscaler can be found as part of the kubernetes autoscaler git repository. We can choose to install it using the script it provides of use one of the available helm charts, for example cowboysysop's helm. We can install it like so:

$ helm repo add cowboysysop https://cowboysysop.github.io/charts/
$ helm install my-release cowboysysop/vertical-pod-autoscaler

Once installed, we will be able to create a VerticalPodAutoscaler object that will define which mode the VPA operates in:

  • The Auto and Recreate modes automatically apply the CPU and memory recommendations throughout the pod lifetime, deleting any pods that are out of alignment with its recommendations.
  • The Initial mode, applies VPA recommendations only at pod creation.
  • The Off mode only analyzes the Pods to calculate it's recommendations, but we will have to manually update it's definitions

A VerticalPodAutoscaler object that is configured to Off would look like this:

apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: pet2cattle-webserver
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind:       Deployment
    name:       pet2cattle-webserver
  updatePolicy:
    updateMode: "Off"

Once it's been applied to the system and have been able to collect metrics to make it's recommendations, we can retrieve them using kubectl describe vpa:

$ kubectl get vpa
NAME                   MODE   CPU   MEM        PROVIDED   AGE
pet2cattle-webserver   Off    11m   26214400   True       107s
$ kubectl describe vpa pet2cattle-webserver
Name:         pet2cattle-webserver
Namespace:    pet2cattle
Labels:       <none>
Annotations:  <none>
API Version:  autoscaling.k8s.io/v1
Kind:         VerticalPodAutoscaler
Metadata:
  (...)
Spec:
  Target Ref:
    API Version:  apps/v1
    Kind:         Deployment
    Name:         pet2cattle-webserver
  Update Policy:
    Update Mode:  Off
Status:
  Conditions:
    Last Transition Time:  2022-02-07T11:23:51Z
    Status:                True
    Type:                  RecommendationProvided
  Recommendation:
    Container Recommendations:
      Container Name:  webapp
      Lower Bound:
        Cpu:     10m
        Memory:  26214400
      Target:
        Cpu:     11m
        Memory:  26214400
      Uncapped Target:
        Cpu:     11m
        Memory:  26214400
      Upper Bound:
        Cpu:           541m
        Memory:        1160861333
      Container Name:  socat
      Lower Bound:
        Cpu:     10m
        Memory:  26214400
      Target:
        Cpu:     11m
        Memory:  26214400
      Uncapped Target:
        Cpu:     11m
        Memory:  26214400
      Upper Bound:
        Cpu:           541m
        Memory:        566273869
      Container Name:  git-sync
      Lower Bound:
        Cpu:     10m
        Memory:  26214400
      Target:
        Cpu:     11m
        Memory:  26214400
      Uncapped Target:
        Cpu:     11m
        Memory:  26214400
      Upper Bound:
        Cpu:           541m
        Memory:        566273869
      Container Name:  static
      Lower Bound:
        Cpu:     203m
        Memory:  1967849540
      Target:
        Cpu:     671m
        Memory:  2162292018
      Uncapped Target:
        Cpu:     671m
        Memory:  2162292018
      Upper Bound:
        Cpu:     37571m
        Memory:  106473866755
Events:          <none>

If we are using the Auto mode but want to skip some of the containers, we can add a containerPolicies to skip it as follows: Exempting containers from applying VPA recommendations

apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: pet2cattle-webserver
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind:       Deployment
    name:       pet2cattle-webserver
  updatePolicy:
    updateMode: "Auto" 
  resourcePolicy: 
    containerPolicies:
    - containerName: my-opt-sidecar
      mode: "Off"

Posted on 14/02/2022