--- name: gcp-gke-cost-optimization description: | Analyzes and optimizes Google Kubernetes Engine costs through right-sizing resources, comparing cluster modes, and implementing autoscaling strategies. Use when analyzing GKE spending, comparing Autopilot vs Standard billing models, configuring Spot VMs for batch workloads, right-sizing pod resources, setting up budget alerts, or tracking cost per service. Includes per-pod billing analysis and resource utilization optimization patterns. allowed-tools: - Bash - Read - Write - Glob --- # GKE Cost Optimization ## Purpose Reduce GKE spending while maintaining performance and reliability. This skill covers cost analysis, resource right-sizing, cluster mode comparison, and budget monitoring strategies. ## When to Use Use this skill when you need to: - Analyze GKE spending and identify cost optimization opportunities - Compare Autopilot vs Standard billing models for your workload - Right-size pod resource requests and limits - Configure Horizontal/Vertical Pod Autoscaling to reduce waste - Set up Spot VMs for batch or non-critical workloads - Create budget alerts and track cost per service - Optimize resource utilization Trigger phrases: "optimize GKE costs", "reduce Kubernetes spending", "right-size resources", "Autopilot vs Standard pricing", "GKE budget alerts" ## Table of Contents - [Purpose](#purpose) - [When to Use](#when-to-use) - [Quick Start](#quick-start) - [Instructions](#instructions) - [Step 1: Understand Your Billing Model](#step-1-understand-your-billing-model) - [Step 2: Analyze Current Resource Usage](#step-2-analyze-current-resource-usage) - [Step 3: Right-Size Pod Resources](#step-3-right-size-pod-resources) - [Step 4: Configure Horizontal Pod Autoscaling](#step-4-configure-horizontal-pod-autoscaling-hpa) - [Step 5: Use Spot VMs](#step-5-use-spot-vms-gke-standard-only) - [Step 6: Set Up Cost Monitoring](#step-6-set-up-cost-monitoring) - [Step 7: Analyze Cost by Namespace/Service](#step-7-analyze-cost-by-namespaceservice) - [Examples](#examples) - [Requirements](#requirements) - [See Also](#see-also) ## Quick Start Analyze and optimize costs in three steps: ```bash # 1. Check current resource usage kubectl top pods -n wtr-supplier-charges # 2. Analyze resource requests vs actual usage kubectl describe deployment supplier-charges-hub -n wtr-supplier-charges | grep -A 10 "resources:" # 3. Compare Autopilot vs Standard pricing for your workload # Autopilot: Pay per pod per second for requests # Standard: Pay per provisioned node per hour (regardless of usage) ``` ## Instructions ### Step 1: Understand Your Billing Model #### GKE Autopilot (Per-Pod Billing - Recommended) **Cost Calculation:** ``` Monthly Cost = (vCPU requests * $0.04 + Memory requests GB * $0.004 + Disk GB * $0.0001) * seconds per month ``` **Example for Supplier Charges Hub:** - 2 replicas, each requesting: 1 vCPU + 2 GB memory - Monthly cost ≈ (2 * 1 * $0.04 + 2 * 2 * $0.004) * 2.592M seconds ≈ $260 **Advantages:** - Pay only for what pods request (not what entire cluster capacity is) - No idle resource costs - Perfect for variable workloads (scales up/down automatically) - Up to 60% cheaper than Standard for typical workloads #### GKE Standard (Node Billing) **Cost Calculation:** ``` Monthly Cost = Number of nodes * Machine type hourly rate * 730 hours per month ``` **Example for Supplier Charges Hub:** - 3 `n2-standard-4` nodes (standard pool) = ~$400/month - Even if pods use only 30% of capacity, you pay for 100% **Advantages:** - Predictable costs (great for committed use discounts) - Full control over infrastructure - Better for stable, high-utilization workloads ### Step 2: Analyze Current Resource Usage Check if pods are over-provisioned: ```bash # View actual vs requested resources kubectl top pods -n wtr-supplier-charges -o wide # Compare to requests kubectl get pods -n wtr-supplier-charges -o jsonpath='{.items[*].spec.containers[*].resources.requests}' ``` **Analysis Questions:** - Are actual values significantly lower than requests? - Is memory usage consistently below 75% of limits? - Is CPU usage consistently below 70% of requests? **If Yes → Right-size resources (reduce requests)** ### Step 3: Right-Size Pod Resources Use Vertical Pod Autoscaler (VPA) recommendations: ```bash # Apply VPA to deployment cat <