0
0
Kubernetesdevops~5 mins

Cost optimization in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Running applications on Kubernetes can use more resources than needed, which costs money. Cost optimization helps you use just the right amount of resources so you save money without hurting your apps.
When your Kubernetes cluster bills are higher than expected and you want to reduce costs.
When you want to avoid paying for unused CPU or memory in your app containers.
When you want to automatically adjust resources based on app demand to save money.
When you want to find and remove idle or unnecessary workloads in your cluster.
When you want to set limits so no app uses more resources than it should.
Commands
Check current CPU and memory usage of all nodes to see if any are underused.
Terminal
kubectl top nodes
Expected OutputExpected
NAME CPU(cores) MEMORY(bytes) worker-node-1 250m 512Mi worker-node-2 100m 256Mi worker-node-3 50m 128Mi
See resource usage of all pods to find which ones use too much or too little CPU and memory.
Terminal
kubectl top pods --all-namespaces
Expected OutputExpected
NAMESPACE NAME CPU(cores) MEMORY(bytes) default my-app-1 100m 200Mi default my-app-2 50m 150Mi kube-system coredns-558bd4d5db-7x9zq 10m 50Mi
--all-namespaces - Shows pods from all namespaces, not just the current one
Save the pod configuration to a file so you can edit resource requests and limits.
Terminal
kubectl get pod my-app-1 -o yaml > my-app-1.yaml
Expected OutputExpected
No output (command runs silently)
-o yaml - Outputs the pod details in YAML format
Apply the updated pod configuration with optimized resource requests and limits to save costs.
Terminal
kubectl apply -f my-app-1.yaml
Expected OutputExpected
pod/my-app-1 configured
Check if Horizontal Pod Autoscaler is set up to automatically adjust pod count based on load.
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 this pattern, remember: monitor your resource usage and set proper requests and limits to avoid paying for unused capacity.

Common Mistakes
Not setting resource requests and limits in pod specs.
Without limits, pods can use more resources than expected, causing higher costs and unstable clusters.
Always define CPU and memory requests and limits in your pod or deployment YAML files.
Ignoring idle or low-usage nodes and pods.
Idle resources waste money because you pay for capacity that is not used.
Regularly check resource usage with kubectl top and remove or scale down unused workloads.
Not using autoscaling features like Horizontal Pod Autoscaler.
Without autoscaling, you either over-provision (waste money) or under-provision (poor performance).
Set up autoscaling to adjust pod counts based on real demand automatically.
Summary
Use 'kubectl top' commands to monitor node and pod resource usage.
Edit pod resource requests and limits in YAML files to control how much CPU and memory each pod can use.
Apply changes with 'kubectl apply' to update running workloads.
Use Horizontal Pod Autoscaler to automatically scale pods based on load and save costs.