Checking Resource Usage

Difficulty: Beginner

Question

How do you check real-time CPU and memory usage of nodes and Pods in a Kubernetes cluster?

Answer

Use kubectl top nodes to see current CPU and memory usage per node, and kubectl top pods (with -n, -A, -l or --containers) for Pods and containers. Both read from the Metrics Server through the metrics.k8s.io API, so Metrics Server must be installed. The numbers are a recent snapshot, roughly 15 to 60 seconds old, not a historical graph.

The commands you will actually use

  • kubectl top nodes: CPU in millicores and memory per node, plus percentages.
  • kubectl top pods -n checkout: usage per Pod in a namespace.
  • kubectl top pods -A --sort-by=memory: find the heaviest Pods across the cluster.
  • kubectl top pod <name> --containers: break usage down per container, useful when a sidecar is the culprit.
  • kubectl top pods -l app=payments: filter by label.

Reading the output correctly

  • CPU is shown in millicores: 250m is a quarter of one core.
  • Memory is the container's working set (roughly memory that cannot be easily reclaimed), shown in Mi or Gi. This is the same number the kubelet uses for OOM and eviction decisions, so it is more useful than total cache-inclusive memory.
  • Node percentages are relative to the node's allocatable resources by default. Newer kubectl versions add --show-capacity to compare against raw capacity instead.

Usage is not the same as requests

A frequent interview trap: kubectl top shows actual usage, while scheduling is based on requests. To see what is reserved on a node, use kubectl describe node <name> and look at the "Allocated resources" section.

QuestionCommand
What is the Pod using right now?kubectl top pod
What has the Pod reserved?kubectl get pod -o yaml (resources.requests)
Why will no new Pod fit on this node?kubectl describe node (allocated requests vs allocatable)
What did usage look like over the last week?Prometheus and Grafana, not kubectl top

A node can show 30% CPU in kubectl top and still reject new Pods because its requests are 100% allocated. That is a sign of over-requesting and a good conversation about rightsizing.

When it does not work

  • error: Metrics API not available: Metrics Server is missing or unhealthy. Check kubectl get apiservice v1beta1.metrics.k8s.io and the metrics-server Pod logs.
  • New Pods show no metrics for the first scrape interval or so.
  • For history, alerting or per-request metrics, use Prometheus with node-exporter and cAdvisor metrics, or your cloud provider's monitoring.

In practice, during an incident such as a festive sale spike at Flipkart, kubectl top pods --sort-by=cpu is often the first quick check to see which service is hot before opening Grafana.

Code examples

Everyday kubectl top usage

# Node level
kubectl top nodes

# Top 5 memory consumers across all namespaces
kubectl top pods -A --sort-by=memory | head -6

# Per-container breakdown for one Pod
kubectl top pod payments-api-7c9f6d-x2k4p -n payments --containers

# Compare usage with what is reserved on a node
kubectl describe node ip-10-0-1-23 | grep -A 8 "Allocated resources"

`kubectl top` answers "what is being used now", while `describe node` answers "what has been promised". You often need both to explain why scheduling fails or why a node is under pressure.

Key points

  • `kubectl top nodes` and `kubectl top pods` need Metrics Server.
  • CPU is shown in millicores, memory as working set bytes.
  • `--containers`, `--sort-by=cpu|memory`, `-A` and `-l` are the most useful flags.
  • Usage is not requests; check `kubectl describe node` for allocated resources.
  • No history: use Prometheus and Grafana for trends.

Common follow-up questions

  • Why can a node show low CPU usage but still not fit new Pods?
  • What is working set memory and why does the kubelet use it?
  • How would you find which container in a Pod is causing high memory?
  • What would you do if `kubectl top` says the Metrics API is not available?

Interview tips

  • Mention the Metrics Server dependency; it shows you know where the data comes from.
  • Proactively contrast usage vs requests; it is the most common follow-up.
  • Quote real flags like `--containers` and `--sort-by` to show hands-on experience.

Concepts covered

kubectl top, Metrics Server, Working Set Memory, Millicores, Requests vs Usage

More Kubernetes & Orchestration questions