Why Use PreStop Hooks?
When a pod is terminated, it often needs to handle tasks like closing database connections, saving in-memory data, or notifying other services about its shutdown. Without this, abrupt termination can lead to lost data, interrupted processes, or application errors.
PreStop hooks address this by giving containers a window of time to handle such tasks before they are stopped.
How PreStop Hooks Work
When Kubernetes decides to terminate a pod (due to scaling, updates, or other reasons), it sends a termination signal (SIGTERM
) to the container. The PreStop hook runs during this termination process, before the container shuts down.
Here’s the sequence:
- Kubernetes sends the termination signal to the pod.
- The PreStop hook executes its defined action.
- The container is terminated after the hook completes (or after the termination grace period).
How to Implement PreStop Hooks
PreStop hooks can be added to a pod’s lifecycle using a YAML configuration. You can define the hook to execute a command or call an HTTP endpoint.
Example 1: Running a Command
This configuration runs a shell command to perform cleanup tasks, such as notifying an external service about the shutdown.
apiVersion: v1
kind: Pod
metadata:
name: prestop-example
spec:
containers:
- name: app-container
image: my-app:latest
lifecycle:
preStop:
exec:
command: ["/bin/sh", "-c", "curl -X POST http://example.com/notify-shutdown"]
Example 2: Using an HTTP Request
If your application has an endpoint to handle shutdown logic, you can configure the PreStop hook to send an HTTP GET request to it.
preStop:
httpGet:
path: /shutdown
port: 8080
Use Cases for PreStop Hooks
1. Graceful Shutdown on Spot Instances
Spot instances can be terminated with little notice, making it critical to prepare for shutdowns.
- Example: Use a PreStop hook to notify an external load balancer to remove the pod from its traffic rotation or save in-memory data to persistent storage before termination.
2. Draining Connections
For web applications or services handling live traffic, PreStop hooks can notify upstream systems (like load balancers) to stop routing traffic to the pod.
- Example: A PreStop hook can deregister the pod from a service discovery system or signal the load balancer to re-route traffic.
3. Data Finalization
Applications often hold temporary or in-memory data that needs to be saved during shutdown.
- Example: A PreStop hook can trigger the application to save cached data to a database or storage system.
4. Releasing Locks or Resources
If your application uses distributed locks, they must be released before the pod shuts down to prevent resource contention.
- Example: Use a PreStop hook to release a lock in a distributed system like Redis or etcd.
5. Logging and Monitoring
PreStop hooks can be used to log shutdown events or update monitoring systems to reflect the pod’s status.
- Example: Send logs to an external service or update a monitoring dashboard.
Things to Consider
- Timeouts: PreStop hooks must complete within the
terminationGracePeriodSeconds
defined for the pod. If they exceed this, Kubernetes will forcefully terminate the container. - Idempotency: Ensure the hook can run multiple times without causing issues in case of retries.
- Error Handling: Errors in the PreStop hook don’t block the pod’s termination, so plan for fallback mechanisms if critical tasks fail.
Testing Your PreStop Hooks
To validate your PreStop hooks:
- Deploy a pod with the hook configured.
- Simulate pod termination with
kubectl delete pod <pod-name>
. - Monitor logs and external systems to ensure the hook executes as expected.
Resources for Learning More
- Kubernetes Documentation:
- Datree Guide:
- Practical Examples:
- How to gracefully shut down your apps with preStop Hook (
Datree)
- How to gracefully shut down your apps with preStop Hook (