gke-cost

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

GKE Cost Optimization

GKE成本优化

This reference covers strategies for reducing GKE costs while maintaining the golden path security and reliability posture.
MCP Tools:
get_k8s_resource
,
describe_k8s_resource
,
apply_k8s_manifest
,
patch_k8s_resource
,
get_cluster
本参考文档涵盖了在保持黄金路径安全与可靠性的前提下降低GKE成本的策略。
MCP工具:
get_k8s_resource
describe_k8s_resource
apply_k8s_manifest
patch_k8s_resource
get_cluster

Golden Path Cost Features

黄金路径成本优化特性

The golden path already includes cost-optimizing settings:
SettingValueImpact
autoscalingProfile
OPTIMIZE_UTILIZATION
Aggressive node
: : : scale-down reduces idle :
: : : compute :
verticalPodAutoscaling
enabled
VPA recommendations
: : : prevent :
: : : over-provisioning :
Autopilot pricingPay per pod requestNo charge for unused
: : : node capacity :
Node Auto ProvisioningenabledRight-sized node pools
: : : created automatically :
黄金路径已内置以下成本优化配置:
配置项取值影响
autoscalingProfile
OPTIMIZE_UTILIZATION
主动缩容减少闲置计算资源
verticalPodAutoscaling
enabled
VPA建议避免过度配置资源
Autopilot定价模式按Pod请求量付费闲置节点容量不收费
Node Auto Provisioningenabled自动创建规格适配的节点池

Cost Optimization Strategies

成本优化策略

1. Spot VMs via ComputeClasses

1. 通过ComputeClass使用Spot VM

Use Spot VMs for fault-tolerant workloads (60-90% cost reduction).
yaml
apiVersion: cloud.google.com/v1
kind: ComputeClass
metadata:
  name: spot-with-fallback
spec:
  activeMigration:
    optimizeRulePriority: true
  priorities:
  - machineFamily: n4
    spot: true
  - machineFamily: n4
    spot: false
Spot-suitable workloads:
WorkloadSpot-Suitable?
Batch / data processingYes
Dev / test environmentsYes
Stateless web/API (replicas >= 2)Yes (with PDBs)
Jobs with checkpointingYes
Stateful workloads (databases)No
Single-replica critical servicesNo
Handling eviction:
yaml
spec:
  template:
    spec:
      terminationGracePeriodSeconds: 25  # Must be < 30s for Spot
      containers:
      - name: app
        lifecycle:
          preStop:
            exec:
              command: ["/bin/sh", "-c", "sleep 5"]
为容错型工作负载使用Spot VM(可降低60-90%成本)。
yaml
apiVersion: cloud.google.com/v1
kind: ComputeClass
metadata:
  name: spot-with-fallback
spec:
  activeMigration:
    optimizeRulePriority: true
  priorities:
  - machineFamily: n4
    spot: true
  - machineFamily: n4
    spot: false
适合Spot VM的工作负载:
工作负载类型是否适合Spot VM
批量/数据处理任务
开发/测试环境
无状态Web/API(副本数≥2)是(需配置PDB)
带检查点的任务
有状态工作负载(数据库)
单副本核心服务
处理驱逐场景:
yaml
spec:
  template:
    spec:
      terminationGracePeriodSeconds: 25  # Spot VM场景下必须小于30秒
      containers:
      - name: app
        lifecycle:
          preStop:
            exec:
              command: ["/bin/sh", "-c", "sleep 5"]

1.1 Direct Workload Spot Selection (NodeSelector)

1.1 直接为工作负载选择Spot VM(NodeSelector)

For stateless or batch workloads in GKE Autopilot, you do not need to create a custom ComputeClass to use Spot VMs. You can target the built-in Spot capacity directly by adding a
nodeSelector
to your Pod or Deployment spec.
[!WARNING] Preemption Warning: Spot VMs are interruptible and can be preempted at any time with a 30-second notice. Workloads must be fault-tolerant and run with at least 2 replicas for high availability. Always explicitly warn users about this preemption risk when recommending Spot VMs.
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: stateless-spot-app
spec:
  replicas: 2
  template:
    spec:
      nodeSelector:
        cloud.google.com/gke-provisioning: Spot
      terminationGracePeriodSeconds: 25  # Must be < 30s to allow graceful shutdown before preemption
      containers:
      - name: app
        image: <IMAGE>
