0
0
KubernetesHow-ToBeginner · 3 min read

How to Use kubectl top to Monitor Kubernetes Resources

Use kubectl top nodes to see CPU and memory usage of all nodes, and kubectl top pods to check resource usage of pods in a namespace. This command requires the Metrics Server to be installed in your cluster.
📐

Syntax

The kubectl top command shows resource usage for nodes or pods in your Kubernetes cluster.

  • kubectl top nodes: Displays CPU and memory usage for all nodes.
  • kubectl top pods: Displays CPU and memory usage for pods in the current namespace.
  • kubectl top pods --namespace <namespace>: Shows pod usage in a specific namespace.
bash
kubectl top nodes
kubectl top pods
kubectl top pods --namespace <namespace>
💻

Example

This example shows how to check CPU and memory usage of nodes and pods in the default namespace.

bash
kubectl top nodes
kubectl top pods
Output
NAME CPU(cores) MEMORY(bytes) node-1 250m 512Mi node-2 180m 400Mi NAME CPU(cores) MEMORY(bytes) nginx-pod-1 50m 100Mi backend-pod-1 120m 200Mi
⚠️

Common Pitfalls

Metrics Server not installed: The kubectl top command needs the Metrics Server running in your cluster. Without it, you will get an error like error: metrics API not available.

Wrong namespace: By default, kubectl top pods shows pods in the current namespace. Use --namespace to specify another.

Permissions: Make sure your user has permission to access metrics.

bash
kubectl top pods
# Error: error: metrics API not available

# Correct: Install Metrics Server
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
📊

Quick Reference

CommandDescription
kubectl top nodesShow CPU and memory usage of all nodes
kubectl top podsShow CPU and memory usage of pods in current namespace
kubectl top pods --namespace Show pod usage in specified namespace
kubectl apply -f Install Metrics Server if missing

Key Takeaways

kubectl top shows CPU and memory usage for nodes and pods in Kubernetes.
Metrics Server must be installed for kubectl top to work.
Use --namespace to check pods in namespaces other than the current one.
Check permissions if you get access errors.
kubectl top nodes and kubectl top pods are the main commands to remember.