kubectl top pod — CPU/Memory Usage & One-Liners¶
Reference: https://kubernetes.io/docs/reference/kubectl/cheatsheet/
See also:
node_resource.md.mdforkubectl top node, millicores,--sort-by,--no-headers, and the full shell pipeline reference.
The Exercise¶
Find the pod that consumes the most CPU across all namespaces. Store the result in
high_cpu_pod.txtin the format:pod_name,namespace
Solution:
kubectl top pod --sort-by=cpu -A --no-headers | head -1 | awk '{print $2 "," $1}' > high_cpu_pod.txt
Full Breakdown — Every Part¶
Stage 1: kubectl top pod --sort-by=cpu -A --no-headers¶
| Flag | What it does |
|---|---|
--sort-by=cpu |
Sort output by CPU usage, highest first. Also accepts memory |
-A |
Short for --all-namespaces — show pods from every namespace including kube-system |
--no-headers |
Suppress the column header row so head -1 gets the first data row, not the header |
Output:
kube-system kube-apiserver-controlplane 98m 282Mi
kube-system etcd-controlplane 22m 58Mi
default my-app-pod 15m 120Mi
kube-system coredns-abc123 5m 18Mi
Stage 2: | head -1¶
Takes only the first line. Since output is sorted by CPU descending, first line = highest CPU pod.
Stage 3: | awk '{print $2 "," $1}'¶
awk extracts fields by whitespace position.
| Expression | Value |
|---|---|
$1 |
kube-system (namespace) |
$2 |
kube-apiserver-controlplane (pod name) |
$2 "," $1 |
kube-apiserver-controlplane,kube-system |
The required format is pod_name,namespace — so $2 comes first, then a literal comma ",", then $1. Reversed from the column order.
Stage 4: > high_cpu_pod.txt¶
Writes the output to the file. Creates if it doesn't exist, overwrites if it does.
Final result in the file:
The #1 Mistake — Column Positions Change With -A¶
This is the most common error on this type of question. The namespace column gets prepended when you use -A.
kubectl top pod --no-headers (single namespace):
$1 = pod name
kubectl top pod -A --no-headers (all namespaces):
$1 = namespace, $2 = pod name
If you use awk '{print $1}' on -A output, you get the namespace, not the pod name. The answer is wrong and you won't know why unless you know this.
Compare with top node which never has a namespace column:
$1 = node name always.
Common Exam Variants¶
Highest CPU pod, all namespaces → pod_name,namespace:
Highest memory pod, all namespaces → pod_name,namespace:
Highest CPU pod in a specific namespace → just pod name:
No -A here, so $1 is the pod name again.
Highest CPU node → node name:
Current context + highest memory node → context,node:
echo "$(kubectl config current-context),$(kubectl top node --sort-by=memory --no-headers | head -1 | awk '{print $1}')" > result.txt
Verify¶
Always cat the file after writing:
Check: one line, comma-separated with no spaces, correct order (pod_name,namespace not namespace,pod_name).
Quick Reference¶
# Pods — current namespace ($1=pod name)
kubectl top pod
kubectl top pod --sort-by=cpu --no-headers
# Pods — all namespaces ($1=namespace, $2=pod name)
kubectl top pod -A
kubectl top pod -A --sort-by=cpu --no-headers
# Nodes ($1=node name)
kubectl top node --sort-by=memory --no-headers
# One-liners
kubectl top pod -A --sort-by=cpu --no-headers | head -1 | awk '{print $2 "," $1}' # pod_name,namespace
kubectl top pod -A --sort-by=memory --no-headers | head -1 | awk '{print $2 "," $1}' # pod_name,namespace
kubectl top node --sort-by=cpu --no-headers | head -1 | awk '{print $1}' # node name only