Introduction
In our last lesson, we established the theoretical foundation for running applications reliably on Kubernetes by defining resource requests and limits. We concluded that the only way to set these values correctly is to first measure how your application behaves under a realistic load.
This lesson transitions from theory to practice. We will cover the primary kubectl commands for doing exactly that. You will learn to use kubectl top to get a real-time snapshot of your pod's resource consumption and kubectl describe to inspect its configuration and event history.
Mastering these two commands provides the data-gathering toolkit essential for "right-sizing" your application. This is a crucial step before we can confidently configure the autoscaling necessary to handle your Pub/Sub message backlog.
kubectl top: Your Real-Time Resource Monitor
The kubectl top command is your window into the live resource consumption of your cluster's components. It provides a point-in-time snapshot of CPU and memory usage, which is invaluable for identifying immediate performance bottlenecks.
From your statistical background, you can think of kubectl top as taking a single sample from the time series of your application's resource usage.
Prerequisite: The Metrics Server
For kubectl top to function, a cluster add-on called the Metrics Server must be running. It collects resource metrics from each node's kubelet and exposes them through the Kubernetes Metrics API, which kubectl top then queries.
Most managed Kubernetes services, including GKE, install the Metrics Server by default. The following article from Datadog explains this prerequisite and shows you how to check if it's running.
Collecting metrics with built-in Kubernetes monitoring tools
Please read this short section from the article 'Collecting metrics with built-in Kubernetes monitoring tools' by Datadog to understand the role of the Metrics Server.
Read the section titled 'First things first: Deploy Metrics Server'. You don't need to install it, as GKE manages this, but it's important to know what makes this command work.
Viewing Resource Usage
Once the Metrics Server is confirmed to be running, you can inspect resource usage at both the node and pod level. The Last9 blog provides an excellent, concise guide to these commands and how to interpret their output.
Monitoring Kubernetes Resource Usage with kubectl top - Last9
This article, 'Monitoring Kubernetes Resource Usage with kubectl top', will be our main guide. Let's start with the basics of the command and how to read its output.
Read the sections 'What Is kubectl top?', 'How to Use kubectl top', and 'Interpreting kubectl top Output'. Pay attention to the units: CPU is in 'millicores' (m) and memory is in 'mebibytes' (Mi) or 'gibibytes' (Gi).
The key commands are:
kubectl top nodes: Shows resource usage for all nodes in the cluster.kubectl top pods: Shows resource usage for pods in the current namespace.kubectl top pods -n <namespace>: Shows usage for pods in a specific namespace.kubectl top pod <pod-name> --containers: Drills down to show usage for each container within a single pod. This is especially useful for pods with sidecars.
kubectl describe: Inspecting Configuration and Events
While kubectl top shows you what's happening right now, kubectl describe tells you the configuration and history of a resource. For our purposes, it's the perfect counterpart to kubectl top. It allows you to see the requests and limits you configured in your Deployment YAML and view a log of important events that have affected the pod.
The most critical distinction is:
kubectl topshows actual, real-time usage.kubectl describeshows allocated/configured resources (requestsandlimits).
The Datadog article you looked at earlier explains this difference very clearly.
Collecting metrics with built-in Kubernetes monitoring tools
Let's return to the Datadog article to see how kubectl describe complements kubectl top.
Read the section 'Query resource allocations with kubectl describe'. Notice how the output explicitly shows the 'Requests' and 'Limits' for each container in the pod.
The two most valuable sections in the kubectl describe pod <pod-name> output for our diagnosis are:
- Containers: Under each container, you'll find the
RequestsandLimitsfor CPU and memory. - Events: At the very bottom, you'll find a chronological list of events. This is where you would see if a pod was
OOMKilledfor exceeding its memory limit or failed to schedule due to insufficient resources.
This short video demonstrates how to use the command for general troubleshooting.
Important Kubernetes kubectl Command with Examples in 20 minutes!
This clip from the video 'Important Kubernetes kubectl Command with Examples' shows a practical example of using kubectl describe to debug a pod.
Watch from 08:59 to 10:34. The example shows debugging an image pull error, but the key takeaway is how the 'Events' section provides a clear, human-readable reason for the pod's failure.
The Diagnostic Workflow: Combining top and describe
The real power comes from using these two commands together. This workflow is central to diagnosing resource-related issues and right-sizing your applications.
Let's apply this to your situation: you've observed your service has high memory usage. Here is how you would investigate:
- Get live data: Run
kubectl top pod <your-service-pod-name>. You see that memory usage is, for example,480Mi. - Check the configuration: Run
kubectl describe pod <your-service-pod-name>. You look at theLimitssection and see that the memory limit is512Mi. - Synthesize: You can now conclude that your pod is operating at over 93% of its memory limit. This is a high-risk situation. Any small spike in message processing could push it over the limit and cause an
OOMKill. - Check history: In the same
describeoutput, you scan theEventstable. If you see recentOOMKilledevents, you have confirmed that the pod is unstable due to insufficient memory limits.
This process transforms a vague observation ("high memory usage") into a concrete, actionable insight ("the pod is at risk of OOMKill because its usage is too close to its limit").
The Last9 article details exactly this process of comparing usage with requests and limits.
Monitoring Kubernetes Resource Usage with kubectl top - Last9
Let's revisit the Last9 blog post to see this diagnostic workflow in action.
Read the sections 'Comparing Usage with Requests and Limits' and 'Detecting Performance Issues Using kubectl top'. This directly addresses how to use these tools to find and fix issues like high memory usage.
Advanced Tips for Practical Monitoring
For a dynamic environment, a single snapshot is often not enough. To get a better feel for resource consumption over time, you can combine kubectl top with the standard Unix watch command.
watch -n 5 kubectl top pods -n <your-namespace>
This command will re-run kubectl top pods every 5 seconds, giving you a simple, real-time dashboard in your terminal. This is extremely useful for observing how resource usage changes as your application processes a workload.
Additionally, you can use standard shell commands to sort the output and quickly identify the most resource-intensive pods in a busy namespace.
kubectl top pods --all-namespaces | sort -k3 -nr | head -10
This command sorts all pods across all namespaces by the third column (CPU) in numeric, reverse order (-nr) and shows the top 10. To sort by memory, you would use -k4. Given your comfort with the command line, these kinds of compositions will likely feel natural.
Conclusion
In this lesson, you've learned how to use two of the most fundamental kubectl commands for operational insight.
Key Takeaways:
kubectl topprovides a real-time snapshot of actual CPU and memory usage, powered by the Metrics Server.kubectl describeprovides a detailed static view of a resource's configuration, including itsrequestsandlimits, and a history of significantEvents.- The core diagnostic workflow is to compare the live usage from
kubectl topwith the configuredlimitsfromkubectl describeto assess the risk of CPU throttling or memory OOMKills. - The
Eventssection of thedescribeoutput is your first stop for debugging pod lifecycle issues.
You now have the tools to measure your subscriber service's baseline performance, which will allow you to confidently set its resource requests and limits.
With a stable, well-configured pod "template," we are finally ready to tackle scaling. In the next lesson, we will dive into the Horizontal Pod Autoscaler (HPA), the Kubernetes component that automatically adjusts the number of replicas based on observed metrics—including the custom Pub/Sub metrics you need.
Can't find a good explanation? Sign up and we'll make it for you
Sign up