Is kubectl top the current memory / CPU value?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In Kubernetes environments, resource monitoring and management are crucial for maintaining efficient and effective deployments. The `kubectl` command-line interface provides several tools for administrators to achieve this, including the `kubectl top` command. This article will delve into how `kubectl top` can be used to retrieve real-time memory and CPU usage metrics for nodes and pods in a Kubernetes cluster.
Overview of `kubectl top`
`kubectl top` is a command used in Kubernetes to show usage statistics for node or pod resources. It provides a way to query metrics directly from a Kubernetes cluster using the command line, thus enabling administrators to assess resource utilization and make informed decisions about resource allocation and scaling.
The `kubectl top` command can be broken down into two primary functions:
- Fetching Node Metrics: View the current CPU and memory usage across all nodes.
- Fetching Pod Metrics: Examine the current CPU and memory usage for individual pods within a namespace or across all namespaces.
Technical Explanation
Cluster Metrics System
Before `kubectl top` can function, a metrics server must be running in your cluster. The metrics server is responsible for gathering metrics from Kubelets on nodes and storing them temporarily in memory, making these metrics accessible via the Kubernetes API.
Architecture:
- Kubelet: This is a node agent that facilitates the reporting of resource usage statistics.
- Metrics Server: The API-compatible replacement for `heapster`. It aggregates metrics from all Kubelets and makes them accessible.
Command Syntax and Examples
Viewing Node Metrics
The command for displaying node metrics is as follows:
- CPU(cores) and MEMORY(bytes): These reflect the total current usage of CPU and memory on each node.
- CPU% and MEMORY%: These show the usage as a percentage of the node's total capacity.
- Performance Monitoring: Continuously check resource consumption and ensure applications do not exceed predefined limits.
- Resource Planning: Use historical data for capacity planning, reducing resource wastage, and avoiding downtime.
- Troubleshooting: Quickly identify CPU or memory bottlenecks affecting application performance.
- Ensure the metrics server is deployed: If you receive errors related to fetching data, validate that the metrics server is installed and running.
- Review Role-Based Access Control (RBAC) settings: `kubectl top` may require specific permissions to access metrics.
Related reading
- Is Kubernetes emptyDir different from no volume at all
- Is Kubernetes local/csi PV content synced into a new node?
- Is there a concept of inheritance for Kubernetes deployments?
- Is there a 'max-retries' for Kubernetes Jobs?
- Is there a simple way to export the data from a meteor deployed app?
- Is there a way in Kubernetes to check when hpa happened?
- Is there a way in kubectl patch to delete a specific object in an array without specifying the index?
- Is there a way to add arbitrary records to kube-dns?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.