简体   繁体   English

Spring Booot 应用程序在 Kube.netes 环境中启动后立即关闭

[英]Spring Booot Application gets shutdown immediately after start in Kubernetes env

I have one application which is running fine in local, dev env.我有一个在本地开发环境中运行良好的应用程序。 In dev it runs as an image in kube.netes in AWS cloud.在开发中,它在 AWS 云中的 kube.netes 中作为图像运行。 Upto this everything went fine.至此一切顺利。 Now I am trying to deploy the app in PP env.现在我正在尝试在 PP env 中部署该应用程序。 However in PP, the applications is starting and then immediately shutdown.但是在 PP 中,应用程序正在启动,然后立即关闭。

Here the configurations:这里的配置:

pom.xml pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>

        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xyz</groupId>
    <artifactId>my-app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>my-app</name>
    <description>My App System</description>
    <properties>
        <java.version>11</java.version>
        <skipTests>true</skipTests>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.awspring.cloud</groupId>
            <artifactId>spring-cloud-starter-aws-parameter-store-config</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.1.3</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-sts</artifactId>
            <version>1.12.263</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Main Class:总机 Class:

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
    
    @Bean
    public RestTemplate getRestTemplate() {     
        return new RestTemplate();
    }

}

Start up log:启动日志:

2022-11-30 13:10:53.112  INFO 1 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-11-30 13:11:03.604  WARN 1 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2022-11-30 13:11:11.305  INFO 1 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 16 endpoint(s) beneath base path '/actuator'
2022-11-30 13:11:11.816  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-11-30 13:11:12.110  INFO 1 --- [           main] c.a.f.MyApplication                      : Started MyApplication in 96.297 seconds (JVM running for 104.706)
2022-11-30 13:11:12.513  INFO 1 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2022-11-30 13:11:12.607  INFO 1 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2022-11-30 13:11:12.797  INFO 1 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

Please help me if you have any clue as where it may go wrong.如果您有任何线索,请帮助我 go 可能在哪里出错。

UPDATE :更新

DEBUG log:-调试日志:-

Application availability state ReadinessState changed to REFUSING_TRAFFIC


