spring-boot - Kubernetes 自动缩放内存

标签 spring-boot docker kubernetes hpa

我有一个在 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/

相关文章:

mysql - 在数据库(mysql)的情况下,kubernetes 服务如何工作?

docker - 来自守护进程 : Cannot kill container: permission denied, 的错误响应如何在 Ubuntu 20.04 上杀死 docker 容器?

amazon-web-services - 将 Docker Compose 应用程序持续部署到 AWS/EC2

logging - 在 GCE 上获取 Kubernetes 错误和日志

bash - 服务器错误 (NotFound) : pods "\nmongo-client-79667cc85d-tsg72" not found

linux - 使用 linux .sh 脚本自动选择提示

java - Spring JPA : How to Model Percentage Value

spring-boot - 嵌套异常是 javax.management.InstanceAlreadyExistsException : com. zaxxer.hikari :name=dataSource, type=HikariDataSource

postgresql - 将 json 字符串从 spring 应用程序插入到 postgres jsonb

java - 如何创建可在整个应用程序中访问但同时请求特定的变量?