liveBook · Manning

Lifecycle method

As explained in Kubernetes in Action, Second Edition

A lifecycle hook is a Kubernetes container configuration that triggers an action at a defined point in the container lifecycle.

A lifecycle hook is a per-container configuration that triggers an action at a defined point in the container’s life. It lets you add startup or shutdown behavior without changing an existing application or rebuilding its image. section 6.3 section 6.3.1

Kubernetes supports two lifecycle hooks:

  • Post-start runs when the container starts. It can execute a command inside the container or send an HTTP GET request to the application, such as a warm-up request. section 6.3 section 6.3.1
  • Pre-stop runs shortly before the container stops. Kubernetes invokes it before sending SIGTERM to the container’s main process. section 6.3 section 6.5

Hooks belong to regular containers, not init containers, and lifecycle hooks do not support tcpSocket handlers. A failing or slow hook affects the container lifecycle: shutdown still follows the termination grace period, after which the process can be killed. section 6.3 section 6.5

Generating a quote when an Nginx container starts

A post-start exec hook can install fortune, download quote data, and write a generated quote into Nginx’s web directory: lifecycle: postStart: exec: command: - sh - -c - | apk add fortune && \ curl -O https://luksa.github.io/kiada/book-quotes.txt && \ curl -O https://luksa.github.io/kiada/book-quotes.txt.dat && \ fortune book-quotes.txt > /usr/share/nginx/html/quote After exposing the pod locally with `kubectl port-forward quote-poststart 1080:80`, request the result with `curl localhost:1080/quote`. section 6.3.1

Figure 6.9 How the post-start and pre-stop hook fit into the container’s life cycle
figure

Use the lifecycle diagram to see where post-start and pre-stop hooks occur relative to container startup and shutdown. section 6.3

How Kubernetes in Action, Second Edition covers it

How deep it goes
Defines hook types, handlers, scope, failure visibility, shutdown ordering, and termination-grace behavior.
Examples
Uses an Nginx quote service, shell commands, kubectl port-forward, curl, and an HTTP warm-up endpoint.
What it assumes
Assumes basic familiarity with pods and containers while teaching hook timing, configuration, and operational behavior.
What you can do after
Operate/configure containers with exec or HTTP lifecycle actions for initialization, warm-up, and shutdown.
How it connects
Connects lifecycle hooks with init containers, liveness probes, startup probes, SIGTERM handling, and termination grace periods.

Also explained in

Compare how 3 books explain lifecycle method →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.