用于 CronJob 的 kubernetes vpa

问题描述

我需要为 CronJob 运行 VPA。我指的是this doc。 我认为我正确地遵循了它,但它对我不起作用。

  • 使用 GKE,1.17
  • VPA 版本为 vpa-release-0.8
  • 我用这个文件创建了 CronJob 和 VPA。
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"

当我输入这个命令时:

kubectl describe vpa

我得到了这个结果:

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>

解决方法

@马里奥哦!!所以没有足够的时间来获得推荐的指标 资源.... – 변상현 2 月 10 日 2:36

是的,没错。如果您的 CronJob 的唯一任务是 echo Hello from the Kubernetes cluster 并退出,您将不会从 VPA 获得任何建议,因为这不是一项资源密集型任务。

但是,如果您修改您的命令以使其在您的 CronJob 管理的 Pod 中生成人工负载

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

几分钟后,您将获得预期的结果:

$ 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>

重要提示:不要让它运行太久,因为你可能会对你的账单感到惊讶?