Sub topic 9
The kubelet: the agent on every node
This chapter assumes you know what a Pod is, how the API server stores desired state, and how the scheduler assigns a Pod to a node. It covers the component that turns that assignment into running containers.
What the kubelet does
The kubelet is an agent that runs on every worker node. It is not a container. It is normally a system service, such as a systemd unit, on the node itself. Its job is to make sure the containers described in the Pods assigned to its node are running and healthy.
The kubelet does not watch the whole cluster. It watches the API server for Pods whose spec.nodeName matches its own node. When the scheduler binds a Pod to a node, that field is set, and the kubelet on that node sees the change.
The working model
The kubelet runs its own reconciliation loop, on the same pattern as the controllers you saw earlier. It compares the Pods it should run with the containers that actually exist on the node. It then acts to close the gap.
For each assigned Pod, the kubelet does the following:
- Prepares the Pod's environment: volumes, ConfigMaps, and Secrets.
- Asks the container runtime to create the Pod sandbox and pull images. It does this through the Container Runtime Interface (CRI).
- Starts the containers and watches them.
- Reports the Pod's status back to the API server.
The kubelet does not pull images or start processes itself. It delegates both to the container runtime, such as containerd or CRI-O.
Health checks and restarts
The kubelet runs the probes you define in a Pod spec. A liveness probe decides whether to restart a container. A readiness probe decides whether the Pod should receive Service traffic. A startup probe delays the other probes for slow-starting applications.
If a container exits, the kubelet restarts it according to the Pod's restartPolicy. Repeated failures lead to the CrashLoopBackOff status. In that state the kubelet waits longer between each restart attempt.
Node reporting
The kubelet also reports on the node. It registers the node with the API server and sends regular heartbeats, which the API server records as Lease objects. It also reports capacity, such as CPU and memory, and conditions, such as memory pressure. If heartbeats stop, a controller eventually marks the node NotReady.
Static Pods
Most Pods come from the API server. The kubelet can also run static Pods, which it reads from manifest files in a local directory on the node. Many clusters run control plane components this way, for example when set up with kubeadm. The kubelet creates a read-only mirror Pod in the API so you can see it.
Inspecting the kubelet
You can see the kubelet's work with the commands from the earlier walkthrough:
kubectl get nodes
kubectl describe pod <pod-name>
kubectl get events --sort-by=.metadata.creationTimestampIn the events, entries whose source is kubelet show image pulls, container creation, and container start. Failures such as ImagePullBackOff or failed probes appear here too.
Common errors
- A node shows
NotReady. The kubelet may be stopped, or it cannot reach the API server. Check the kubelet service on the node. - A Pod stays
Pendingwith a node assigned. The kubelet may be failing to pull the image or mount a volume. Read the Pod events. - A Pod restarts repeatedly. A liveness probe may be too strict, or the application itself may be crashing.
Exercise
A Pod has been scheduled to node-2, but its container never starts and kubectl describe pod shows an image pull error. Which component reported the error, and which component actually performed the pull?
Show answer
The kubelet on node-2 reported the error as an event and Pod status. The container runtime, which the kubelet calls through the CRI, performed the pull.