Kubernetes 自动缩放内存

Kubernates autoscale memory

我在 K8s 上有一个 java 应用程序 运行,部署中有 min:2 和 max:6 pods。 堆 min:256Mb,堆最大值:512Mb。请求和限制内存为 1Gi 这是 hpa 规范:

  spec:
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: my-app
  minReplicas: 2
  maxReplicas: 6
  metrics:
    - type: Resource
      resource:
        name: cpu
        targetAverageUtilization: 60
    - type: Resource
      resource:
        name: memory
        targetAverageUtilization: 60

在性能测试期间,我注意到部署正试图非常积极地向上扩展。

没有负载时,内存利用率在33%左右,据此link https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/ 粗略了解所需 pods 的公式是 desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]

从 K8s 监控中,我注意到当内存利用率增加到 40% 左右时,它会尝试向上扩展。如果我正确理解上面的公式是如何工作的,desiredReplicas = ceil[2*(0.4/0.6)] = 2,那么它不应该按比例放大。

我理解的对吗?

这看起来是正确的,但我猜测了一段时间,因为您没有分享 kubectl top pods 的输出。您的部署可能不是因为内存利用率而扩展,而是因为 CPU 首先是利用率。

如果您看到 docs 第一个达到目标的指标将启动自动缩放过程:

Kubernetes 1.6 adds support for scaling based on multiple metrics. You can use the autoscaling/v2beta2 API version to specify multiple metrics for the Horizontal Pod Autoscaler to scale on. Then, the Horizontal Pod Autoscaler controller will evaluate each metric, and propose a new scale based on that metric. The largest of the proposed scales will be used as the new scale

您也可以尝试使用内存目标的价值指标进行故障排除:

  metrics:
    - type: Resource
      resource:
        name: cpu
        targetAverageUtilization: 60
    - type: Resource
      resource:
        name: memory
        targetAverageValue: 700M

查看当前指标的一个好方法是只获取 HPA 上完整输出的状态:

$ kubectl get hpa <hpa-name> -o=yaml