Sub topic 8
Observing real usage with kubectl top and pod status
This chapter assumes you can apply YAML manifests with kubectl and know what requests, limits, throttling and OOM kills are from the earlier chapters. Here you will learn how to see what a container actually uses, so you can compare it with what you declared.
Two sources of truth
Requests and limits are declarations. They say nothing about real consumption. Two places answer the "what is happening" question:
- Metrics, through
kubectl top. These are current CPU and memory numbers. - Pod status and events, through
kubectl getandkubectl describe. These record what Kubernetes did: the QoS class, why a container last stopped, and why a pod could not be scheduled.
You need both. Metrics show the present. Status shows what already went wrong.
kubectl top
kubectl top reads from the Metrics API, which is usually served by metrics-server. If metrics-server is not installed, the command fails with an error such as Metrics API not available.
kubectl top pod web
kubectl top pod web --containers
kubectl top nodeThe output looks like this:
NAME CPU(cores) MEMORY(bytes)
web 12m 48MiRead the numbers as follows:
12mis 12 millicores, or 0.012 of one core. It is a short-window average, not a peak.48Miis the working set memory. This is the figure the kubelet uses when it decides on OOM kills and evictions.--containerssplits a pod into its containers. Use it when a pod has sidecars, because the pod total hides which container is heavy.
Reading pod status
The second half of the example command set reads status fields directly.
kubectl get pod web -o jsonpath='{.status.qosClass}'
kubectl get pod web -o jsonpath='{.status.containerStatuses[0].lastState.terminated.reason}'
kubectl describe pod web| Field or section | What it tells you |
|---|---|
| `status.qosClass` | Guaranteed, Burstable or BestEffort, derived from your requests and limits |
| `lastState.terminated.reason` | Why the previous container instance stopped, for example `OOMKilled` |
| `lastState.terminated.exitCode` | `137` means the process received SIGKILL, which is typical for an OOM kill |
| `describe` Events | Scheduling failures such as `FailedScheduling`, plus evictions and restarts |
| `describe` Limits and Requests | The values the container was actually admitted with, including LimitRange defaults |
The lastState field is empty until a container has terminated at least once. An empty result for a new pod is normal.
Comparing usage with settings
Put the two views together. Suppose the web pod from the first example has a 128Mi memory request and a 256Mi limit.
- Run
kubectl top pod web --containersand note 48Mi of memory and 12m of CPU. - Compare memory with the request (128Mi). Usage is well below it, so the pod reserves more than it uses.
- Compare CPU with the request (250m). Usage is far below it, so the node holds capacity that other pods cannot claim.
- Check
lastState. If it is empty, the container has not been killed.
A single reading is weak evidence. Sample during peak traffic and over several days before you change a value.
Common errors
Metrics API not available. metrics-server is missing or not ready. Check it withkubectl get deployment metrics-server -n kube-system.- No data right after pod creation. Metrics take roughly a minute to appear. Wait and retry.
- Treating
topas a limit check. A container near its CPU limit is throttled, andtopsimply shows usage flat at the limit. Memory near the limit is the warning sign for an OOM kill. - Reading only
topafter a crash. A killed container restarts with fresh, low numbers. The cause is inlastState, not in the metrics.
Exercise
A pod keeps restarting. kubectl top pod shows low memory, but you suspect an OOM kill. Which command shows the reason for the last termination, and what value confirms your suspicion?
Show answer
Run kubectl get pod <name> -o jsonpath='{.status.containerStatuses[0].lastState.terminated.reason}', or read the Last State section of kubectl describe pod <name>. The reason OOMKilled, usually with exit code 137, confirms it. Memory looks low in top because the restarted container has just started.