Overview

6 Managing the pod lifecycle and container health

This chapter deepens the practical understanding of how a pod and its containers operate over their entire lifetime. It explains how to interpret pod status (phases, conditions, and per-container states) to diagnose behavior, and it shows how these signals change as a pod starts, runs, and terminates. With that foundation, it moves from observation to control: how Kubernetes sustains container health, how to hook into container startup and shutdown, and how the full pod lifecycle—initialization, run, and termination—plays out in real clusters.

The chapter details the mechanisms Kubernetes uses to keep workloads healthy. The Kubelet recreates failed containers and honors a pod-wide restartPolicy (Always, OnFailure, Never), applying exponential backoff that can lead to CrashLoopBackOff when failures repeat. Liveness probes provide active health checks via HTTP, TCP, or command execution, with configurable timing, thresholds, and timeouts; startup probes decouple slow initialization from steady-state monitoring to prevent false restarts. Guidance emphasizes designing probes that are simple, lightweight, and focused on internal health (not external dependencies), avoiding unnecessary restarts and resource contention, and using failureThreshold instead of bespoke retry logic. Throughout, the chapter shows how to read container status, lastState, exit codes, and events to understand what happened and why.

To run logic at key moments, containers can define lifecycle hooks: postStart (runs alongside process startup) and preStop (runs just before termination), each supporting exec or HTTP handlers. The text highlights important caveats—postStart runs immediately and can delay readiness or even trigger restarts if it fails; HTTP hooks must not depend on a server that isn’t ready yet; preStop failures don’t block shutdown, so monitoring events is essential. The lifecycle sections stitch everything together: image pull policies and init containers during initialization (idempotent by design), parallel container operation during the run stage (with ordering nuances around postStart), and graceful termination using preStop, signals, and configurable grace periods (including delete-time overrides and custom stop signals). Practical fixes—like handling SIGTERM in app code—demonstrate how to achieve fast, predictable shutdowns.

The phases of a Kubernetes Pod
The transitions of the pod’s conditions during its lifecycle
The possible states of a container
The pod’s restartPolicy determines whether its containers are restarted.
Exponential back-off between container restarts
The configuration and operation of a liveness probe
The exec liveness probe runs the command inside the container.
Fast detection of application health problems using a combination of startup and liveness probe
How the post-start and pre-stop hook fit into the container’s life cycle
The Kubernetes in Action Demo Application Suite
The operation of the quote-poststart Pod
The three stages of the pod’s lifecycle
An overview of the three different image-pull policies
All init containers must run to completion before the regular containers can start.
A container’s termination sequence
The termination sequence inside a pod
Complete overview of the pod’s initialization stage
Complete overview of the pod’s normal operation

Summary

  • The status of the pod contains information about the phase of the pod, its conditions, and the status of each of its containers. You can view the status by running the kubectl describe command or by retrieving the full pod manifest using the command kubectl get -o yaml.
  • Depending on the pod’s restart policy, its containers can be restarted after they are terminated. In reality, a container is never actually restarted. Instead, the old container is destroyed, and a new container is created in its place.
  • If a container is repeatedly terminated, an exponentially increasing delay is inserted before each restart. There is no delay for the first restart, then the delay is 10 seconds, and it then doubles before each subsequent restart. The maximum delay is 5 minutes and is reset to zero when the container has been running properly for at least twice this time.
  • An exponentially increasing delay is also used after each failed attempt to download a container image.
  • Adding a liveness probe to a container ensures that the container is restarted when it stops responding. The liveness probe checks the state of the application via an HTTP GET request, by executing a command in the container, or opening a TCP connection to one of the network ports of the container.
  • If the application needs a long time to start, a startup probe can be defined with settings that are more forgiving than those in the liveness probe to prevent premature restarting of the container.
  • You can define lifecycle hooks for each of the pod’s main containers. A post-start hook is invoked when the container starts, whereas a pre-stop hook is invoked when the container must shut down. A lifecycle hook is configured to either send an HTTP GET request or execute a command within the container.
  • If a pre-stop hook is defined in the container and the container must terminate, the hook is invoked first. The SIGTERM signal is then sent to the main process in the container. If the process doesn’t stop within terminationGracePeriodSeconds after the start of the termination sequence, the process is killed.
  • When you delete a Pod object, all its regular containers are terminated in parallel, and any native sidecars are terminated afterward, once all regular containers have stopped. The pod’s deletionGracePeriodSeconds is the time given to the containers to shut down. By default, it’s set to the termination grace period but can be overridden using the kubectl delete command.
  • If shutting down a pod takes a long time, it is likely that one of the processes running in it doesn’t handle the SIGTERM signal. Adding a SIGTERM signal handler is a better solution than shortening the termination or deletion grace period.

