Sub topic 1
Why Kubernetes needs health checks: Pod and container lifecycle
Prerequisites
This chapter assumes you can do the following:
- Describe Pods, Deployments, and Services at a basic level.
- Read YAML manifests and apply them with
kubectl. - Use
kubectl getandkubectl describeto inspect resources. - Explain HTTP and TCP connections in general terms.
A running container is not a healthy container
Kubernetes knows whether a container process exists. It does not know whether that process does useful work. The process can be running while it is stuck in a deadlock, waiting on a database that is down, or still loading data at startup.
Without more information, Kubernetes treats a running container as a working one. It sends traffic to the Pod as soon as the container starts. Users then see errors or timeouts from an application that is not ready for them.
Health probes close this gap. A probe is a periodic check that the kubelet runs against a container. The kubelet is the agent on each node that starts and monitors the containers of the Pods assigned to that node. The probe result tells Kubernetes what to do next.
The Pod lifecycle
A Pod moves through a small set of phases. The phase is a coarse summary stored in status.phase.
| Phase | Meaning |
|---|---|
| Pending | The Pod is accepted, but one or more containers are not running yet. This includes time spent scheduling and pulling images. |
| Running | The Pod is bound to a node and at least one container is running or starting. |
| Succeeded | All containers exited with status 0 and will not restart. |
| Failed | All containers have ended and at least one failed. |
The Running phase is the important one for this tutorial. A Pod can stay in Running for hours while its application is broken. The phase says nothing about application health.
Container states
Each container inside a Pod has its own state. There are three.
- Waiting: the container is not running yet. It may be pulling an image or waiting out a restart delay.
- Running: the container process is executing.
- Terminated: the process has exited, either successfully or with an error.
The kubelet records these states in status.containerStatuses. A container that is Running has only started its process. It may not yet accept connections.
Pod conditions
Pods also carry conditions, which are named true or false flags. Two matter here:
ContainersReadyis true when all containers in the Pod are ready.Readyis true when the Pod can serve requests and should be added to the matching Service endpoints.
The Ready condition is the link between health checks and traffic. A readiness probe controls it. You can see it in the READY column of kubectl get pods.
kubectl get pods -l app=web
kubectl get pod <pod-name> -o jsonpath='{.status.conditions[*].type}'A Pod shown as Running with 0/1 in the READY column has a running container that is not ready for traffic.
The three questions
Kubernetes needs a way to answer three separate questions about each container. Each question gets its own probe type.
| Question | Probe | If the answer is no |
|---|---|---|
| Has the application finished starting? | Startup | Other probes stay disabled. If startup never succeeds, the container is restarted. |
| Is the application still working? | Liveness | The kubelet restarts the container. |
| Can it handle traffic right now? | Readiness | The Pod is removed from Service endpoints. The container keeps running. |
The responses differ on purpose. A container that is overloaded may recover if traffic stops for a while, so readiness only removes it from traffic. A container that is deadlocked will never recover on its own, so liveness restarts it.
How the pieces fit together
Later chapters cover each branch in detail. For now, notice that probes drive two different actions: restarting a container and changing where traffic goes.
What you will build
By the end of this tutorial you will configure a container with all three probes. The following snippet is the minimum that shows the idea: a readiness probe on a Pod's HTTP endpoint.
apiVersion: v1
kind: Pod
metadata:
name: web
labels:
app: web
spec:
containers:
- name: web
image: registry.example.com/web:1.0.0
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /ready
port: 8080
periodSeconds: 5After you apply it, the Pod stays at 0/1 until /ready returns a success status code. Only then does it join the Service endpoints.
Exercise
A Pod shows Running in kubectl get pods, but users get errors from the Service. Name two reasons this can happen, and name the probe that addresses each.
Show answer
First, the application may still be starting and cannot accept requests yet. A readiness probe keeps the Pod out of Service endpoints until it can serve, and a startup probe protects slow starts. Second, the application may be deadlocked while its process keeps running. A liveness probe detects this and makes the kubelet restart the container. The Running phase only says a container process exists.