Customize nginx Startup Behavior with a postStart Lifecycle Hook
Scenario
A web server Pod needs its homepage customised at startup time before it begins serving traffic, using a Kubernetes lifecycle hook.
A NodePort Service (hook-demo-svc) has already been created in the web-hooks namespace on port 30088. It selects Pods with the label app: hook-demo. Your job is to create the Pod that the Service will route traffic to, and prove the hook ran correctly.
Task
Create a Pod named hook-demo in the web-hooks namespace.
The Pod label app: hook-demo is required because the existing Service hook-demo-svc uses it as its selector. Without this label, the Service will not be able to route traffic to the Pod.
Use a container named web with the nginx image, and configure a postStart exec lifecycle hook that runs:
/bin/sh -c 'echo "Hello from the postStart handler" > /usr/share/nginx/html/index.html'

Kubernetes starts the container, runs the postStart hook, and exposes the healthy app to users.
Once the Pod is Running, verify the hook wrote the custom message:
curl http://cplane-01:30088/ #Expected output: Hello from the postStart handler
# Or exec directly into the container
kubectl exec hook-demo -n web-hooks -- cat /usr/share/nginx/html/index.html
Hint
A postStart hook fires immediately after a container is created, before the container's main process is confirmed to be running. Kubernetes blocks the container from reaching Running state until the hook completes.
containers:
- lifecycle:
postStart:
exec:
command:
Key points:
postStartruns concurrently with the container entrypoint; nginx may not be fully up yet, but file writes succeed immediately.- If the hook exits non-zero, Kubernetes kills and restarts the container.
Useful references:
Container Lifecycle Hooks | Attach Handlers to Container Events