Sign In

Curriculum 19: Resource Management

kubectl top Deep Dive

12 min · 25 XP

Deep Dive into kubectl top

The kubectl top command displays real-time CPU and memory usage for pods and nodes. It requires the metrics-server to be installed in the cluster.

Prerequisites

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

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

Node Resource Usage

# All nodes
kubectl top nodes

# Output:
# NAME       CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
# node-1     350m         17%    2048Mi           52%
# node-2     820m         41%    3200Mi           80%

Pod Resource Usage

# All pods in current namespace
kubectl top pods

# All pods across all namespaces
kubectl top pods -A

# Specific namespace
kubectl top pods -n production

Sorting Results

# Sort by CPU usage (highest first)
kubectl top pods --sort-by=cpu

# Sort by memory usage
kubectl top pods --sort-by=memory

# Sort nodes
kubectl top nodes --sort-by=cpu

Container-Level Metrics

Use the --containers flag to see resource usage broken down by individual containers within each pod:

kubectl top pods --containers

# Output:
# POD          CONTAINER   CPU(cores)   MEMORY(bytes)
# web-app      nginx       10m          32Mi
# web-app      sidecar     5m           16Mi

This is essential for multi-container pods where you need to identify which container is consuming resources.

Combining with Other Commands

# Find the top 5 memory-consuming pods
kubectl top pods --sort-by=memory -A --no-headers | head -5

# Check if any pod exceeds its requests
kubectl top pods -n production --containers

Metrics from kubectl top reflect a point-in-time snapshot. For historical trends, use a monitoring system like Prometheus.