如何将 Kubernetes Horizo​​ntalPodAutoscaler 与内存指标一起使用?

How to use the Kubernetes HorizontalPodAutoscaler with the memory metric?

我正在尝试了解 Kubernetes Horizo​​ntalPodAutoscaler 的工作原理。 到目前为止,我使用了以下配置:

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: my-deployment
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-deployment
  minReplicas: 1
  maxReplicas: 10
  targetCPUUtilizationPercentage: 50

这使用了 targetCPUUtilizationPercentage 参数,但我想使用一个指标来衡量所使用的内存百分比,但我找不到任何示例。 有什么提示吗?

我还发现有这种类型的配置可以支持多个指标,但是 apiVersionautoscaling/v2alpha1。这可以用于生产环境吗?

kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2alpha1
metadata:
  name: WebFrontend
spec:
  scaleTargetRef:
    kind: ReplicationController
    name: WebFrontend
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      targetAverageUtilization: 80
  - type: Object
    object:
      target:
        kind: Service
        name: Frontend
      metricName: hits-per-second
      targetValue: 1k

这是您需要的清单示例,其中包括 内存指标:

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

需要注意的重要一点是,如您所见,它使用 autoscaling/v2beta2 API 版本 ,因此您需要遵循所有先前的说明已列出 here

关于使用 autoscaling/v2alpha1 的可能性,是的,你可以使用它,因为它包括对内存扩展和自定义指标的支持,因为这个 URL 指定,但请记住,alpha 版本是为了测试而发布的,因为它们不是最终版本。

如需更多 autoscaling/v2beta2 YAML 的 示例以及对内存指标的更深入了解,您可以查看此