0
0
Kubernetesdevops~5 mins

Resource monitoring best practices in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
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.