Cloud Intermediate
Kubernetes Cost Optimization¶
KubernetesFinOpsCost OptimizationCloud 6 min read
Kubernetes cost optimization. Right-sizing, spot instances, resource quotas and cost visibility tools.
Where the Money Goes¶
A typical K8s cluster uses only 20-40% of allocated resources.
- Over-provisioning — resource requests set too high
- No limits — workloads consume more than they need
- Idle resources — dev/staging clusters running 24/7
- Wrong instance types — overly expensive VMs
Right-sizing with VPA¶
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: api-vpa
spec:
targetRef:
apiVersion: apps/v1
kind: Deployment
name: api-server
updatePolicy:
updateMode: "Off"
resourcePolicy:
containerPolicies:
- containerName: api
minAllowed:
cpu: 50m
memory: 64Mi
maxAllowed:
cpu: 2
memory: 4Gi
Spot Instances¶
Spot/preemptible instances offer 60-90% discounts. Use them for stateless and batch workloads.
spec:
tolerations:
- key: kubernetes.azure.com/scalesetpriority
operator: Equal
value: spot
effect: NoSchedule
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
preference:
matchExpressions:
- key: kubernetes.azure.com/scalesetpriority
operator: In
values: [spot]
Tools¶
- Kubecost — cost allocation per namespace/label/team
- OpenCost — open-source cost monitoring (CNCF)
- Goldilocks — VPA recommendations for an entire namespace
- Karpenter — intelligent node provisioning (AWS)
Summary¶
K8s cost optimization is a continuous process. Right-sizing, spot instances, resource quotas and cost visibility tools can reduce costs by 40-60%.
Need Help with Implementation?¶
Our team has experience designing and implementing modern architectures. We’re happy to help.