smruti ranjan
smruti ranjan

Reputation: 781

Best way to run exit logic on accidental pod shutdown

I am working on a spring boot application and we deploy through kubernetes. My requirement is to run some logic in case the pod crashes or pod gets removed or pod is intentionally shut down. Currently I am using @PreDestroy to run my logics on exit.

@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.");
}
}

However I am not sure whether this code will run on all possible exit scenarios. I have also learnt about spring's ExitCodeGenerator. Can you please suggest which is the best way to achieve this ?

Upvotes: 1

Views: 993

Answers (1)

Kushal Arya
Kushal Arya

Reputation: 364

Use Container Lifecycle Hooks of K8s

PreStop: This hook is called immediately before a container is terminated due to an API request or management event such as liveness probe failure, preemption, resource contention and others.

Upvotes: 1

Related Questions