inspection
kubectl top
Displays CPU and memory usage for nodes or pods, powered by the metrics.k8s.io API. Requires the metrics-server (or an equivalent aggregator) to be installed in the cluster.
kubectl top (node | pod) [NAME | -l label] [flags] Common flags
| Flag | Purpose |
|---|---|
| -A, --all-namespaces | Show usage for pods across every namespace. |
| -l, --selector | Filter pods by label. |
| --containers | For pods, break down usage per container. |
| --sort-by | Sort by cpu or memory. |
| --use-protocol-buffers | Use protobuf for lower overhead on large clusters. |
| --no-headers | Skip the column header row (useful in scripts). |
Examples
kubectl top nodes --sort-by=cpu Rank nodes by CPU pressure.
kubectl top pods -A --sort-by=memory Find the largest memory consumers across the cluster.
kubectl top pod web-abc --containers Per-container breakdown for one pod.
Gotcha
Returns "Metrics API not available" without metrics-server. Numbers are point-in-time and can lag by up to a minute.
Related commands
kubectl describe
Shows a human-readable, aggregated view of a resource including its spec, status, related events, and controllers. It is the go-to command for debugging why a pod is Pending, CrashLoopBackOff, or otherwise misbehaving.
kubectl get
Lists one or more resources of a given type from the cluster. It is the primary read command for pods, services, deployments, and any other Kubernetes object, with flexible output formats for scripting and inspection.
kubectl autoscale
Creates a HorizontalPodAutoscaler (v2) that scales a workload based on CPU utilization. For memory or custom/external metrics, author the HPA manifest directly since this command only exposes CPU targets.
kubectl cluster-info
Prints the endpoints of the control-plane services for the current context. The dump subcommand collects a large diagnostic bundle of cluster state for offline analysis.
kubectl explain
Prints documentation for a resource kind and its fields, sourced from the live cluster's OpenAPI schema. It is the offline-safe reference for authoring manifests against the exact API version your cluster serves.