$npx -y skills add google/skills --skill gke-costOptimizes GKE costs, rightsizes workloads, and configures Spot VMs and CUDs. Use when optimizing GKE costs, rightsizing GKE workloads, or configuring GKE Spot VMs. Don't use for general compute class provisioning or GPU Selection (use gke-compute-classes instead).
| 1 | # GKE Cost Optimization |
| 2 | |
| 3 | This reference covers strategies for reducing GKE costs while maintaining the |
| 4 | golden path security and reliability posture. |
| 5 | |
| 6 | > **MCP Tools:** `get_k8s_resource`, `describe_k8s_resource`, |
| 7 | > `apply_k8s_manifest`, `patch_k8s_resource`, `get_cluster` |
| 8 | |
| 9 | ## Golden Path Cost Features |
| 10 | |
| 11 | The golden path already includes cost-optimizing settings: |
| 12 | |
| 13 | | Setting | Value | Impact | |
| 14 | | ------------------------ | ---------------------- | ----------------------- | |
| 15 | | `autoscalingProfile` | `OPTIMIZE_UTILIZATION` | Aggressive node | |
| 16 | : : : scale-down reduces idle : |
| 17 | : : : compute : |
| 18 | | `verticalPodAutoscaling` | `enabled` | VPA recommendations | |
| 19 | : : : prevent : |
| 20 | : : : over-provisioning : |
| 21 | | Autopilot pricing | Pay per pod request | No charge for unused | |
| 22 | : : : node capacity : |
| 23 | | Node Auto Provisioning | enabled | Right-sized node pools | |
| 24 | : : : created automatically : |
| 25 | |
| 26 | ## Cost Optimization Strategies |
| 27 | |
| 28 | ### 1. Spot VMs via ComputeClasses |
| 29 | |
| 30 | Use Spot VMs for fault-tolerant workloads (60-90% cost reduction). |
| 31 | |
| 32 | ```yaml |
| 33 | apiVersion: cloud.google.com/v1 |
| 34 | kind: ComputeClass |
| 35 | metadata: |
| 36 | name: spot-with-fallback |
| 37 | spec: |
| 38 | activeMigration: |
| 39 | optimizeRulePriority: true |
| 40 | priorities: |
| 41 | - machineFamily: n4 |
| 42 | spot: true |
| 43 | - machineFamily: n4 |
| 44 | spot: false |
| 45 | ``` |
| 46 | |
| 47 | **Spot-suitable workloads:** |
| 48 | |
| 49 | Workload | Spot-Suitable? |
| 50 | --------------------------------- | --------------- |
| 51 | Batch / data processing | Yes |
| 52 | Dev / test environments | Yes |
| 53 | Stateless web/API (replicas >= 2) | Yes (with PDBs) |
| 54 | Jobs with checkpointing | Yes |
| 55 | Stateful workloads (databases) | No |
| 56 | Single-replica critical services | No |
| 57 | |
| 58 | **Handling eviction:** |
| 59 | |
| 60 | ```yaml |
| 61 | spec: |
| 62 | template: |
| 63 | spec: |
| 64 | terminationGracePeriodSeconds: 25 # Must be < 30s for Spot |
| 65 | containers: |
| 66 | - name: app |
| 67 | lifecycle: |
| 68 | preStop: |
| 69 | exec: |
| 70 | command: ["/bin/sh", "-c", "sleep 5"] |
| 71 | ``` |
| 72 | |
| 73 | ### 1.1 Direct Workload Spot Selection (NodeSelector) |
| 74 | |
| 75 | For stateless or batch workloads in GKE Autopilot, you do not need to create a |
| 76 | custom ComputeClass to use Spot VMs. You can target the built-in Spot capacity |
| 77 | directly by adding a `nodeSelector` to your Pod or Deployment spec. |
| 78 | |
| 79 | > [!WARNING] **Preemption Warning**: Spot VMs are interruptible and can be |
| 80 | > preempted at any time with a 30-second notice. Workloads must be |
| 81 | > fault-tolerant and run with at least 2 replicas for high availability. Always |
| 82 | > explicitly warn users about this preemption risk when recommending Spot VMs. |
| 83 | |
| 84 | ```yaml |
| 85 | apiVersion: apps/v1 |
| 86 | kind: Deployment |
| 87 | metadata: |
| 88 | name: stateless-spot-app |
| 89 | spec: |
| 90 | replicas: 2 |
| 91 | template: |
| 92 | spec: |
| 93 | nodeSelector: |
| 94 | cloud.google.com/gke-provisioning: Spot |
| 95 | terminationGracePeriodSeconds: 25 # Must be < 30s to allow graceful shutdown before preemption |
| 96 | containers: |
| 97 | - name: app |
| 98 | image: <IMAGE> |
| 99 | ``` |
| 100 | |
| 101 | ### 2. Pod Rightsizing |
| 102 | |
| 103 | Use VPA recommendations to reduce over-provisioned requests. |
| 104 | |
| 105 | ```bash |
| 106 | # 1. Deploy VPA in recommendation mode |
| 107 | kubectl apply -f - <<EOF |
| 108 | apiVersion: autoscaling.k8s.io/v1 |
| 109 | kind: VerticalPodAutoscaler |
| 110 | metadata: |
| 111 | name: <DEPLOYMENT>-vpa |
| 112 | spec: |
| 113 | targetRef: |
| 114 | apiVersion: apps/v1 |
| 115 | kind: Deployment |
| 116 | name: <DEPLOYMENT> |
| 117 | updatePolicy: |
| 118 | updateMode: "Off" |
| 119 | EOF |
| 120 | |
| 121 | # 2. Wait 24+ hours for data collection |
| 122 | |
| 123 | # 3. Read recommendations |
| 124 | kubectl get vpa <DEPLOYMENT>-vpa -o jsonpath='{.status.recommendation}' |
| 125 | ``` |
| 126 | |
| 127 | **Optimization rules:** |
| 128 | |
| 129 | Condition | Action | Savings |
| 130 | ----------------------------- | ---------------------------------- | ------- |
| 131 | CPU request >5x P95 actual | Reduce to `P95 * 1.2` | High |
| 132 | Memory request >3x P95 actual | Reduce to `P95 * 1.2` | High |
| 133 | CPU request >2x P95 actual | Reduce to `P95 * 1.2` | Medium |
| 134 | No resource requests set | Add requests (enables bin-packing) | Medium |
| 135 | |
| 136 | ### 3. Machine Type Selection |
| 137 | |
| 138 | | Family | Use Case | Relative Cost | |
| 139 | | ------------- | -------------------------------------------- | ------------- | |
| 140 | | e2 | General purpose, burstable | Lowest | |
| 141 | | t2a / t2d | Scale-out (Arm/AMD), pri |