Kubernetes 未知字段 "behavior"

Kubernetes unknown field "behavior"

我正在 Kubernetes 中创建一个 Horizo​​ntalPodAutoscaler,我需要将缩减稳定 window 配置为小于默认值。使用的代码和错误如下:

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
    name: busy-autoscaler
spec:
    behavior:
        scaleDown:
            stabilizationWindowSeconds: 10
    scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: busy-worker
    minReplicas: 1
    maxReplicas: 2
    metrics:
        - type: Resource
          resource:
              name: cpu
              target:
                  type: Utilization
                  averageUtilization: 50
$ kubectl create -f some-autoscale.yaml
error validating "some-autoscale.yaml": error validating data: ValidationError(HorizontalPodAutoscaler.spec): unknown field "behavior" in io.k8s.api.autoscaling.v2beta2.HorizontalPodAutoscalerSpec

我的理解是 behavior 字段应该从 Kubernetes 1.17 开始支持,如 docs 中所述。 运行 kubectl version 给出以下输出:

Client Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.1", GitCommit:"d224476cd0730baca2b6e357d144171ed74192d6", GitTreeState:"clean", BuildDate:"2020-01-14T21:04:32Z", GoVersion:"go1.13.5", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.0", GitCommit:"70132b0f130acc0bed193d9ba59dd186f0e634cf", GitTreeState:"clean", BuildDate:"2019-12-07T21:12:17Z", GoVersion:"go1.13.4", Compiler:"gc", Platform:"linux/amd64"}

API reference 没有 v2beta2behavior 字段,这让这更令人困惑。

我在本地 运行 Minikube 1.6.2。我做错了什么?

您对文档的阅读有误。没有像 behavior 这样的对象,因此出现了为什么应用 yaml 的错误,这就是 Api reference.

中缺少它的原因

Here 您有关于用于缩放的算法的详细信息。

但我想你正在考虑这个Support for cooldown/delay

--horizontal-pod-autoscaler-downscale-stabilization: The value for this option is a duration that specifies how long the autoscaler has to wait before another downscale operation can be performed after the current one has completed. The default value is 5 minutes (5m0s).

Note: When tuning these parameter values, a cluster operator should be aware of the possible consequences. If the delay (cooldown) value is set too long, there could be complaints that the Horizontal Pod Autoscaler is not responsive to workload changes. However, if the delay value is set too short, the scale of the replicas set may keep thrashing as usual.

看来这是一个文档不正确的案例,在我提出问题后不久就得到了更正。 PR #18157 on kubernetes/website adds the following text to the page on the Horizontal Pod Autoscaler.

Starting from v1.17 the downscale stabilization window can be set on a per-HPA basis by setting the behavior.scaleDown.stabilizationWindowSeconds field in the v2beta2 API. See Support for configurable scaling behavior.

PR #18965 恢复之前的拉取请求,因为 behavior 对象是 1.18 而非 1.17 中的目标功能。

目前,解决方案是使用控制器管理器上的 --horizontal-pod-autoscaler-downscale-stabilization 标志,如上面@ShantyMan 的回答中所述,这将为每个 HPA 设置值。

Doc 已将其更新为 v1.18 开始行为字段

支持可配置的缩放行为 从 v1.18 开始,v2beta2 API 允许通过 HPA 行为字段配置缩放行为。在 behavior 字段下的 scaleUp 或 scaleDown 部分单独指定行为以用于放大和缩小。可以为两个方向指定稳定性 window,以防止缩放目标中的副本数量发生波动。类似地,指定缩放策略控制缩放时副本的变化率。

客户端版本:v1.20.2 服务器版本:v1.18.9-eks-d1db3c

确保 kubectl api-versions 并且您的集群支持 autoscaling/v2beta2

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: {{ template "ks.fullname" . }}-keycloak
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ template "ks.fullname" . }}-keycloak
  minReplicas: {{ .Values.keycloak.hpa.minpods }}
  maxReplicas: {{ .Values.keycloak.hpa.maxpods }}
  metrics:
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: {{ .Values.keycloak.hpa.memory.averageUtilization }}
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: {{ .Values.keycloak.hpa.cpu.averageUtilization }}
  behavior:
    scaleDown:
      stabilizationWindowSeconds: {{ .Values.keycloak.hpa.stabilizationWindowSeconds }}
      policies:
        - type: Pods
          value: 1
          periodSeconds: {{ .Values.keycloak.hpa.periodSeconds }}