gke-scaling

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

GKE Workload Scaling

GKE工作负载扩缩容

This reference covers scaling workloads on GKE. The golden path enables VPA, OPTIMIZE_UTILIZATION autoscaling profile, and Node Auto Provisioning by default.
MCP Tools:
get_k8s_resource
,
describe_k8s_resource
,
apply_k8s_manifest
,
patch_k8s_resource
,
get_cluster
,
update_cluster
,
update_node_pool
本参考文档介绍GKE上的工作负载扩缩容配置。黄金路径默认启用VPA、OPTIMIZE_UTILIZATION扩缩容配置文件以及节点自动配置(NAP)。
MCP工具:
get_k8s_resource
describe_k8s_resource
apply_k8s_manifest
patch_k8s_resource
get_cluster
update_cluster
update_node_pool

Golden Path Scaling Defaults

黄金路径扩缩容默认配置

SettingGolden Path ValueNotes
autoscaling.autoscalingProfile
OPTIMIZE_UTILIZATION
Aggressive scale-down for cost savings
verticalPodAutoscaling.enabled
true
VPA recommendations available
autoscaling.enableNodeAutoprovisioning
true
NAP creates node pools on demand
GPU resource limits (T4, A100)
1000000000
each
NAP can provision GPU nodes
配置项黄金路径默认值说明
autoscaling.autoscalingProfile
OPTIMIZE_UTILIZATION
主动缩容以节省成本
verticalPodAutoscaling.enabled
true
可获取VPA推荐配置
autoscaling.enableNodeAutoprovisioning
true
NAP会按需创建节点池
GPU资源限制(T4、A100)
1000000000
每个
NAP可配置GPU节点

Scaling Mechanisms

扩缩容机制

1. Manual Scaling

1. 手动扩缩容

kubectl-only — no MCP equivalent for
kubectl scale
. Use kubectl directly.
bash
kubectl scale deployment <DEPLOYMENT> --replicas=<N> -n <NAMESPACE>
仅支持kubectl —— 没有与
kubectl scale
对应的MCP工具。请直接使用kubectl。
bash
kubectl scale deployment <DEPLOYMENT> --replicas=<N> -n <NAMESPACE>

2. Horizontal Pod Autoscaling (HPA)

2. 水平Pod自动扩缩容(HPA)

Scales the number of pods based on metrics.
Quick setup (kubectl-only — no MCP equivalent for
kubectl autoscale
):
bash
kubectl autoscale deployment <DEPLOYMENT> --cpu-percent=50 --min=1 --max=10
Manifest approach (recommended — use MCP
apply_k8s_manifest
):
See assets/hpa-example.yaml for a template.
yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: <DEPLOYMENT>-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: <DEPLOYMENT>
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50
基于指标调整Pod数量。
快速配置(仅支持kubectl —— 没有与
kubectl autoscale
对应的MCP工具):
bash
kubectl autoscale deployment <DEPLOYMENT> --cpu-percent=50 --min=1 --max=10
清单配置法(推荐 —— 使用MCP工具
apply_k8s_manifest
):
模板可参考assets/hpa-example.yaml
yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: <DEPLOYMENT>-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: <DEPLOYMENT>
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50

3. Vertical Pod Autoscaling (VPA)

3. 垂直Pod自动扩缩容(VPA)

Adjusts CPU and memory requests to match actual usage. Enabled by default on golden path.
Update modes:
  • Off
    — recommendations only (safest, start here)
  • Initial
    — sets resources only at pod creation
  • Auto
    — restarts pods to apply new resource values
  • InPlaceOrRecreate
    — updates resources without restart when possible (GKE 1.34+)
Create VPA in recommendation mode:
yaml
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: <DEPLOYMENT>-vpa
spec:
  targetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: <DEPLOYMENT>
  updatePolicy:
    updateMode: "Off"
Read recommendations (prefer MCP
describe_k8s_resource
):
undefined
根据实际使用情况调整CPU和内存请求。黄金路径默认启用该功能。
更新模式:
  • Off
    —— 仅提供推荐配置(最安全,建议从该模式开始)
  • Initial
    —— 仅在Pod创建时设置资源
  • Auto
    —— 重启Pod以应用新的资源配置
  • InPlaceOrRecreate
    —— 尽可能在不重启的情况下更新资源(GKE 1.34+版本支持)
创建推荐模式的VPA:
yaml
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: <DEPLOYMENT>-vpa
spec:
  targetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: <DEPLOYMENT>
  updatePolicy:
    updateMode: "Off"
查看推荐配置(优先使用MCP工具
describe_k8s_resource
):
undefined

MCP (preferred)

MCP(优先选择)

describe_k8s_resource(parent="...", resourceType="verticalpodautoscaler", name="<DEPLOYMENT>-vpa", namespace="<NAMESPACE>")
describe_k8s_resource(parent="...", resourceType="verticalpodautoscaler", name="<DEPLOYMENT>-vpa", namespace="<NAMESPACE>")

kubectl fallback

kubectl备选方案

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

See [assets/vpa-example.yaml](./assets/vpa-example.yaml) for a full template.
kubectl get vpa <DEPLOYMENT>-vpa -o jsonpath='{.status.recommendation}'

