A post-start hook runs when a Kubernetes container starts. It is useful for initialization or warm-up that should happen at container start without changing the application code or container image. The hook is configured on an individual regular container, not on an init container. section 6.3 section 6.3.1
Set `lifecycle.postStart` on the container and choose a handler. `exec` runs a command inside the container; `httpGet` sends an HTTP GET request to the application. Lifecycle hooks do not support the `tcpSocket` handler available to probes. section 6.3 section 6.3.1
Generate a quote when Nginx starts
An `nginx:alpine` container can use a post-start `exec` command to install `fortune`, download quote files, and write a generated quote to `/usr/share/nginx/html/quote`. To check the result locally, run `kubectl port-forward quote-poststart 1080:80`, then `curl localhost:1080/quote`. Using local port 1080 also avoids the permission error shown when trying to listen on port 80. section 6.3.1
Listing 6.3 Pod with a post-start lifecycle hook
apiVersion: v1
kind: Pod
metadata:
name: quote-poststart #1
spec:
containers:
- name: nginx #2
image: nginx:alpine #2
ports: #3
- name: http #3
containerPort: 80 #3
lifecycle: #4
postStart: #4
exec: #4
command: #4
- sh #5
- -c #6
- | #7
apk add fortune && \ #8
curl -O https://luksa.github.io/kiada/book-quotes.txt && \ #8
curl -O https://luksa.github.io/kiada/book-quotes.txt.dat && \ #8
fortune book-quotes.txt > /usr/share/nginx/html/quote #8
Complete Pod manifest showing where the `postStart.exec.command` configuration belongs. section 6.3.1
The hook runs alongside the container’s main process, not before it. A slow hook can therefore leave that process running while the hook is still executing. The Kubelet currently waits for one container’s post-start handler before creating the next container listed in the Pod spec; that ordering behavior is an implementation detail that may change. A failed hook can be inspected through its `PostStartHookError` status and `FailedPostStartHook` event. section 6.3.1 section 6.4.2
