For this simple example, you need access to a Kubernetes installation. Anyone as described in INSTALL is good enough.
Our sample random-generator application provides a /shutdown
endpoint, which we use to log its call during the shutdown of a Pod.
But let’s create the Pod first:
kubectl create -f https://k8spatterns.io/ManagedLifecycle/pod.yml
And verify its status with
kubectl get pods -w
As you can see, the Pod only starts after 30s because we use a sleep 30
as a preStart hook (you can’t even check the logs before).
To watch the logs during the shutdown, we start a kubectl logs
in the background:
kubectl logs -f random-generator &
You can see the postStart
message, which has been picked up by a random generator during startup and copied to the standard output of the application container.
The environment variable WAIT_FOR_POST_START
that we set for our main application container indicates the random generator application to wait until the postStart
file has been created:
io.k8spatterns.examples.RandomGeneratorApplication - Waiting for postStart to be finished .... io.k8spatterns.examples.RandomGeneratorApplication - Waiting for postStart to be finished .... io.k8spatterns.examples.RandomGeneratorApplication - Waiting for postStart to be finished .... io.k8spatterns.examples.RandomGeneratorApplication - postStart Message: Wake up! ....
Now let’s kill the Pod:
kubectl delete pod random-generator
You should see in the logs messages like the following which indicate that our /shutdown
endpoint has been called indeed:
.... i.k.examples.RandomGeneratorApplication : SHUTDOWN NOW o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' i.k.examples.RandomGeneratorApplication : >>>> SHUTDOWN HOOK called. Possibly because of a SIGTERM from Kubernetes