<p>I am using the Kubernetes cluster with docker. When I deploy the java services[springboot] some requests get dropped(for a couple of secs) with the following error.</p>
<p>exception=org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'controller': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!), stackTrace=[org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton() at line: 208]</p>
<p>I am already using livenessProbe &amp; readinessProbe.
Java Version: 12
SpringBoot Version: 2.1.5.RELEASE
Hibernate Version: 2.4.3 with Postgres DB</p>
<p>As per my knowledge, it is happening due to the closing of the application context while executing some requests. Ideally, it should not be.</p>
<p>can anyone help here ?</p>

Pankaj Garg
<p>the problem is not actually springboot, but rather the way Kubernetes stops pods.
at the moment when a pod from your old deployment/replicaset is being terminated (or rather actually set to state &quot;terminating&quot;), 2 things happen simultaneously:</p>
<p><strong>A)</strong> pod is removed from service endpoints, so it does no longer receive new requests</p>
<p><strong>B)</strong> pod container gets a SIGTERM, so apps can gracefully shutdown</p>
<p>so what you are seeing here is basically active requests that are being processed when the context gets shut down (as you already found out)</p>
<p>there are (at least) two solutions:</p>
<p><strong>1</strong> in kubernetes pod definition:</p>
<p>Kubernetes pods can be configured with a pre-stop hook that get executes a command in between A and B.
depending on your app, a simple &quot;sleep&quot; for a couple (milli)seconds should be sufficient, leaving the app enough time to finish the current requests before shutting down.
theres nice docu from google that goes more into detail:
<a href="https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-terminating-with-grace" rel="nofollow noreferrer">https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-terminating-with-grace</a></p>
<p><strong>2</strong> in SpringBoot:</p>
<p>you can make Java wait for finishing up running tasks when receiving the shutdown interrupt.
this is (imho) nicely explained here:
<a href="https://www.baeldung.com/spring-boot-graceful-shutdown" rel="nofollow noreferrer">https://www.baeldung.com/spring-boot-graceful-shutdown</a></p>
<p>Beware: kubernetes default graceful shutdown timeout is 30seconds, then the pod is forcefully removed. but as usual you can configure this timeout in <code>terminationGracePeriodSeconds</code> (also described in the google blog in (1)</p>
