Sign In

Curriculum 7: Reading & Inspecting

kubectl top

10 min · 15 XP

kubectl top -- Resource Metrics

kubectl top shows real-time CPU and memory usage for Pods and Nodes. It is the quickest way to check if a workload is consuming too many resources or if a node is under pressure.

Prerequisites

kubectl top requires the Metrics Server to be installed in your cluster. Most managed Kubernetes services (EKS, GKE, AKS) include it by default. For local clusters, you may need to install it:

# Check if metrics server is running
kubectl get deployment metrics-server -n kube-system

# If not installed, apply the official manifest
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

Node Metrics

See CPU and memory usage across all nodes:

kubectl top nodes
NAME       CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
worker-1   250m         12%    1024Mi          52%
worker-2   480m         24%    1536Mi          78%
worker-3   120m         6%     768Mi           39%

This helps you identify nodes that are running hot and might need more capacity or workload rebalancing.

Pod Metrics

View resource consumption for Pods:

# All pods in the current namespace
kubectl top pods

# All pods across all namespaces
kubectl top pods -A

# Pods in a specific namespace
kubectl top pods -n production

# Sort by CPU usage
kubectl top pods --sort-by=cpu

# Sort by memory usage
kubectl top pods --sort-by=memory
NAME                     CPU(cores)   MEMORY(bytes)
my-app-abc12             45m          128Mi
my-app-def34             52m          135Mi
database-0               200m         512Mi

Container-Level Metrics

See metrics broken down by container within each Pod:

kubectl top pods --containers

This is useful for multi-container Pods where you need to know which container is using the most resources.

Using Metrics for Right-Sizing

Compare actual usage against resource requests and limits to right-size your workloads:

# See what resources are requested
kubectl get pods -o custom-columns=\
NAME:.metadata.name,\
CPU_REQ:.spec.containers[0].resources.requests.cpu,\
MEM_REQ:.spec.containers[0].resources.requests.memory

# Then compare with actual usage
kubectl top pods

If a Pod requests 500m CPU but only uses 50m, you are wasting cluster capacity. If a Pod uses more than its request, it may get throttled or evicted.

Key Takeaways

  • kubectl top requires the Metrics Server to be installed
  • Use kubectl top nodes to monitor cluster-wide resource pressure
  • Use kubectl top pods to identify resource-hungry workloads
  • Sort by CPU or memory to quickly find the heaviest consumers
  • Compare top output with resource requests to right-size your workloads