在GKE Autopilot中,对于无状态或批量工作负载,无需创建自定义ComputeClass即可使用Spot VM。只需在Pod或Deployment配置中添加
nodeSelector
,直接指向内置的Spot资源池。
[!WARNING] 抢占警告:Spot VM为可中断实例,随时可能收到30秒通知后被抢占。工作负载必须具备容错能力,且至少运行2个副本以保证高可用性。推荐Spot VM时务必明确告知用户该抢占风险。
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: stateless-spot-app
spec:
  replicas: 2
  template:
    spec:
      nodeSelector:
        cloud.google.com/gke-provisioning: Spot
      terminationGracePeriodSeconds: 25  # 必须小于30秒,以便在抢占前完成优雅关闭
      containers:
      - name: app
        image: <IMAGE>

2. Pod Rightsizing

2. Pod规格调整

Use VPA recommendations to reduce over-provisioned requests.
bash
undefined
使用VPA建议减少过度配置的资源请求。
bash
undefined

1. Deploy VPA in recommendation mode

1. 以推荐模式部署VPA

kubectl apply -f - <<EOF apiVersion: autoscaling.k8s.io/v1 kind: VerticalPodAutoscaler metadata: name: <DEPLOYMENT>-vpa spec: targetRef: apiVersion: apps/v1 kind: Deployment name: <DEPLOYMENT> updatePolicy: updateMode: "Off" EOF
kubectl apply -f - <<EOF apiVersion: autoscaling.k8s.io/v1 kind: VerticalPodAutoscaler metadata: name: <DEPLOYMENT>-vpa spec: targetRef: apiVersion: apps/v1 kind: Deployment name: <DEPLOYMENT> updatePolicy: updateMode: "Off" EOF

2. Wait 24+ hours for data collection

2. 等待24小时以上收集数据

3. Read recommendations

3. 查看推荐配置

kubectl get vpa <DEPLOYMENT>-vpa -o jsonpath='{.status.recommendation}'

**Optimization rules:**

Condition                     | Action                             | Savings
----------------------------- | ---------------------------------- | -------
CPU request >5x P95 actual    | Reduce to `P95 * 1.2`              | High
Memory request >3x P95 actual | Reduce to `P95 * 1.2`              | High
CPU request >2x P95 actual    | Reduce to `P95 * 1.2`              | Medium
No resource requests set      | Add requests (enables bin-packing) | Medium
kubectl get vpa <DEPLOYMENT>-vpa -o jsonpath='{.status.recommendation}'

**优化规则:**

条件                     | 操作                             | 成本节省幅度
----------------------------- | ---------------------------------- | -------
CPU请求量 > 实际P95值的5倍    | 调整为`P95 * 1.2`              | 高
内存请求量 > 实际P95值的3倍 | 调整为`P95 * 1.2`              | 高
CPU请求量 > 实际P95值的2倍    | 调整为`P95 * 1.2`              | 中
未设置资源请求                | 添加资源请求(启用装箱优化) | 中

3. Machine Type Selection

3. 机器类型选择

