
Metrics Server and kubectl top
See your cluster in real-time. Learn to install the Metrics Server, interpret the 'top' command results, and identify resource-hungry pods before they crash your nodes.
Metrics Server: The Vital Sign Monitor of Kubernetes
When you are running a complex infrastructure (especially one housing memory-intensive AI models), you cannot afford to be blind. You need to know:
- Which of my 20 worker nodes is at 90% CPU?
- Which FastAPI pod is currently leaking memory?
- Is my Next.js frontend actually using the 2GB of RAM I requested?
Kubernetes provides the answer through the Metrics Server.
The Metrics Server is a cluster-wide aggregator of resource usage data. It collects short-term metrics from every node and exposes them via the API. This is what enables the kubectl top command and, more importantly, what allows the Horizontal Pod Autoscaler (HPA) to function.
In this lesson, we will learn how to install the Metrics Server, master the top commands, and learn to differentiate between "Actual Usage" and "Reserved Resource" to optimize our cluster's health.
1. What is the Metrics Server? (The Aggregator)
By default, Kubernetes does NOT come with a long-term data store for metrics. If you want a dashboard showing your CPU usage over the last 30 days, you need Prometheus (Lesson 9.2).
The Metrics Server is built for the Right Now. It stores usage data in memory for a window of about 60 seconds. It is designed to be lightweight and fast, providing a "Snapshot" of the cluster's vital signs.
2. Installing the Metrics Server
If you are on a local cluster (like Minikube) or some cloud providers, the Metrics Server might not be installed.
# Apply the official manifest
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
Once installed, wait 60 seconds for the first sampling window to complete. You can verify it's working with:
kubectl top nodes
3. Mastering 'kubectl top'
There are two primary commands you will use daily as a K8s Operator.
A. kubectl top nodes
Shows you the health of the physical (or virtual) servers.
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
node-1 544m 13% 2936Mi 39%
node-2 1200m 30% 4000Mi 50%
B. kubectl top pods
Shows you which of your applications is the "Noisy Neighbor."
NAME CPU(cores) MEMORY(bytes)
ai-agent-v1-abc 800m 1500Mi
nextjs-frontend-v1-xyz 50m 128Mi
4. The "Usage vs. Requests" Trap
A common point of confusion is looking at kubectl top and seeing low usage, but getting "Insufficient CPU" errors when trying to start new pods.
- Usage (Measured by top): What the container is physically using right now.
- Requests (Defined in YAML): What the container has "Reserved."
Analogy: You book a hotel room. You are "Requesting" 100% of the room. Even if you are outside sightseeing (0% "Usage"), the hotel cannot give that room to anyone else. If your cluster's total Requests hit 100%, the cluster is "Full," even if the actual Usage is only 10%. This is called Slack.
5. Visualizing the Metrics Flow
graph TD
subgraph "Worker Node"
C1["Pod A"]
C2["Pod B"]
K["Kubelet (cAdvisor)"]
end
C1 & C2 -- "Stats" --> K
K -- "Expose /metrics" --> MS["Metrics Server (Aggregator)"]
MS -- "metrics.k8s.io API" --> API["K8s API Server"]
API -- "Query" --> CLI["kubectl top"]
API -- "Query" --> HPA["Autoscaler"]
6. Practical Example: Detecting a "Zombied" AI Model
Sometimes a Python process hangs. It uses 100% of one CPU core but isn't actually processing any requests.
How to find it:
- Run
kubectl top pods. - Find the pod using suspiciously high CPU (e.g.
1000mconstant). - Use
kubectl logsto see if anything is moving. - If the logs are silent but the CPU is at 100%, you have found a "Zombied" process.
- Action:
kubectl delete pod <name>. K8s will restart it, and hopefully, it comes back healthy.
7. AI Implementation: Monitoring GPU Power
If you are running NVIDIA GPUs, the standard Metrics Server will NOT show you GPU usage. To see your "GPU Vital Signs," you need the NVIDIA Device Plugin and the NVIDIA Data Center GPU Manager (DCGM).
This allows you to see:
- GPU Memory Usage: Is your model too big for the VRAM?
- GPU Temp: Is the server overheating during training?
- GPU Utilization: Are you successfully feeding data fast enough to keep the GPU busy?
For a professional AI cluster, these metrics are more important than standard CPU/RAM.
8. Summary and Key Takeaways
- Metrics Server: The realtime monitor for the cluster.
- kubectl top: The command-line tool for seeing usage.
- API Integration: Metrics Server is required for the HPA to work.
- Usage vs Requests: Usage is what's happening; Requests are what's reserved.
- Slack: The gap between usage and reservation (where your money is wasted).
In the next lesson, we will move from "Real-time" to "Historical" data with Prometheus and Grafana.
9. SEO Metadata & Keywords
Focus Keywords: Kubernetes Metrics Server tutorial, kubectl top pods usage empty, K8s CPU utilization vs requests, monitoring K8s resource usage CLI, troubleshoot Kubernetes high CPU usage, Metrics Server installation guide.
Meta Description: Master the basic observability of Kubernetes. Learn how to install and use the Metrics Server and 'kubectl top' to monitor the real-time health of your pods and nodes, and understand the difference between actual usage and resource reservations.