Bird
Raised Fist0
Kubernetesdevops~5 mins

Resource monitoring best practices in Kubernetes - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Monitoring resources in Kubernetes helps you keep your applications healthy and running smoothly. It shows you how much CPU and memory your apps use, so you can fix problems before they get big.
When you want to know if your app is using too much CPU or memory and might slow down.
When you need to find out why your app is crashing or restarting often.
When you want to plan for more servers because your app is growing.
When you want to see if your app is balanced well across your servers.
When you want to get alerts if your app's resource use is too high.
Commands
This command shows the current CPU and memory usage of all pods in the default namespace. It helps you see which pods use the most resources.
Terminal
kubectl top pods
Expected OutputExpected
NAME CPU(cores) MEMORY(bytes) my-app-12345 50m 100Mi my-db-67890 30m 200Mi
This command shows the CPU and memory usage of each node in your cluster. It helps you understand the overall resource use on your servers.
Terminal
kubectl top nodes
Expected OutputExpected
NAME CPU(cores) MEMORY(bytes) node-1 500m 2Gi node-2 300m 1.5Gi
This command gives detailed information about a specific pod, including its resource requests and limits. It helps you check if the pod has proper resource settings.
Terminal
kubectl describe pod my-app-12345
Expected OutputExpected
Name: my-app-12345 Namespace: default Containers: my-app: Requests: cpu: 100m memory: 200Mi Limits: cpu: 200m memory: 400Mi Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 2m default-scheduler Successfully assigned default/my-app-12345 to node-1
This command lists Horizontal Pod Autoscalers, which automatically adjust the number of pods based on resource use. It helps you see if autoscaling is set up.
Terminal
kubectl get hpa
Expected OutputExpected
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE my-app-hpa Deployment/my-app 50%/80% 1 5 2 10m
Key Concept

If you remember nothing else from resource monitoring, remember: always check both current usage and resource limits to keep your apps healthy and efficient.

Common Mistakes
Not setting resource requests and limits in pod specs.
Without these, Kubernetes can't manage resources well, leading to crashes or slow apps.
Always define CPU and memory requests and limits in your pod configuration.
Ignoring node resource usage and focusing only on pods.
Nodes can become overloaded even if individual pods look fine, causing cluster issues.
Monitor both pod and node resource usage regularly.
Not using autoscaling to adjust pod counts based on load.
Without autoscaling, your app may be underpowered during traffic spikes or waste resources when idle.
Set up Horizontal Pod Autoscalers to scale pods automatically based on CPU or memory use.
Summary
Use 'kubectl top pods' and 'kubectl top nodes' to see current resource usage.
Check pod resource requests and limits with 'kubectl describe pod'.
Use 'kubectl get hpa' to verify autoscaling is configured.
Set resource requests and limits to help Kubernetes manage your apps well.

Practice

(1/5)
1. Why is it important to set resource requests and limits in Kubernetes pods?
easy
A. To ensure pods get the resources they need and prevent resource conflicts
B. To make pods run slower and use more CPU
C. To disable monitoring tools automatically
D. To allow unlimited resource usage without restrictions

Solution

  1. Step 1: Understand resource requests and limits

    Resource requests define the minimum resources a pod needs, and limits set the maximum it can use.
  2. Step 2: Recognize the effect on cluster stability

    Setting these prevents pods from using too many resources and causing conflicts or crashes.
  3. Final Answer:

    To ensure pods get the resources they need and prevent resource conflicts -> Option A
  4. Quick Check:

    Resource requests and limits = prevent conflicts [OK]
Hint: Requests = minimum, limits = maximum resources [OK]
Common Mistakes:
  • Thinking limits slow down pods intentionally
  • Believing requests disable monitoring
  • Assuming unlimited usage is safe
2. Which command correctly shows current CPU and memory usage of pods in Kubernetes?
easy
A. kubectl monitor pods
B. kubectl get pods --usage
C. kubectl top pods
D. kubectl describe pods --metrics

Solution

  1. Step 1: Identify the command for resource usage

    The kubectl top pods command shows CPU and memory usage of pods.
  2. Step 2: Check other options for correctness

    Other commands are invalid or do not show usage metrics.
  3. Final Answer:

    kubectl top pods -> Option C
  4. Quick Check:

    Usage command = kubectl top pods [OK]
Hint: Use 'kubectl top pods' to see pod resource usage [OK]
Common Mistakes:
  • Using 'kubectl get pods --usage' which is invalid
  • Confusing 'describe' with usage metrics
  • Assuming 'kubectl monitor' is a valid command
3. Given this command output:
NAME          CPU(cores)   MEMORY(bytes)
myapp-pod-1   150m         200Mi
myapp-pod-2   300m         400Mi

What is the total CPU usage of both pods?
medium
A. 300m
B. 450m
C. 150m
D. 600m

Solution

  1. Step 1: Add CPU usage values from both pods

    150m + 300m = 450m CPU cores.
  2. Step 2: Confirm units and sum

    Both values are in millicores (m), so sum is 450m.
  3. Final Answer:

    450m -> Option B
  4. Quick Check:

    150m + 300m = 450m [OK]
Hint: Add CPU millicores values directly [OK]
Common Mistakes:
  • Adding memory values instead of CPU
  • Confusing 450m with 600m
  • Ignoring units and summing incorrectly
4. You set resource limits on a pod, but kubectl top pods shows usage exceeding those limits. What is the likely cause?
medium
A. The pod has no resource requests set
B. The pod is using burstable QoS and can exceed limits temporarily
C. Resource limits are not enforced by Kubernetes by default
D. The metrics server is not installed or reporting incorrect data

Solution

  1. Step 1: Understand resource limits enforcement

    Kubernetes enforces limits strictly; pods cannot exceed set limits.
  2. Step 2: Consider metrics server role

    If usage shows above limits, metrics server may be missing or reporting wrong data.
  3. Final Answer:

    The metrics server is not installed or reporting incorrect data -> Option D
  4. Quick Check:

    Incorrect metrics = wrong usage shown [OK]
Hint: Check metrics server if usage exceeds limits [OK]
Common Mistakes:
  • Thinking Kubernetes allows exceeding limits
  • Confusing QoS classes with limit enforcement
  • Ignoring metrics server installation
5. You want to monitor resource usage trends over time for your Kubernetes cluster. Which approach is best?
hard
A. Set resource requests and limits, then use a monitoring tool like Prometheus
B. Use kubectl top repeatedly and save output manually
C. Only set resource limits without monitoring tools
D. Rely on kubectl describe to check resource usage daily

Solution

  1. Step 1: Understand monitoring needs over time

    Manual commands show current usage but not trends or history.
  2. Step 2: Use monitoring tools with resource limits

    Setting requests/limits ensures stable usage; tools like Prometheus collect and visualize trends.
  3. Final Answer:

    Set resource requests and limits, then use a monitoring tool like Prometheus -> Option A
  4. Quick Check:

    Requests + monitoring tool = best practice [OK]
Hint: Combine limits with Prometheus for trend monitoring [OK]
Common Mistakes:
  • Relying on manual commands for long-term trends
  • Skipping resource requests or limits
  • Using describe command for usage stats