spring-boot - Kubernetes 在单独的端口上使用 Spring Boot 应用程序进行事件/就绪检查

标签 spring-boot kubernetes spring-boot-actuator kubernetes-health-check

我在 kubernetes 中部署了一个 spring-boot 应用程序,并在端口 8081 上使用执行器。主应用程序在端口 8080 上运行。当我描述该实例时,检查失败:

$ kubectl describe pod app-844d96f469-8vkbn
...
  Warning  Unhealthy  29s (x4 over 48s)  kubelet            Readiness probe failed: Get "http://192.168.13.189:8081/actuator/health/readiness": dial tcp 192.168.13.189:8081: connect: connection refused
  Warning  Unhealthy  29s (x2 over 39s)  kubelet            Liveness probe failed: Get "http://192.168.13.189:8081/actuator/health/liveness": dial tcp 192.168.13.189:8081: connect: connection refused

当我 ssh 进入 pod 时,检查显示应用程序在本地主机上以及错误消息中的 IP 地址上都运行正常:

$ kubectl exec -it pod/app-844d96f469-8vkbn -- /bin/sh
# curl localhost:8081/actuator/health/liveness
{"status":"UP"}
# curl localhost:8081/actuator/health/readiness
{"status":"UP"}
# curl 192.168.13.189:8081/actuator/health/liveness
{"status":"UP"}
# curl 192.168.13.189:8081/actuator/health/readiness
{"status":"UP"}

应用程序在 10*10=100 秒窗口内运行正常,因此这不仅仅是调整阈值和周期的问题。

这是用于部署检查的 kubernetes 配置:

        livenessProbe:
          httpGet:
            path: "/actuator/health/liveness"
            port: 8081
          failureThreshold: 10
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: "/actuator/health/readiness"
            port: 8081
          failureThreshold: 10
          periodSeconds: 10

非常感谢有关此问题的任何帮助!


编辑:添加 Dockerfile,因为 Thomas 询问图像上是否暴露了 8081。我不需要做任何事情就可以暴露端口 8080,但也许问题与 Dockerfile 无关。

FROM eclipse-temurin:17.0.5_8-jre
RUN mkdir /opt/app
ARG JAR_FILE
COPY ${JAR_FILE} /opt/app/app.jar
ENTRYPOINT ["java","-jar","/opt/app/app.jar"]

最佳答案

我认为您的应用程序在由于 active 探测而失败之前需要很长时间才能准备好。 Spring boot/Java应用程序在启动时会进行大量初始化,当CPU有限时,它会很慢并且需要很长时间。

这里有一个很好的解释: https://faun.pub/java-application-optimization-on-kubernetes-on-the-example-of-a-spring-boot-microservice-cf3737a2219c

一个简单的服务在 0.5 个 CPU 上需要 80 秒,而在 3 个 CPU 上只需要 12 秒。

如果您设置了 CPU 限制,请将其忽略。只提出一个请求。 也许还可以为 active 探针配置 initialDelaySeconds: 60 左右。

关于spring-boot - Kubernetes 在单独的端口上使用 Spring Boot 应用程序进行事件/就绪检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74428291/

相关文章:

spring - Spring Boot 中何时需要 Thymeleaf 表达式分隔符?

java - 无法使用 Spring RestTemplate 使用 JSON 数组

kubernetes - 通过自己的服务从pod请求到localhost无效

kubernetes - 如何在命令行中启动 pod,而不在 kubernetes 中部署?

kubernetes - Liveness Probes是否重新启动或杀死Pod?

arrays - 如何在 Hibernate 中使用 Postgres 数组 "contains"条件?

java - 无法将 Spring Boot Admin Server 2 和 Spring Boot Admin Client 2 与 Spring Boot 2 集成在单个应用程序中

spring-boot - Spring Boot 2 : Refresh properties on the fly not working

java - Spring Boot Actuator/health 端点不显示数据库或文件系统信息

spring-boot - Hystrix 的健康端点不可用