我有一个在 K8s 上运行的 Java 应用程序,部署中有 min:2 和 max:6 pod。
堆最小值: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%左右,根据这个链接https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/
粗略了解所需 pod 的公式是
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
您还可以尝试为您的内存目标使用 Value 指标来进行故障排除:
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 60
- type: Resource
resource:
name: memory
targetAverageValue: 700M
查看当前指标的一个好方法是在 HPA 上获取完整输出的状态:$ kubectl get hpa <hpa-name> -o=yaml
关于spring-boot - Kubernetes 自动缩放内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62648847/