2022-11-30 14:45:14.420  INFO 1 --- [           main] c.a.f.MyApplication                      : Started FMyApplication in 98.611 seconds (JVM running for 107.522)
2022-11-30 14:45:14.512 DEBUG 1 --- [ionShutdownHook] o.s.b.a.ApplicationAvailabilityBean      : Application availability state ReadinessState changed to REFUSING_TRAFFIC
2022-11-30 14:45:14.514 DEBUG 1 --- [ionShutdownHook] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5733f295, started on Wed Nov 30 14:44:06 GMT 2022, parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@78ffe6dc
2022-11-30 14:45:14.516 DEBUG 1 --- [ionShutdownHook] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 2147483647
2022-11-30 14:45:14.601 DEBUG 1 --- [ionShutdownHook] o.s.c.support.DefaultLifecycleProcessor  : Bean 'webServerGracefulShutdown' completed its stop procedure
2022-11-30 14:45:14.601 DEBUG 1 --- [ionShutdownHook] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 2147483646
2022-11-30 14:45:14.616 DEBUG 1 --- [           main] o.s.b.a.ApplicationAvailabilityBean      : Application availability state LivenessState changed to CORRECT
2022-11-30 14:45:14.715 DEBUG 1 --- [           main] o.s.b.a.ApplicationAvailabilityBean      : Application availability state ReadinessState changed from REFUSING_TRAFFIC to ACCEPTING_TRAFFIC
2022-11-30 14:45:14.716 DEBUG 1 --- [ionShutdownHook] o.s.c.support.DefaultLifecycleProcessor  : Bean 'webServerStartStop' completed its stop procedure
2022-11-30 14:45:14.812 DEBUG 1 --- [ionShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2022-11-30 14:45:14.904  INFO 1 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2022-11-30 14:45:14.904 DEBUG 1 --- [ionShutdownHook] o.hibernate.internal.SessionFactoryImpl  : HHH000031: Closing
2022-11-30 14:45:14.904 DEBUG 1 --- [ionShutdownHook] o.h.type.spi.TypeConfiguration$Scope     : Un-scoping TypeConfiguration [org.hibernate.type.spi.TypeConfiguration$Scope@6534185f] from SessionFactory [org.hibernate.internal.SessionFactoryImpl@d499c13]
2022-11-30 14:45:14.904 DEBUG 1 --- [ionShutdownHook] o.h.s.i.AbstractServiceRegistryImpl      : Implicitly destroying ServiceRegistry on de-registration of all child ServiceRegistries
2022-11-30 14:45:14.915 DEBUG 1 --- [ionShutdownHook] o.h.b.r.i.BootstrapServiceRegistryImpl   : Implicitly destroying Boot-strap registry on de-registration of all child ServiceRegistries
2022-11-30 14:45:14.917  INFO 1 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2022-11-30 14:45:14.917 DEBUG 1 --- [ionShutdownHook] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Before shutdown stats (total=10, active=0, idle=10, waiting=0)
2022-11-30 14:45:15.009 DEBUG 1 --- [nnection closer] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@14a049f9: (connection evicted)
2022-11-30 14:45:15.011 DEBUG 1 --- [nnection closer] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@82010d7: (connection evicted)
2022-11-30 14:45:15.013 DEBUG 1 --- [nnection closer] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@47daf598: (connection evicted)
2022-11-30 14:45:15.018 DEBUG 1 --- [nnection closer] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@4ff4f31c: (connection evicted)
2022-11-30 14:45:15.021 DEBUG 1 --- [nnection closer] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@76d717a6: (connection evicted)
2022-11-30 14:45:15.104 DEBUG 1 --- [nnection closer] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@37134f98: (connection evicted)
2022-11-30 14:45:15.106 DEBUG 1 --- [nnection closer] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@27f50ad7: (connection evicted)
2022-11-30 14:45:15.109 DEBUG 1 --- [nnection closer] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@4ff16126: (connection evicted)
2022-11-30 14:45:15.111 DEBUG 1 --- [nnection closer] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@1d53559: (connection evicted)
2022-11-30 14:45:15.113 DEBUG 1 --- [nnection closer] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@67077c93: (connection evicted)
2022-11-30 14:45:15.201 DEBUG 1 --- [ionShutdownHook] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - After shutdown stats (total=0, active=0, idle=0, waiting=0)
2022-11-30 14:45:15.201  INFO 1 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

Here is the point:这是重点:

sometimes our apps may need a little bit of time before being able to accept requests.有时我们的应用程序可能需要一点时间才能接受请求。 The kubelet can make sure that the application receives requests only when it's ready. kubelet 可以确保应用程序仅在准备就绪时才接收请求。 Also, if the main process of a pod crashes for any reason, the kubelet will restart the container.此外,如果 pod 的主进程因任何原因崩溃,kubelet 将重新启动容器。

Reference: Spring-Boot-Liveness-And-Readiness参考: Spring-Boot-Liveness-And-Readiness

In order to fulfill these responsibilities, Kube.netes has two probes: liveness probes and readiness probes为了履行这些职责,Kube.netes 有两个探针:liveness 探针和 readiness 探针

In my case as soon as the app was starting the ReadinessState changed to REFUSING_TRAFFIC .在我的例子中,应用程序启动后ReadinessState立即更改为REFUSING_TRAFFIC Hence added some delay seconds to the property in kube.netes service config yml to give some time to the app to make ReadinessState changed from REFUSING_TRAFFIC to ACCEPTING_TRAFFIC因此,在 kube.netes 服务配置 yml 中的属性中添加了一些延迟秒数,以便为应用程序提供一些时间,使ReadinessStateREFUSING_TRAFFIC更改为ACCEPTING_TRAFFIC

In kustomization.yml file added the below 2 config properties which resolved the issue:在 kustomization.yml 文件中添加了以下 2 个解决问题的配置属性:

1. readinessProbe/initialDelaySeconds : 60
2. livenessProbe/initialDelaySeconds : 60

Also same can be added in application.yml file as below:同样可以添加到 application.yml 文件中,如下所示:

livenessProbe:
  httpGet:
    path: /actuator/health/liveness
    port: 8080
    initialDelaySeconds: 30
    periodSeconds: 30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 spring-boot应用程序启动后立即关闭(计划的) - spring-boot application shutdown immediately after starting (schedular) Spring 引导应用程序在启动后立即关闭 - Spring Boot application shutting down immediately after start 优雅地关闭在 Kubernetes 中运行的 Spring 应用程序 - Gracefully shutdown Spring application running in Kubernetes 应用程序关闭后删除了 Spring JPA 数据 - Spring JPA data removed after shutdown of application 启动后立即启动Spring启动应用程序 - Spring boot application shutdown immediate after starting Spring Batch 应用程序计划每分钟运行一次但立即退出? - Spring Batch application is scheduled to run every minute but gets exited immediately? 如何通过start-stop-daemon优雅地关闭Spring Boot应用程序 - How to gracefuly shutdown a Spring Boot application by start-stop-daemon 关闭终端后如何防止Spring MVC应用程序关闭? - How to prevent Spring MVC application to shutdown after closing the terminal? 为什么在创建 newSingleThreadScheduledExecutor 后会立即关闭? - Why would shutdown immediately after creating newSingleThreadScheduledExecutor? 为什么我的 Spring Boot App 总是在启动后立即关闭? - Why does my Spring Boot App always shutdown immediately after starting?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM