Monitor Kubernetes with kubectl top

Monitor Kubernetes Clusters with kubectl top

As part of managing a Kubernetes cluster, monitoring resource usage such as CPU and memory is crucial to maintaining efficiency and preventing outages. The kubectl top command offers an easy way to see live metrics of your pods and nodes right from the command line.

Prerequisites

  • An active Kubernetes cluster
  • kubectl (Official site) installed and configured on your machine
  • Metrics Server deployed in your Kubernetes cluster

Installing the Metrics Server

The first step in using kubectl top is ensuring that the Metrics Server is running in your cluster. Metrics Server aggregates live resource metrics from the nodes and pods in your cluster.

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

This command deploys the latest version of the Metrics Server.

Using kubectl top

Viewing Node Metrics

To view resource metrics of nodes, run the following command:

kubectl top nodes

You will see output similar to:

NAME               CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%

Viewing Pod Metrics

To check the metrics collected for each pod, use:

kubectl top pods --namespace=<your-namespace>

The output will list CPU and memory usage for each pod in the specified namespace.

Troubleshooting

  • Metrics Server not available: Verify that Metrics Server pods are running by using kubectl get pods -n kube-system.
  • No metrics displayed: Check if the Metrics Server pods have network connectivity to your cluster’s API server.

Summary Checklist

  • Ensure kubectl is configured correctly and connected to your cluster.
  • Install the Metrics Server for real-time metrics collection.
  • Use kubectl top nodes and kubectl top pods to monitor resource usage.

For an in-depth look at setting up a Kubernetes environment, check out our guide on exposing services in Kubernetes.

Post Comment

You May Have Missed