How to Use Metrics Server in Kubernetes for Resource Monitoring
To use
metrics-server in Kubernetes, first deploy it to your cluster to collect resource usage data. Then use commands like kubectl top nodes or kubectl top pods to view CPU and memory metrics in real time.Syntax
The metrics-server is deployed as a Kubernetes add-on that collects resource metrics from nodes and pods. After deployment, you use kubectl top commands to query metrics.
kubectl top nodes: Shows CPU and memory usage for each node.kubectl top pods: Shows CPU and memory usage for pods in the current namespace.kubectl top pods --all-namespaces: Shows metrics for pods across all namespaces.
bash
kubectl top nodes kubectl top pods kubectl top pods --all-namespaces
Example
This example shows how to deploy the Metrics Server and then check resource usage of nodes and pods.
bash
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml # Wait a few moments for metrics-server to start kubectl top nodes kubectl top pods --all-namespaces
Output
NAME CPU(cores) MEMORY(bytes)
node-1 250m 512Mi
node-2 300m 600Mi
NAMESPACE NAME CPU(cores) MEMORY(bytes)
default my-app-pod-1 100m 200Mi
kube-system metrics-server-abcdef12345 5m 20Mi
Common Pitfalls
Common mistakes when using Metrics Server include:
- Not deploying Metrics Server before running
kubectl topcommands, which results in errors like "metrics API not available". - Metrics Server failing to start due to missing permissions or network issues.
- Using an outdated Metrics Server manifest that is incompatible with your Kubernetes version.
Always use the latest official Metrics Server manifest and check pod logs if metrics are missing.
bash
kubectl top nodes
# Error: metrics API not available
# Correct approach:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
kubectl top nodesQuick Reference
| Command | Description |
|---|---|
| kubectl apply -f | Deploy Metrics Server to the cluster |
| kubectl top nodes | Show CPU and memory usage of nodes |
| kubectl top pods | Show CPU and memory usage of pods in current namespace |
| kubectl top pods --all-namespaces | Show pod metrics across all namespaces |
| kubectl get deployment metrics-server -n kube-system | Check Metrics Server deployment status |
Key Takeaways
Deploy Metrics Server using the official manifest before querying metrics.
Use 'kubectl top nodes' and 'kubectl top pods' to view resource usage.
Ensure Metrics Server pods are running and have proper permissions.
Use the latest Metrics Server version compatible with your Kubernetes cluster.
Check Metrics Server logs if metrics are missing or commands fail.