What Are PreStop Hooks in Kubernetes? Use cases and examples

Kubernetes PreStop Hooks

PreStop hooks in Kubernetes are part of the container lifecycle management system, allowing you to run specific actions just before a container is terminated. These hooks provide a way to gracefully handle the shutdown process, helping applications perform cleanup tasks or finalize operations.

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:

  1. Kubernetes sends the termination signal to the pod.
  2. The PreStop hook executes its defined action.
  3. 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.

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.

3. Data Finalization

Applications often hold temporary or in-memory data that needs to be saved during shutdown.

4. Releasing Locks or Resources

If your application uses distributed locks, they must be released before the pod shuts down to prevent resource contention.

5. Logging and Monitoring

PreStop hooks can be used to log shutdown events or update monitoring systems to reflect the pod’s status.

Things to Consider

Testing Your PreStop Hooks

To validate your PreStop hooks:

  1. Deploy a pod with the hook configured.
  2. Simulate pod termination with kubectl delete pod <pod-name>.
  3. Monitor logs and external systems to ensure the hook executes as expected.

Resources for Learning More

  1. Kubernetes Documentation:
  1. Datree Guide:
  1. Practical Examples:

Datree)

How to Gracefully Shutdown Your Apps with a preStop Hook - YouTube

How to Gracefully Shutdown Your Apps with a preStop Hook Datree

Datree2.81K subscribers