Kubernetes AutoScaler - 我应该在哪里指定缩小和放大

Kubernetes AutoScaler - Where should I specify scaledown and scaleup

我找不到任何关于应该在 Kind: HorizontalPodAutoscaler 中指定行为部分的示例。

在文档中他们有这个 section 但我找不到任何例子说明它应该放在什么地方?

behavior:
  scaleDown:
    stabilizationWindowSeconds: 300
    policies:
    - type: Percent
      value: 100
      periodSeconds: 15
  scaleUp:
    stabilizationWindowSeconds: 0
    policies:
    - type: Percent
      value: 100
      periodSeconds: 15
    - type: Pods
      value: 4
      periodSeconds: 15
    selectPolicy: Max

这是一个没有行为部分的示例auto-scaler.yml

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: nginx
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50
  - type: Resource
    resource:
      name: memory
      target:
        type: AverageValue
        averageValue: 100Mi

放在规范下

One or more scaling policies can be specified in the behavior section of the spec. https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#scaling-policies

具体讨论您的示例,您需要将 .behavior 定义部分粘贴到 .spec 下,如下所示:

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: nginx
spec:
  # <--- START ---> 
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
      - type: Percent
        value: 100
        periodSeconds: 15
    scaleUp:
      stabilizationWindowSeconds: 0
      policies:
      - type: Percent
        value: 100
        periodSeconds: 15
      - type: Pods
        value: 4
        periodSeconds: 15
      selectPolicy: Max
      # <--- END ---> 
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50
  - type: Resource
    resource:
      name: memory
      target:
        type: AverageValue
        averageValue: 100Mi

请记住,此功能从 Kubernetes v1.18 开始提供。

较早版本的 Kubernetes 会显示以下错误:

error: error validating "hpa.yaml": error validating data: ValidationError(HorizontalPodAutoscaler.spec): unknown field "behavior" in io.k8s.api.autoscaling.v2beta2.HorizontalPodAutoscalerSpec; if you choose to ignore these errors, turn validation off with --validate=false

关于旁注,您还可以查看:

  • $ kubectl autoscale
  • $ kubectl autoscale deployment nginx --min=1 --max=10 --cpu-percent=80 <- 示例

Creates an autoscaler that automatically chooses and sets the number of pods that run in a kubernetes cluster.


附加参考: