Logs, metrics & resource usage
Use kubectl logs, top and events to find resource-starved workloads.
By Invicly Tech, , in Kubernetes Administrator (CKA). About 9 minutes to read.
In short
kubectl gives three quick views of a running cluster: logs show what containers print, top shows live CPU and memory from metrics-server, and events show what the control plane did and why. Combining them finds resource-starved or misbehaving workloads fast.
Explained simply
To fix a problem you first have to see it. kubectl gives you three windows into a running cluster: logs (what the app says), top (how much CPU and memory things use right now), and events (what Kubernetes itself did, and why).
Logs are simply whatever the container prints to its standard output and error. Kubernetes saves them on the node and kubectl logs fetches them. If your app writes logs only to a file inside the container, kubectl logs shows nothing.
kubectl top shows live numbers from metrics-server. It is a snapshot of now, not a history. To see yesterday's peak you need a real monitoring system such as Prometheus. For the exam, top is enough to find the hungry Pod.
Events are short notes like Scheduled, Pulled, Killing or FailedMount. They are the cluster's diary, but it only keeps about the last hour. Read them early, before they expire.
Press Play on a diagram to walk through it one step at a time.
- Container stdout and stderr
- -c for one container
- --previous after crashes
- -l selects many Pods
- Live CPU and memory
- Needs metrics-server
- --sort-by=cpu or memory
- --containers for detail
- Scheduling, pulls, kills
- Kept about one hour
- --sort-by timestamp
- Filter type=Warning
- 1kubectl logs
Your terminal
- 2API server
Proxies the request to the node
- 3kubelet on the node
Reads the container's log file
- 4/var/log/pods/...
Files written by the runtime, rotated
- 5Container stdout and stderr
Only what the app prints there
In depth
Where logs live and what kubectl logs can show
The container runtime writes each container's output to files on the node under /var/log/pods/NAMESPACE_POD_UID/CONTAINER/, with symlinks in /var/log/containers. The kubelet rotates them: by default at 10Mi per file, keeping 5 files (containerLogMaxSize and containerLogMaxFiles in its config). kubectl logs reads only the current file, so very old lines may be gone even when rotated files remain on disk.
Useful flags: -f follows, --since=10m or --since-time limits by time, --tail=50 limits lines, --timestamps adds times, -c picks a container and --all-containers shows all, -p shows the previous instance. With -l you can read many Pods at once; --prefix labels each line with its source and --max-log-requests (default 5) caps parallel streams. kubectl logs deploy/web picks one Pod of the Deployment.
- kubectl logs -l app=web --all-containers --prefix --since=15m
- kubectl logs POD --timestamps | tail
- Logs disappear with the Pod; ship them elsewhere for history
What kubectl top numbers mean
kubectl top nodes and kubectl top pods show CPU in millicores and memory as working set, which is roughly the memory the kernel cannot easily free and the number the OOM killer and evictions care about. metrics-server refreshes about every 15 seconds and keeps no history.
Compare usage with requests and limits to find trouble. A Pod near its memory limit is a candidate for OOMKilled. A Pod with high CPU and a CPU limit may be throttled and slow. A node whose real usage is low but whose requests are full (kubectl describe node, Allocated resources) cannot accept new Pods: the scheduler looks at requests, not at top. --containers splits a Pod's usage per container, and --sort-by=cpu or memory ranks them.
- 1kubectl top nodes
Which node is busy?
- 2top pods -A --sort-by
Which Pods use the most?
- 3top pod --containers
Which container inside?
- 4Compare with spec
Requests and limits from get -o yaml
- 5Check events
OOMKilling, Evicted, FailedScheduling
Events: filtering and sorting
Events are API objects with a type (Normal or Warning), a reason, a message and the object they concern (involvedObject). The API server deletes them after one hour by default (its --event-ttl flag). kubectl get events sorts poorly by default, so add --sort-by=.metadata.creationTimestamp, or use kubectl events, which sorts by time for you.
Filter with field selectors: type=Warning for problems only, involvedObject.kind=Pod or involvedObject.name=web-0 for one object, reason=FailedScheduling for one kind of event. kubectl events --for pod/web-0 and kubectl events --types=Warning -w (watch) are shorter forms. Events from the kubelet about evictions and OOM kills also appear on the Node object, so look at kubectl describe node too.
- kubectl get events -A --field-selector type=Warning --sort-by=.metadata.creationTimestamp
- kubectl get events --field-selector involvedObject.name=web-0
- kubectl events -A --types=Warning
Writing answers the grader can read
Monitoring tasks often say: find the Pod with the highest CPU in namespace X and write its name to /opt/answer.txt. The grader compares the file content exactly, so write only the name, without headers or extra columns. --no-headers removes the header line; awk '{print $1}' keeps the first column.
Custom output also helps beyond top. kubectl get pods --sort-by='.status.containerStatuses[0].restartCount' finds crash-prone Pods. -o custom-columns builds small tables, such as name, node and restart count. Always cat the file afterwards to check it contains what you expect.
Learn it in 3 short steps
About 40m each. Track them with a timer in your plan.
- 1Read logs across containers~40m
- 2Check usage with kubectl top~40m
- 3Sort and filter cluster events~40m
Key points
- kubectl logs --all-containers shows every container in the Pod.
- kubectl top pods -A --sort-by=memory finds the heaviest Pods.
- Warning events surface OOMKills, failed mounts and failed scheduling.
- Write answers to the exact file path the task requests.
Key terms
Tap a card to flip it. 0/4 known
Examples
kubectl logs web-0 -c app --since=1h --tail=50
kubectl logs -l app=web --all-containers --prefix
kubectl top nodes
kubectl top pods -A --sort-by=cpu | head -5
kubectl top pod web-0 --containers
kubectl get events -A --field-selector type=Warning \
--sort-by=.metadata.creationTimestamp
kubectl events --for pod/web-0Answer file: the hungriest Pod
Task: in namespace analytics, write the name of the Pod using the most memory to /opt/top-mem.txt. Step 1: sort Pods by memory. Step 2: take the first row without headers and keep only the name. Step 3: check the file. If several Pods share a label, add -l to narrow the search as the task requires.
kubectl top pods -n analytics --sort-by=memory
kubectl top pods -n analytics --sort-by=memory --no-headers \
| head -1 | awk '{print $1}' > /opt/top-mem.txt
cat /opt/top-mem.txtWhat happened in the last hour?
Users said the app was slow 20 minutes ago. Step 1: list Warning events across namespaces, sorted by time. Step 2: you see OOMKilling and BackOff for a worker Pod. Step 3: follow up on that Pod with events for it alone, its previous logs and its current usage. Step 4: save the evidence before the events expire.
kubectl get events -A --field-selector type=Warning \
--sort-by=.metadata.creationTimestamp | tail -15
kubectl events -n jobs --for pod/worker-2
kubectl logs -n jobs worker-2 --previous --tail=30
kubectl top pod -n jobs worker-2 --containers
kubectl get events -n jobs -o yaml > /tmp/events-$(date +%H%M).yamlQuick reference
- kubectl logs -l app=web --all-containers --prefix --since=1h
- kubectl top pods -A --sort-by=memory --no-headers | head -3
- kubectl top pod POD --containers splits usage per container
- kubectl events -A --types=Warning sorts by time automatically
- Events expire after 1 hour by default (kube-apiserver --event-ttl)
- Node log files: /var/log/pods/NS_POD_UID/CONTAINER/0.log
- kubectl get pods -A --sort-by='.status.containerStatuses[0].restartCount'
Common mistakes
- Expecting kubectl top to work without metrics-server.
- Relying on events from yesterday; they expire after about an hour.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Show the last 20 log lines, with timestamps, from every container of the Pods labeled app=web.
- Exercise 2medium
Write all Warning events of namespace prod, oldest first, to /opt/prod-warnings.txt with only the reason and the object name.
- Exercise 3hard
Find, across all namespaces, the container closest to its memory limit as a percentage, and write namespace/pod/container to /opt/near-oom.txt.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Why does kubectl top say metrics not available?
- metrics-server is not installed, not Ready, or has not collected its first sample yet. Check its Pod in kube-system and its logs.
- How do I see logs of a Pod that was deleted?
- You cannot with kubectl; the logs are removed with the Pod. Use a log collector that ships logs to central storage if you need history.
- What is the difference between kubectl get events and kubectl events?
- Both read the same events. kubectl events sorts by time by default and has handy flags like --for and --types, while get events needs --sort-by and field selectors.