FamilyUse CaseRelative Cost
e2General purpose, burstableLowest
t2a / t2dScale-out (Arm/AMD), price-performanceLow
: : optimized : :
n4aAxion Arm-based, general-purposeLow
: : price-performance : :
n4 / n4dGeneral purpose (Intel/AMD), flexible shapesLow-Medium
c4aCompute-optimized (Arm), high efficiencyMedium-High
c3 / c4Compute-optimized (Intel)Medium-High
c3d / c4dCompute-optimized (AMD), high-performanceMedium-High
: : throughput : :
ek-standardAutopilot enhanced (golden path)Medium
m3 / x4Memory-optimized, SAP HANA, large databasesHigh
g2 (L4 GPU)AI inferenceHigh
a3 (H100 GPU)AI trainingHighest
a4 / a4xUltra-scale AI (Blackwell GPUs)Highest
In Autopilot, machine type is managed. Use ComputeClasses to influence selection.
系列使用场景相对成本
e2通用型、可突发性能最低
t2a / t2d横向扩展型(Arm/AMD)、性价比优化
n4aAxion Arm架构、通用型、性价比优化
n4 / n4d通用型(Intel/AMD)、灵活规格中低
c4a计算优化型(Arm)、高能效中高
c3 / c4计算优化型(Intel)中高
c3d / c4d计算优化型(AMD)、高性能吞吐量中高
ek-standardAutopilot增强型(黄金路径)
m3 / x4内存优化型、SAP HANA、大型数据库
g2 (L4 GPU)AI推理场景
a3 (H100 GPU)AI训练场景最高
a4 / a4x超大规模AI场景(Blackwell GPU)最高
在Autopilot模式下,机器类型由系统管理。可通过ComputeClass影响类型选择。

4. Committed Use Discounts (CUDs)

4. 承诺使用折扣(CUD)

For steady-state workloads, purchase 1-year or 3-year CUDs:
  • 1-year: ~20-30% discount
  • 3-year: ~50-55% discount
  • Applied automatically to matching usage in the region
  • Purchase via Google Cloud Console > Billing > Committed use discounts
对于稳定运行的工作负载,购买1年或3年的CUD:
  • 1年期:约20-30%折扣
  • 3年期:约50-55%折扣
  • 自动应用于区域内匹配的资源使用
  • 通过Google Cloud控制台 > 计费 > 承诺使用折扣进行购买

5. Cluster Management

5. 集群管理

  • Stop/start dev clusters: Idle dev clusters cost money even with no workloads (control plane fee).
  • Right-size node pools (Standard): Use Cluster Autoscaler with appropriate min/max.
  • Multi-tenant clusters: Share a single cluster across teams instead of per-team clusters (see the
    gke-multitenancy
    skill).
  • 启停开发集群:闲置的开发集群即使没有工作负载也会产生控制平面费用。
  • 调整节点池规格(Standard模式):配合Cluster Autoscaler设置合适的最小/最大节点数。
  • 多租户集群:多个团队共享单个集群,而非每个团队单独部署集群(详见
    gke-multitenancy
    技能)。

Cost Monitoring

成本监控

bash
undefined
bash
undefined

View cluster cost breakdown (requires Cost Management API)

查看集群成本明细(需启用Cost Management API)

gcloud billing budgets list --billing-account=<BILLING_ACCOUNT> --quiet
gcloud billing budgets list --billing-account=<BILLING_ACCOUNT> --quiet

View node utilization

查看节点利用率

kubectl top nodes
kubectl top nodes

View pod resource usage vs requests

查看Pod资源使用量 vs 请求量

kubectl top pods --all-namespaces --containers
undefined
kubectl top pods --all-namespaces --containers
undefined

Dev/Test Cost Savings

开发/测试环境成本节省

For non-production environments, these golden path deviations are acceptable:
| Setting | Production (Golden | Dev/Test | : : Path) : : | ----------------------- | ------------------ | ----------------------------- | | Cluster mode | Autopilot | Autopilot (cheaper with fewer | : : : pods) : | Release channel | Regular | Rapid (get fixes faster) | | Private nodes | Required | Optional (simpler access) | | Monitoring components | Full suite | SYSTEM_COMPONENTS only | | Secret Manager rotation | 120s | Disabled | | Maintenance windows | Configured | Not needed |
对于非生产环境,以下黄金路径偏差是可接受的:
配置项生产环境(黄金路径)开发/测试环境
集群模式AutopilotAutopilot(Pod数量更少时更经济)
发布通道RegularRapid(更快获取修复更新)
私有节点必填可选(简化访问)
监控组件完整套件仅启用SYSTEM_COMPONENTS
Secret Manager轮换120s禁用
维护窗口已配置无需配置