完整模板可参考[assets/vpa-example.yaml](./assets/vpa-example.yaml)。

4. Cluster Autoscaler / Node Auto Provisioning (NAP)

4. 集群自动扩缩容 / 节点自动配置(NAP)

On Autopilot (golden path), node scaling is fully managed. NAP automatically creates and sizes node pools based on workload demands.
For Standard clusters:
bash
undefined
在Autopilot模式(黄金路径)下,节点扩缩容完全由平台管理。NAP会根据工作负载需求自动创建并调整节点池规模。
对于Standard集群:
bash
undefined

Enable cluster autoscaler on a node pool

在节点池上启用集群自动扩缩容

gcloud container clusters update <CLUSTER_NAME> --region <REGION>
--enable-autoscaling --node-pool <POOL_NAME>
--min-nodes <MIN> --max-nodes <MAX>
--quiet
gcloud container clusters update <CLUSTER_NAME> --region <REGION>
--enable-autoscaling --node-pool <POOL_NAME>
--min-nodes <MIN> --max-nodes <MAX>
--quiet

Enable NAP

启用NAP

gcloud container clusters update <CLUSTER_NAME> --region <REGION>
--enable-autoprovisioning
--min-cpu <MIN_CPU> --max-cpu <MAX_CPU>
--min-memory <MIN_MEM> --max-memory <MAX_MEM>
--quiet

**Autoscaling profiles:**

| Profile                | Behavior                             | Golden Path? |
| ---------------------- | ------------------------------------ | ------------ |
| `BALANCED`             | Default GKE; conservative scale-down | No           |
| `OPTIMIZE_UTILIZATION` | Aggressive scale-down; lower idle    | **Yes**      |
:                        : resources                            :              :
gcloud container clusters update <CLUSTER_NAME> --region <REGION>
--enable-autoprovisioning
--min-cpu <MIN_CPU> --max-cpu <MAX_CPU>
--min-memory <MIN_MEM> --max-memory <MAX_MEM>
--quiet

**扩缩容配置文件:**

| 配置文件                | 行为描述                             | 是否为黄金路径默认值 |
| ---------------------- | ------------------------------------ | ------------ |
| `BALANCED`             | GKE默认配置;保守缩容策略 | 否           |
| `OPTIMIZE_UTILIZATION` | 主动缩容;减少闲置资源    | **是**      |

Best Practices

最佳实践

  1. Define resource requests: HPA and VPA rely on accurate requests. Always set them.
  2. Avoid metric conflicts: Do not use HPA and VPA on the same metric. Typical pattern: HPA on CPU, VPA on memory.
  3. Pod Disruption Budgets: Define PDBs for all production workloads to ensure availability during scaling events.
  4. HPA stabilization: HPA has a default 5-minute stabilization window. Tune
    behavior
    for faster response if needed.
  5. VPA "Auto" caution: Auto mode restarts pods. Ensure your app handles SIGTERM gracefully. VPA requires at least 2 replicas for evictions by default.
  6. Use ComputeClasses: For workload-specific node targeting (Spot fallback, GPU, specific machine families), use ComputeClasses instead of node selectors.
  1. 定义资源请求:HPA和VPA依赖准确的资源请求,请务必配置。
  2. 避免指标冲突:不要在同一指标上同时使用HPA和VPA。典型搭配:HPA基于CPU,VPA基于内存。
  3. Pod中断预算(PDB):为所有生产环境工作负载定义PDB,确保扩缩容事件期间的可用性。
  4. HPA稳定窗口:HPA默认有5分钟稳定窗口,如需更快响应可调整
    behavior
    配置。
  5. VPA "Auto"模式注意事项:Auto模式会重启Pod,请确保应用能优雅处理SIGTERM信号。默认情况下,VPA需要至少2个副本才能执行驱逐操作。
  6. 使用ComputeClasses:如需针对特定工作负载选择节点(如Spot实例 fallback、GPU、特定机型系列),请使用ComputeClasses而非节点选择器。

Rightsizing Workflow

资源优化流程

  1. Deploy VPA in
    Off
    mode for 24+ hours
  2. Read recommendations:
    kubectl describe vpa <NAME>
  3. Compare
    target
    values against current
    requests
  4. Apply with 20% buffer:
    new_request = target * 1.2
  5. Use patch format to update Deployment
ConditionRecommendationRisk
CPU request >5x P95 actualReduce to
P95 * 1.2
Medium
Memory request >3x P95 actualReduce to
P95 * 1.2
Medium
CPU request >2x P95 actualRightsizing with 20% bufferLow
No resource limits setAdd limits to prevent noisy-neighborLow
  1. Off
    模式部署VPA并运行24小时以上
  2. 查看推荐配置:
    kubectl describe vpa <NAME>
  3. 对比
    target
    值与当前
    requests
    配置
  4. 预留20%缓冲后应用配置:
    新请求值 = target * 1.2
  5. 使用patch格式更新Deployment
场景建议措施风险等级
CPU请求 > 实际P95值的5倍调整为
P95 * 1.2
中等
内存请求 > 实际P95值的3倍调整为
P95 * 1.2
中等
CPU请求 > 实际P95值的2倍预留20%缓冲进行资源优化
未设置资源限制添加限制以避免“噪声邻居”问题