Container Lifecycle Hooks
Analogous to many programming language frameworks that have component lifecycle hooks, such as Angular, Kubernetes provides Containers with lifecycle hooks. The hooks enable Containers to be aware of events in their management lifecycle and run code implemented in a handler when the corresponding lifecycle hook is executed.
There are two hooks that are exposed to Containers:
This hook is executed immediately after a container is created. However, there is no guarantee that the hook will execute before the container ENTRYPOINT. No parameters are passed to the handler.
This hook is called immediately before a container is terminated due to an API request or management event such as a liveness/startup probe failure, preemption, resource contention and others. A call to the PreStop
hook fails if the container is already in a terminated or completed state and the hook must complete before the TERM signal to stop the container can be sent. The Pod’s termination grace period countdown begins before the PreStop
hook is executed, so regardless of the outcome of the handler, the container will eventually terminate within the Pod’s termination grace period. No parameters are passed to the handler.
A more detailed description of the termination behavior can be found in Termination of Pods.
Containers can access a hook by implementing and registering a handler for that hook. There are two types of hook handlers that can be implemented for Containers:
- HTTP - Executes an HTTP request against a specific endpoint on the Container.
When a Container lifecycle management hook is called, the Kubernetes management system executes the handler according to the hook action, httpGet
and tcpSocket
are executed by the kubelet process, and exec
is executed in the container.
PreStop
hooks are not executed asynchronously from the signal to stop the Container; the hook must complete its execution before the TERM signal can be sent. If a PreStop
hook hangs during execution, the Pod’s phase will be Terminating
and remain there until the Pod is killed after its terminationGracePeriodSeconds
expires. This grace period applies to the total time it takes for both the PreStop
hook to execute and for the Container to stop normally. If, for example, terminationGracePeriodSeconds
is 60, and the hook takes 55 seconds to complete, and the Container takes 10 seconds to stop normally after receiving the signal, then the Container will be killed before it can stop normally, since terminationGracePeriodSeconds
is less than the total time (55+10) it takes for these two things to happen.
If either a PostStart
or hook fails, it kills the Container.
Users should make their hook handlers as lightweight as possible. There are cases, however, when long running commands make sense, such as when saving state prior to stopping a Container.
Hook delivery is intended to be at least once, which means that a hook may be called multiple times for any given event, such as for PostStart
or PreStop
. It is up to the hook implementation to handle this correctly.
The logs for a Hook handler are not exposed in Pod events. If a handler fails for some reason, it broadcasts an event. For PostStart
, this is the FailedPostStartHook
event, and for PreStop
, this is the FailedPreStopHook
event. To generate a failed FailedPostStartHook
event yourself, modify the file to change the postStart command to “badcommand” and apply it. Here is some example output of the resulting events you see from running kubectl describe pod lifecycle-demo
:
- Learn more about the .
- Get hands-on experience attaching handlers to Container lifecycle events.