意外关闭Pod时运行退出逻辑的最佳方法

问题描述 投票:0回答:1

我正在开发一个Spring Boot应用程序,我们通过kubernetes进行部署。我的要求是运行一些逻辑,以防容器崩溃或容器被卸下或容器被有意关闭。目前,我正在使用@ PreDestroy在退出时运行我的逻辑。

@Component
public class EngineShutDownHook{

private static final Logger LOGGER = LoggerFactory.getLogger(EngineShutDownHook.class);

@PreDestroy
public void onExit() {
    LOGGER.info("Shutting engine.");
    System.out.println(" engine is stopping.");
}
}

但是我不确定此代码是否将在所有可能的退出方案中运行。我还了解了spring的ExitCodeGenerator。您能否建议实现这一目标的最佳方法?

java spring-boot kubernetes shutdown shutdown-hook
1个回答
1
投票

使用K8的Container Lifecycle Hooks

PreStop:由于API请求或管理事件(例如,活动探测失败,抢占,资源争用等),在终止容器之前立即调用此挂钩。

© www.soinside.com 2019 - 2024. All rights reserved.