Kubernetes Cost Optimization
VPA, Spot nodes, Kubecost.
Kubernetes Cost Optimization
Kubernetes clusters are notoriously over-provisioned. Studies show that 60-70% of allocated cluster resources are unused. This guide covers practical strategies to right-size your cluster and reduce costs by 30-50% without sacrificing reliability.
Understanding the Cost Problem
Kubernetes costs come from three sources:
- Compute (70%) — EC2/VM instances for worker nodes
- Storage (15%) — EBS volumes, EFS file systems
- Networking (15%) — Load balancers, data transfer, NAT gateways
The primary waste comes from over-provisioned resource requests. Developers set CPU/memory requests once and never revisit them.
Right-Sizing with VPA Recommendations
Deploy Vertical Pod Autoscaler in recommendation mode:
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: api-vpa
spec:
targetRef:
apiVersion: apps/v1
kind: Deployment
name: api-service
updatePolicy:
updateMode: "Off"# Check recommendations
kubectl describe vpa api-vpa
# Typical output:
# Target: cpu=250m, memory=256Mi
# Lower Bound: cpu=100m, memory=128Mi
# Upper Bound: cpu=1000m, memory=512MiSpot Instances for Non-Critical Workloads
Use node selectors and tolerations to schedule workloads on Spot nodes:
spec:
template:
spec:
nodeSelector:
karpenter.sh/capacity-type: spot
tolerations:
- key: karpenter.sh/capacity-type
operator: Equal
value: spot
effect: NoSchedule
containers:
- name: worker
resources:
requests:
cpu: 500m
memory: 512MiSpot instances save 60-90% on compute but can be interrupted. Use them for:
- Stateless web servers with multiple replicas
- Background job workers
- CI/CD build agents
- Development/staging environments
Kubecost for Visibility
Kubecost provides per-namespace, per-deployment, and per-label cost breakdowns:
helm install kubecost kubecost/cost-analyzer \
--namespace kubecost \
--create-namespace \
--set kubecostToken="YOUR_TOKEN"Key Kubecost insights:
- Idle costs — Resources allocated but not used
- Right-sizing recommendations — Based on actual usage
- Namespace cost allocation — Chargeback to teams
- Savings opportunities — Ranked by potential impact
Cluster Consolidation
Karpenter's consolidation feature automatically replaces underutilized nodes:
spec:
disruption:
consolidationPolicy: WhenUnderutilized
consolidateAfter: 30sWhen pods are removed and a node becomes underutilized, Karpenter will reschedule pods to other nodes and terminate the empty node.
Quick Wins Checklist
- Deploy VPA in recommendation mode — Find over-provisioned workloads
- Use Spot for stateless services — 60-90% compute savings
- Right-size node instance types — Use Graviton for 40% better price/performance
- Enable Karpenter consolidation — Automatic bin-packing
- Schedule dev/staging shutdowns — Scale to zero outside business hours
- Set resource requests accurately — Base on P95 usage, not guesses
- Delete unused PVCs — Orphaned volumes cost money
- Review load balancer count — Consolidate with shared Ingress
Eazy SaaS Tip: We run quarterly Kubernetes cost audits for our clients using Kubecost data combined with AWS Cost Explorer. The average client saves $2,000-5,000/month from right-sizing and Spot adoption alone.