kubernetes vpa for CronJob

2/8/2021

I need to run VPA for CronJob. I refer to this doc. I think i followed it properly but it doesn't work for me.

  • using GKE, 1.17
  • VPA version is vpa-release-0.8
  • I created CronJob and VPA with this file.
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: hello
        spec:          
          containers:
          - name: hello
            image: busybox
            imagePullPolicy: IfNotPresent
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: my-vpa
spec:
  targetRef:
    apiVersion: "batch/v1beta1"
    kind: CronJob
    name: hello
  updatePolicy:
    updateMode: "Auto"

When I type this command:

kubectl describe vpa

I got this result:

Name:         my-vpa
Namespace:    default
Labels:       <none>
Annotations:  <none>
API Version:  autoscaling.k8s.io/v1
Kind:         VerticalPodAutoscaler
Metadata:
  Creation Timestamp:  2021-02-08T07:38:23Z
  Generation:          2
  Resource Version:    3762
  Self Link:           /apis/autoscaling.k8s.io/v1/namespaces/default/verticalpodautoscalers/my-vpa
  UID:                 07803254-c549-4568-a062-144c570a8d41
Spec:
  Target Ref:
    API Version:  batch/v1beta1
    Kind:         CronJob
    Name:         hello
  Update Policy:
    Update Mode:  Auto
Status:
  Conditions:
    Last Transition Time:  2021-02-08T07:39:14Z
    Status:                False
    Type:                  RecommendationProvided
  Recommendation:
Events:  <none>
-- 변상현
autoscaling
kubernetes
kubernetes-cronjob

1 Answer

5/22/2021

@mario oh!! so there was not enough time to get metrics to recommend resource.... – 변상현 Feb 10 at 2:36

Yes, exactly. If the only task of your CronJob is to echo Hello from the Kubernetes cluster and exit you won't get any recommendations from VPA as this is not a resource-intensive task.

However if you modify your command so that it generates an artificial load in your CronJob-managed pods:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: hello
        spec:
          containers:
          - name: hello
            image: busybox
            imagePullPolicy: IfNotPresent
            args:
            - /bin/sh
            - -c
            - date; dd if=/dev/urandom | gzip -9 >> /dev/null
          restartPolicy: OnFailure

after a few minutes you'll get the expected result:

$ kubectl describe vpa my-vpa
Name:         my-vpa
Namespace:    default
Labels:       <none>
Annotations:  <none>
API Version:  autoscaling.k8s.io/v1
Kind:         VerticalPodAutoscaler
Metadata:
  Creation Timestamp:  2021-05-22T13:02:27Z
  Generation:          8

...

    Manager:         vpa-recommender
    Operation:       Update
    Time:            2021-05-22T13:29:40Z
  Resource Version:  5534471
  Self Link:         /apis/autoscaling.k8s.io/v1/namespaces/default/verticalpodautoscalers/my-vpa
  UID:               e37abd79-296d-4f72-8bd5-f2409457e9ff
Spec:
  Target Ref:
    API Version:  batch/v1beta1
    Kind:         CronJob
    Name:         hello
  Update Policy:
    Update Mode:  Auto
Status:
  Conditions:
    Last Transition Time:  2021-05-22T13:39:40Z
    Status:                False
    Type:                  LowConfidence
    Last Transition Time:  2021-05-22T13:29:40Z
    Status:                True
    Type:                  RecommendationProvided
  Recommendation:
    Container Recommendations:
      Container Name:  hello
      Lower Bound:
        Cpu:     1185m
        Memory:  2097152
      Target:
        Cpu:     1375m
        Memory:  2097152
      Uncapped Target:
        Cpu:     1375m
        Memory:  2097152
      Upper Bound:
        Cpu:     96655m
        Memory:  115343360
Events:          <none>

Important: Just don't leave it running for too long as you might be quite surprised with your bill 😉

-- mario
Source: StackOverflow