đź’° Cost Optimization = Cloud Savings
Cloud costs can spiral. Smart resource management in Kubernetes can reduce cloud bills by 30-50%.
📝 Resource Optimization
# Right-Sizing Workloads
apiVersion: apps/v1
kind: Deployment
metadata:
name: optimized-app
spec:
template:
spec:
containers:
- name: app
image: myapp:latest
resources:
requests:
memory: "128Mi"
cpu: "50m"
limits:
memory: "256Mi"
cpu: "200m"
# QoS Classes
- Guaranteed: requests = limits (Best performance)
- Burstable: requests < limits (Balanced)
- BestEffort: no requests/limits (Cost-effective)
# Node Affinity for Spot Instances
apiVersion: v1
kind: Pod
spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
preference:
matchExpressions:
- key: node-type
operator: In
values:
- spot
# Tolerations for Spot Instances
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
tolerations:
- key: spot
operator: Equal
value: true
effect: NoSchedule
🎯 Cost Monitoring Tools
# Kubecost Installation
helm repo add kubecost https://kubecost.github.io/cost-analyzer/
helm install kubecost kubecost/cost-analyzer
# View Costs
kubectl cost namespace # Costs per namespace
kubectl cost deployment # Costs per deployment
kubectl cost pod # Costs per pod
kubectl cost node # Costs per node
# Resource Usage Dashboard
kubectl top nodes
kubectl top pods --sort-by=cpu
kubectl top pods --sort-by=memory
# VPA Recommendations (Recommendation Mode)
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: recommend-only-vpa
spec:
targetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
updatePolicy:
updateMode: Off
resourcePolicy:
containerPolicies:
- containerName: '*'
controlledResources: [cpu, memory]
đź’ˇ Cost Optimization Tips
- Use spot instances for non-critical workloads
- Implement cluster autoscaling
- Use namespaces for cost allocation
- Set resource quotas per namespace
- Regularly review resource requests/limits
"Cloud cost optimization is continuous. With the right Kubernetes tools, you can achieve significant savings while maintaining performance."
