Monitoring Kubernetes resources is a critical task for maintaining cluster performance and ensuring efficient resource usage. Kubernetes provides a native tool, kubectl top
, which allows you to monitor resource usage (CPU and memory) of nodes and pods in real-time. This guide explains how to use kubectl top
effectively.
Step 1: Prerequisites
Before using the kubectl top
command, ensure the following:
- Metrics Server:
Kubernetes requires the Metrics Server to collect and provide resource metrics. Check if Metrics Server is installed:
kubectl get deployment -n kube-system
Look for metrics-server
in the output. If not installed, you can set it up by following the Metrics Server installation guide.
2. RBAC Permissions
Ensure your user or role has the necessary permissions to access metrics. You can define a ClusterRole as follows:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: view-metrics
rules:
- apiGroups: [""]
resources: ["pods", "nodes"]
verbs: ["get", "list", "watch"]
Step 2: Monitor Node Metrics
To view CPU and memory usage across all nodes in your cluster, run:
kubectl top nodes
Example Output:
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
node1 250m 5% 1Gi 25%
node2 300m 6% 1.5Gi 37%
This provides a quick overview of the cluster’s resource utilization.
Step 3: Monitor Pod Metrics
For a Specific Namespace: To see resource usage for pods within a namespace:
kubectl top pods -n <namespace>
For All Namespaces: To list resource usage across all namespaces:
kubectl top pods --all-namespaces
Example Output:
NAME CPU(cores) MEMORY(bytes)
nginx-pod 100m 200Mi
api-server-pod 500m 600Mi
Step 4: Monitor Specific Resources
Monitor a Specific Pod:
kubectl top pod <pod-name> -n <namespace>
Monitor a Specific Node:
kubectl top node <node-name>
ip-10-0-4-216.ap-south-1.compute.internal 574m 29% 4099Mi 57%
ip-10-0-5-109.ap-south-1.compute.internal 415m 21% 2978Mi 41%
ip-10-0-6-156.ap-south-1.compute.internal 615m 31% 6028Mi 84%
Step 5: Automate Monitoring with Continuous Updates
To monitor resource usage continuously, combine kubectl top
with the watch
command:
watch -n 5 kubectl top pods -n <namespace>
NAME CPU(cores) MEMORY(bytes)
aws-load-balancer-controller-76f7d9b8f7-c2bfr 4m 32Mi
aws-node-2bctx 4m 40Mi
aws-node-6hhpb 5m 35Mi
aws-node-ptdhg 6m 36Mi
This updates the output every 5 seconds.
Why Use kubectl top?
- Real-Time Monitoring: Provides immediate insights into resource usage.
- Native Integration: No need for additional tools to start monitoring.
- Quick Debugging: Ideal for troubleshooting resource contention issues.
For advanced monitoring, consider integrating tools like Prometheus and Grafana for detailed metrics visualization and alerting capabilities.
Conclusion
Using kubectl top
is a simple yet powerful way to monitor Kubernetes resources. It’s especially useful for quick checks and troubleshooting. For more comprehensive monitoring and visualization, pairing it with tools like Prometheus and Grafana is recommended. Start using kubectl top
today to keep your Kubernetes cluster running smoothly.