← All notes

Kubernetes container lifecycle hooks - postStart, preStop and Sleep

Answer: A container can run code through the lifecycle hook framework, triggered by events in the container's management lifecycle. The PreStop hook is called immediately before a container is terminated, and PostStart is executed immediately after a container is created.

Example

apiVersion: v1
kind: Pod
metadata:
  name: hook-demo
spec:
  terminationGracePeriodSeconds: 30
  containers:
  - name: app
    image: example/app
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "echo warmed > /tmp/ready"]
      preStop:
        sleep:
          seconds: 5

Hook types

| Handler | Behavior | |---|---| | exec | run a command inside the container | | httpGet | HTTP request to an endpoint on the Container | | sleep | pause for a specified duration |

Rules

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Kubernetes docs, Container Lifecycle Hooks; kubernetes/website (GitHub), container-lifecycle-hooks.md. CC-BY-4.0 (https://raw.githubusercontent.com/kubernetes/website/main/LICENSE). Copyright The Kubernetes Authors. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Kubernetes docs, Container Lifecycle Hooks; kubernetes/website (GitHub), container-lifecycle-hooks.md. CC-BY-4.0 (https://raw.githubusercontent.com/kubernetes/website/main/LICENSE). Copyright The Kubernetes Authors.

Cite this

Permanent ID: iil:note:k9xz9wmk

iinventedlight. 2026. "Kubernetes container lifecycle hooks - postStart, preStop and Sleep." iinventedlight Library, iil:note:k9xz9wmk, version of 2026-11-18. https://iinventedlight.com/library/notes/kubernetes-prestop-hook/. License: CC-BY-4.0.

@misc{iil_note_k9xz9wmk,
  author = {{iinventedlight}},
  title = {{Kubernetes container lifecycle hooks - postStart, preStop and Sleep}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-18},
  url = {https://iinventedlight.com/library/notes/kubernetes-prestop-hook/},
  note = {Permanent ID iil:note:k9xz9wmk. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/kubernetes-prestop-hook.md.