Using kubectl top
allows you to monitor resource usage within Kubernetes clusters. This command provides insights into CPU and memory utilization across nodes or pods, aiding in performance optimization and resource allocation.
Here are several examples showcasing how to utilize kubectl top
:
Example 1: Monitoring Node CPU and Memory Usage
kubectl top node
This command displays CPU and memory usage for each node in the cluster. It helps administrators identify nodes under heavy load or those with available resources.
Example 2: Checking Pod CPU and Memory Usage
kubectl top pod
Executes the command to show CPU and memory usage for all pods in the default namespace. It’s useful for understanding the resource demands of deployed applications.
Example 3: Monitoring a Specific Pod
kubectl top pod nginx-pod
Displays CPU and memory usage specifically for the pod named “nginx-pod”. This targeted view assists in troubleshooting performance issues related to individual pods.
Example 4: Viewing Resource Usage Across All Namespaces
kubectl top pod --all-namespaces
Provides CPU and memory usage for all pods across all namespaces in the cluster. This is beneficial for cluster-wide resource management and monitoring.
Example 5: Monitoring Nodes Sorted by CPU Usage
kubectl top node --sort-by cpu
Sorts nodes based on CPU usage in descending order. This helps prioritize nodes needing attention due to high CPU consumption.
Example 6: Checking Pods Sorted by Memory Usage
kubectl top pod --sort-by=memory
Sorts pods based on memory usage, providing insights into which pods are consuming the most memory resources.
Example 7: Monitoring Specific Namespace Resources
kubectl top pod -n development
Limits the resource view to pods within the “development” namespace, aiding in namespace-specific resource management.
Example 8: Checking Node CPU Usage in Watch Mode
kubectl top node -w
Continuously monitors and updates node CPU and memory usage metrics. Useful for real-time monitoring of node performance.
Example 9: Checking Top Processes of a Pod
kubectl top pod nginx-pod --containers
Displays the top processes consuming CPU and memory within the pod “nginx-pod”. Helps pinpoint resource-intensive processes for optimization.
Example 10: Checking Node Allocatable Resources
kubectl top node --use-protocol-buffers
Uses protocol buffers for more efficient data transfer when querying node resource metrics. Suitable for large-scale clusters to reduce API latency.
To verify if the command executed successfully, follow these steps:
- Run the respective
kubectl top
command as per the example provided. - Observe the output in the terminal or command prompt. Ensure it shows the expected metrics such as CPU usage, memory usage, and optionally, top processes.
- If available, cross-check with Kubernetes dashboard or other monitoring tools for consistency.
Also check similar articles.
Access Cluster Information Using kubectl cluster-info
Managing Kubernetes Certificates with kubectl certificate
Implement Auto-Scaling in Kubernetes with kubectl autoscale
Scaling Kubernetes Deployments with kubectl scale
Manage Resource Rollouts with kubectl rollout
Discussion about this post