liveBook · Manning

Post-start hook

As explained in Kubernetes in Action, Second Edition

A post-start hook is a Kubernetes container lifecycle handler invoked when a container starts.

A post-start hook runs when a Kubernetes container starts. It is useful for initialization or warm-up that should happen at container start without changing the application code or container image. The hook is configured on an individual regular container, not on an init container. section 6.3 section 6.3.1

Set `lifecycle.postStart` on the container and choose a handler. `exec` runs a command inside the container; `httpGet` sends an HTTP GET request to the application. Lifecycle hooks do not support the `tcpSocket` handler available to probes. section 6.3 section 6.3.1

Generate a quote when Nginx starts

An `nginx:alpine` container can use a post-start `exec` command to install `fortune`, download quote files, and write a generated quote to `/usr/share/nginx/html/quote`. To check the result locally, run `kubectl port-forward quote-poststart 1080:80`, then `curl localhost:1080/quote`. Using local port 1080 also avoids the permission error shown when trying to listen on port 80. section 6.3.1

Listing 6.3 Pod with a post-start lifecycle hook
apiVersion: v1
kind: Pod
metadata:
  name: quote-poststart     #1
spec:
  containers:
  - name: nginx            #2
    image: nginx:alpine    #2
    ports:                   #3
    - name: http             #3
      containerPort: 80      #3
    lifecycle:                    #4
      postStart:                  #4
        exec:                     #4
          command:                #4
          - sh                    #5
          - -c       #6
          - |               #7
            apk add fortune && \                                            #8
            curl -O https://luksa.github.io/kiada/book-quotes.txt && \       #8
            curl -O https://luksa.github.io/kiada/book-quotes.txt.dat && \   #8
            fortune book-quotes.txt > /usr/share/nginx/html/quote            #8

Complete Pod manifest showing where the `postStart.exec.command` configuration belongs. section 6.3.1

The hook runs alongside the container’s main process, not before it. A slow hook can therefore leave that process running while the hook is still executing. The Kubelet currently waits for one container’s post-start handler before creating the next container listed in the Pod spec; that ordering behavior is an implementation detail that may change. A failed hook can be inspected through its `PostStartHookError` status and `FailedPostStartHook` event. section 6.3.1 section 6.4.2

How Kubernetes in Action, Second Edition covers it

How deep it goes
Shows exec and HTTP GET configuration, a worked Pod example, and startup-order behavior; identifies ordering as changeable.
Examples
Uses an Nginx quote Pod, port forwarding, a curl check, an HTTP warm-up handler, and failed-hook output.
What it assumes
Builds on Pod containers, init containers, and liveness probes while defining the hook’s role and handlers.
What you can do after
Operate/configure it with a Pod’s postStart handler, verify its effect, and inspect a failed hook.
How it connects
Relates post-start hooks to init containers, pre-stop hooks, liveness probes, and the container lifecycle.

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.