4.0 KiB
Kubernetes Lifecycle Hooks: postStart and preStop
Overview
In Kubernetes, lifecycle hooks allow you to run specific code when a container starts or stops. These hooks are part of a container’s lifecycle and are useful for performing setup and cleanup tasks.
The two main lifecycle hooks are:
postStart– Executed immediately after the container starts.preStop– Executed just before the container stops.
These hooks can be defined inside the container specification in a Pod manifest.
1. postStart Hook
Description
The postStart hook runs immediately after a container is created, but before it is marked as Running. It can be used to perform initialization tasks that need to occur right after startup.
The postStart hook supports two types of actions:
exec– Run a command inside the container.httpGet– Send an HTTP request to a specified endpoint inside the container.
Notes
- The
postStarthook and the container’s main process run concurrently. - If the hook fails, the container is considered to have failed and will be restarted according to its restart policy.
Example
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo Container started at $(date) >> /var/log/startup.log"]
Using an HTTP GET action:
lifecycle:
postStart:
httpGet:
path: /startup
port: 8080
Common Use Cases
- Initializing configuration files or environment data.
- Sending startup notifications to monitoring systems.
- Preparing application dependencies.
2. preStop Hook
Description
The preStop hook runs just before a container is terminated. Kubernetes will execute the hook and wait for it to complete before sending the SIGTERM signal to the container’s main process.
This hook is typically used to ensure a graceful shutdown of the application.
Like postStart, it supports:
exechttpGet
Notes
- The hook must complete within the time defined by
terminationGracePeriodSeconds. - If the hook does not finish within that time, the container will be forcibly terminated.
Example
lifecycle:
preStop:
exec:
command: ["/bin/sh", "-c", "echo Shutting down... >> /var/log/shutdown.log && sleep 5"]
Common Use Cases
- Closing active connections.
- Flushing logs or metrics.
- Deregistering the service from a load balancer or discovery system.
- Notifying external systems about the shutdown event.
3. Full Example
apiVersion: v1
kind: Pod
metadata:
name: lifecycle-demo
spec:
containers:
- name: demo-container
image: nginx
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo 'Container started!' > /usr/share/nginx/html/index.html"]
preStop:
exec:
command: ["/bin/sh", "-c", "echo 'Container stopping...' >> /usr/share/nginx/html/index.html && sleep 10"]
terminationGracePeriodSeconds: 15
4. Best Practices for DevOps Engineers
-
Handle Signals in Your Application Ensure your application properly handles
SIGTERMandSIGKILLfor clean shutdowns. -
Combine Hooks with Grace Periods Use
preStoptogether withterminationGracePeriodSecondsto allow sufficient cleanup time. -
Monitor Hook Execution Log hook outputs for debugging and verification.
-
Avoid Long-Running or Blocking Hooks Hooks should be lightweight to avoid delaying container lifecycle events.
5. Summary
| Hook Type | Trigger Time | Purpose | Common Actions |
|---|---|---|---|
postStart |
Immediately after container starts | Initialization | Load configs, warm cache, notify service |
preStop |
Before container termination | Graceful shutdown | Flush data, close connections, deregister service |