FAQ

How do I check a pod’s current phase and what do the phases mean?The pod phase is a high‑level summary: Pending, Running, Succeeded, Failed, or Unknown. Use kubectl get po NAME -o yaml | grep phase or kubectl describe pod NAME. Note: kubectl get pods shows more specific problem states in STATUS for unhealthy pods, not always the exact phase.
What are pod conditions and how do they differ from the phase?Pod conditions provide finer‑grained status and multiple can be true at once. Common types: PodScheduled, Initialized, ContainersReady, Ready. View them with kubectl describe pod NAME or kubectl get pod NAME -o json | jq .status.conditions to see status, reason, message, and lastTransitionTime.
How can I inspect individual container status inside a pod?Use kubectl describe pod NAME to see each container’s state (Waiting, Running, Terminated), lastState, ready flag, restartCount, image, and containerID. Or kubectl get pod NAME -o json | jq .status.containerStatuses. Init containers appear under .status.initContainerStatuses.
What happens when a container crashes, and what is CrashLoopBackOff?The Kubelet doesn’t “restart” a container; it recreates it. Repeated failures trigger exponential backoff before the next start (0s, 10s, 20s, 40s, 80s, up to 5 min; reset after ~10 min of healthy run). While waiting, state shows Waiting with reason CrashLoopBackOff. RESTARTS increments; other containers in the pod keep running.
How does restartPolicy affect container restarts?restartPolicy is set on the pod and applies to all its containers: Always (default) restarts regardless of exit code, OnFailure restarts only on non‑zero exit, Never never restarts. For init containers, a failure restarts them only if policy is Always/OnFailure; with Never, the pod gets stuck (Init:Error) until recreated.
What is a liveness probe and which probe types are available?A liveness probe tells Kubernetes if a container is still healthy; failures lead to termination (and restart if allowed). Types: httpGet (HTTP 2xx/3xx = success), tcpSocket (successful TCP connect = success), exec (command exit code 0 = success). Probes run only in regular containers, not init containers.
How do I configure and tune liveness probes effectively?Key fields: initialDelaySeconds (wait before first probe), periodSeconds (interval), timeoutSeconds (per‑probe timeout), failureThreshold (consecutive failures before action). Keep handlers lightweight, avoid authentication on health endpoints, and check only internal component health (don’t fail due to external dependencies).
When should I use a startup probe and how does it interact with liveness?Use a startup probe for slow‑starting apps. While it runs, liveness/readiness are suppressed. Once startup succeeds, liveness begins (typically with shorter periods to detect failures quickly). If the startup probe reaches its failureThreshold, the container is terminated.
What are container lifecycle hooks (postStart and preStop), and what are common pitfalls?postStart runs immediately after container creation (in parallel with the main process); failures cause the container to be restarted and can delay pod readiness. preStop runs just before SIGTERM is sent; it should help graceful shutdown but won’t block termination if it fails. Both support exec and httpGet (no tcpSocket). Avoid httpGet hooks targeting the same container—they can cause endless restart loops if the app isn’t ready.
How are containers and pods terminated, and how can I control shutdown time?On termination: preStop (if set) runs, then SIGTERM is sent; the app has terminationGracePeriodSeconds (default 30s) to exit before SIGKILL. Deleting a pod starts the deletion grace period; you can override it with kubectl delete pod NAME --grace-period SECONDS (0 skips preStop). Ensure apps handle SIGTERM for fast, graceful shutdown.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Kubernetes in Action, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Kubernetes in Action, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Kubernetes in Action, Second Edition ebook for free