Skip to main content
In this article, we explore how to leverage the kubectl top command to gain real-time insights into CPU and memory usage of your Kubernetes cluster. By understanding these metrics for both pods and nodes, you can quickly diagnose issues such as memory exhaustion and make informed decisions about scaling workloads.

Comparing kubectl get and kubectl top

Previously, we looked at the kubectl get command, which retrieves essential metadata about Kubernetes resources. For example, executing:
displays basic information such as readiness, status, restart counts, and age for each pod in the kube-system namespace:
In contrast, the kubectl top command provides live metrics on resource usage. For instance, running:
yields the current CPU and memory consumption for each pod:

Inspecting Node Information

To view node details, use the following command:
which displays each node’s status, roles, age, and Kubernetes version:
For enhanced details such as internal IP, OS image, kernel version, and container runtime, append the -o wide flag:
The output provides a comprehensive view:
To monitor the resource usage of each node, execute:
This command displays key metrics such as CPU consumption, memory usage, and memory percentage:
Ensure you have the metrics server installed in your cluster for the kubectl top command to return accurate data.

How kubectl top Works

The kubectl top command depends on the metrics server, which collects and aggregates resource usage data from each node’s kubelet. Without a properly configured metrics server, the command will not display any metrics. This tool is invaluable for quickly identifying resource bottlenecks and monitoring overall cluster performance, helping you decide whether to scale nodes or investigate pods using excessive resources.

Summary

In summary, both kubectl get and kubectl top offer different perspectives on your Kubernetes cluster: Using these complementary commands, you can efficiently monitor your cluster’s health and performance, diagnose issues, and make scaling decisions based on up-to-date resource consumption data. For more Kubernetes insights, check out Kubernetes Documentation. Happy monitoring!

Watch Video