A lifecycle hook is a per-container configuration that triggers an action at a defined point in the container’s life. It lets you add startup or shutdown behavior without changing an existing application or rebuilding its image. section 6.3 section 6.3.1
Kubernetes supports two lifecycle hooks:
- Post-start runs when the container starts. It can execute a command inside the container or send an HTTP GET request to the application, such as a warm-up request. section 6.3 section 6.3.1
- Pre-stop runs shortly before the container stops. Kubernetes invokes it before sending SIGTERM to the container’s main process. section 6.3 section 6.5
Hooks belong to regular containers, not init containers, and lifecycle hooks do not support tcpSocket handlers. A failing or slow hook affects the container lifecycle: shutdown still follows the termination grace period, after which the process can be killed. section 6.3 section 6.5
Generating a quote when an Nginx container starts
A post-start exec hook can install fortune, download quote data, and write a generated quote into Nginx’s web directory: lifecycle: postStart: exec: command: - sh - -c - | apk add fortune && \ curl -O https://luksa.github.io/kiada/book-quotes.txt && \ curl -O https://luksa.github.io/kiada/book-quotes.txt.dat && \ fortune book-quotes.txt > /usr/share/nginx/html/quote After exposing the pod locally with `kubectl port-forward quote-poststart 1080:80`, request the result with `curl localhost:1080/quote`. section 6.3.1
Figure 6.9 How the post-start and pre-stop hook fit into the container’s life cycle
Use the lifecycle diagram to see where post-start and pre-stop hooks occur relative to container startup and shutdown. section 6.3


