Appearance
GKE Cost Optimization Guide
Complete guide to optimizing costs in Google Kubernetes Engine (GKE), covering resource management, Spot VMs, committed use discounts, and best practices.
Cost Optimization Overview
Cost Factors in GKE
| Factor | Impact | Optimization |
|---|---|---|
| Compute | 60-80% of cost | Right-size, Spot VMs, CUDs |
| Storage | 10-20% of cost | Right-size PVs, lifecycle policies |
| Networking | 5-15% of cost | Optimize egress, use internal LBs |
| Management | Fixed per cluster | Consolidate workloads |
Right-Sizing Resources
Resource Requests vs Usage
Use VPA Recommendations
yaml
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: my-app-vpa
spec:
targetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
updatePolicy:
updateMode: "Off" # Recommendations onlybash
# View recommendations
kubectl describe vpa my-app-vpa
# Example output:
# Recommendation:
# Container Recommendations:
# Container Name: app
# Lower Bound: Cpu: 25m, Memory: 64Mi
# Target: Cpu: 50m, Memory: 128Mi
# Upper Bound: Cpu: 100m, Memory: 256MiResource Request Guidelines
| Resource | Recommendation |
|---|---|
| CPU Request | P95 usage + 20% buffer |
| Memory Request | P99 usage + 10% buffer |
| CPU Limit | 2-4x request or unset |
| Memory Limit | Equal to request |
Spot VMs
Up to 91% discount for interruptible workloads.
Create Spot VM Pool
bash
gcloud container node-pools create spot-pool \
--cluster=my-cluster \
--spot \
--enable-autoscaling \
--min-nodes=0 \
--max-nodes=100 \
--machine-type=e2-standard-4Schedule on Spot VMs
yaml
spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
preference:
matchExpressions:
- key: cloud.google.com/gke-spot
operator: In
values:
- "true"
tolerations:
- key: cloud.google.com/gke-spot
operator: Equal
value: "true"
effect: NoScheduleSpot VM Best Practices
| Practice | Description |
|---|---|
| Use for batch jobs | Jobs can handle interruption |
| Set Pod disruption budgets | Graceful handling |
| Enable preemption handling | Handle SIGTERM |
| Mix with on-demand | Fallback for critical workloads |
Committed Use Discounts (CUDs)
Save up to 57% with 1-3 year commitments.
CUD Recommendations
| Scenario | Recommendation |
|---|---|
| Stable baseline | Commit to baseline usage |
| Variable workloads | Combine CUD + Spot VMs |
| Multi-region | Regional CUDs for flexibility |
Calculate CUD Coverage
bash
# View current usage
gcloud compute resource-policies list
# Recommended: Commit to 60-70% of baseline
# Use Spot VMs for variable demandMachine Type Selection
Cost-Effective Machine Types
Machine Type Comparison
| Series | Cost | Best For |
|---|---|---|
| E2 | Lowest | General purpose, dev/test |
| N2 | Medium | Production workloads |
| N2D | Medium | AMD-based, cost-effective |
| C2 | Higher | Compute-intensive |
| C2D | Higher | High single-thread performance |
Shared-Core Options
| Type | vCPU | Memory | Use Case |
|---|---|---|---|
| e2-micro | 0.25 | 1 GB | Tiny services |
| e2-small | 0.5 | 2 GB | Light workloads |
| e2-medium | 1 | 4 GB | Small apps |
Autoscaling for Cost
Scale to Zero
Configure HPA for Cost
yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: my-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
minReplicas: 0 # Scale to zero!
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 50
periodSeconds: 60Cluster Autoscaler Profile
bash
# Aggressive scale-down for cost
gcloud container clusters update my-cluster \
--autoscaling-profile=optimize-utilizationStorage Cost Optimization
Storage Type Selection
Storage Best Practices
| Practice | Savings |
|---|---|
| Use pd-standard for cold data | 60-75% vs SSD |
| Right-size PVCs | Pay only for what you use |
| Delete unused PVCs | Eliminate waste |
| Use regional PD selectively | 2x cost for HA |
Clean Up Unused Storage
bash
# Find unbound PVCs
kubectl get pvc -A | grep -v Bound
# Find orphaned PVs
kubectl get pv | grep Released
# Delete unused PVC
kubectl delete pvc unused-pvc -n namespaceNetwork Cost Optimization
Egress Costs
Minimize Egress Costs
| Strategy | Implementation |
|---|---|
| Co-locate services | Same zone node affinity |
| Use internal LBs | For internal traffic |
| Cache at edge | Cloud CDN for static content |
| Compress data | Reduce transfer size |
Pod Anti-Affinity for Zone Spread
yaml
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
topologyKey: topology.kubernetes.io/zoneCost Visibility
GKE Cost Allocation
Enable Cost Allocation
bash
# Enable GKE usage metering
gcloud container clusters update my-cluster \
--resource-usage-bigquery-dataset=gke_usage
# Add labels for cost tracking
gcloud container node-pools update my-pool \
--cluster=my-cluster \
--node-labels=cost-center=engineering,environment=productionLabel Standards
| Label | Purpose | Example |
|---|---|---|
cost-center | Department allocation | engineering |
environment | Dev/staging/prod | production |
team | Team ownership | platform |
app | Application name | api-gateway |
Cost Optimization Checklist
Immediate Actions
| Action | Potential Savings |
|---|---|
| Right-size resource requests | 20-40% |
| Use Spot VMs for batch | 60-91% |
| Delete idle resources | Variable |
| Use E2 machine types | 10-30% |
Medium-Term Actions
| Action | Potential Savings |
|---|---|
| Implement autoscaling | 30-50% |
| Committed use discounts | 37-57% |
| Optimize storage classes | 20-40% |
| Network optimization | 10-20% |
Ongoing Optimization
Quick Reference
Cost Commands
bash
# View node pool machine types
gcloud container node-pools describe POOL --cluster=CLUSTER \
--format="value(config.machineType)"
# List Spot VM nodes
kubectl get nodes -l cloud.google.com/gke-spot=true
# View resource usage
kubectl top pods -A --sort-by=cpu
kubectl top nodes
# Find over-provisioned pods
kubectl describe vpa -A | grep -A5 "Container Recommendations"Cost Estimation
| Resource | Monthly Cost (approx) |
|---|---|
| e2-standard-4 (on-demand) | ~$100 |
| e2-standard-4 (Spot) | ~$30 |
| e2-standard-4 (1yr CUD) | ~$63 |
| pd-balanced 100GB | ~$10 |
| pd-standard 100GB | ~$4 |
Last updated: December 2025