Skip to content

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

FactorImpactOptimization
Compute60-80% of costRight-size, Spot VMs, CUDs
Storage10-20% of costRight-size PVs, lifecycle policies
Networking5-15% of costOptimize egress, use internal LBs
ManagementFixed per clusterConsolidate 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 only
bash
# 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: 256Mi

Resource Request Guidelines

ResourceRecommendation
CPU RequestP95 usage + 20% buffer
Memory RequestP99 usage + 10% buffer
CPU Limit2-4x request or unset
Memory LimitEqual 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-4

Schedule 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: NoSchedule

Spot VM Best Practices

PracticeDescription
Use for batch jobsJobs can handle interruption
Set Pod disruption budgetsGraceful handling
Enable preemption handlingHandle SIGTERM
Mix with on-demandFallback for critical workloads

Committed Use Discounts (CUDs)

Save up to 57% with 1-3 year commitments.

CUD Recommendations

ScenarioRecommendation
Stable baselineCommit to baseline usage
Variable workloadsCombine CUD + Spot VMs
Multi-regionRegional 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 demand

Machine Type Selection

Cost-Effective Machine Types

Machine Type Comparison

SeriesCostBest For
E2LowestGeneral purpose, dev/test
N2MediumProduction workloads
N2DMediumAMD-based, cost-effective
C2HigherCompute-intensive
C2DHigherHigh single-thread performance

Shared-Core Options

TypevCPUMemoryUse Case
e2-micro0.251 GBTiny services
e2-small0.52 GBLight workloads
e2-medium14 GBSmall 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: 60

Cluster Autoscaler Profile

bash
# Aggressive scale-down for cost
gcloud container clusters update my-cluster \
    --autoscaling-profile=optimize-utilization

Storage Cost Optimization

Storage Type Selection

Storage Best Practices

PracticeSavings
Use pd-standard for cold data60-75% vs SSD
Right-size PVCsPay only for what you use
Delete unused PVCsEliminate waste
Use regional PD selectively2x 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 namespace

Network Cost Optimization

Egress Costs

Minimize Egress Costs

StrategyImplementation
Co-locate servicesSame zone node affinity
Use internal LBsFor internal traffic
Cache at edgeCloud CDN for static content
Compress dataReduce transfer size

Pod Anti-Affinity for Zone Spread

yaml
spec:
  affinity:
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        podAffinityTerm:
          topologyKey: topology.kubernetes.io/zone

Cost 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=production

Label Standards

LabelPurposeExample
cost-centerDepartment allocationengineering
environmentDev/staging/prodproduction
teamTeam ownershipplatform
appApplication nameapi-gateway

Cost Optimization Checklist

Immediate Actions

ActionPotential Savings
Right-size resource requests20-40%
Use Spot VMs for batch60-91%
Delete idle resourcesVariable
Use E2 machine types10-30%

Medium-Term Actions

ActionPotential Savings
Implement autoscaling30-50%
Committed use discounts37-57%
Optimize storage classes20-40%
Network optimization10-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

ResourceMonthly 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

Built with VitePress & Mermaid diagrams