gke-scaling
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGKE 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_clusterupdate_node_pool
本参考文档介绍GKE上的工作负载扩缩容配置。黄金路径默认启用VPA、OPTIMIZE_UTILIZATION扩缩容配置文件以及节点自动配置(NAP)。
MCP工具:、get_k8s_resource、describe_k8s_resource、apply_k8s_manifest、patch_k8s_resource、get_cluster、update_clusterupdate_node_pool
Golden Path Scaling Defaults
黄金路径扩缩容默认配置
| Setting | Golden Path Value | Notes |
|---|---|---|
| | Aggressive scale-down for cost savings |
| | VPA recommendations available |
| | NAP creates node pools on demand |
| GPU resource limits (T4, A100) | | NAP can provision GPU nodes |
| 配置项 | 黄金路径默认值 | 说明 |
|---|---|---|
| | 主动缩容以节省成本 |
| | 可获取VPA推荐配置 |
| | NAP会按需创建节点池 |
| GPU资源限制(T4、A100) | | NAP可配置GPU节点 |
Scaling Mechanisms
扩缩容机制
1. Manual Scaling
1. 手动扩缩容
kubectl-only — no MCP equivalent for. Use kubectl directly.kubectl scale
bash
kubectl scale deployment <DEPLOYMENT> --replicas=<N> -n <NAMESPACE>仅支持kubectl —— 没有与对应的MCP工具。请直接使用kubectl。kubectl scale
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 autoscalebash
kubectl autoscale deployment <DEPLOYMENT> --cpu-percent=50 --min=1 --max=10Manifest approach (recommended — use MCP ):
apply_k8s_manifestSee 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 —— 没有与对应的MCP工具):
kubectl autoscalebash
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: 503. Vertical Pod Autoscaling (VPA)
3. 垂直Pod自动扩缩容(VPA)
Adjusts CPU and memory requests to match actual usage. Enabled by default on
golden path.
Update modes:
- — recommendations only (safest, start here)
Off - — sets resources only at pod creation
Initial - — restarts pods to apply new resource values
Auto - — updates resources without restart when possible (GKE 1.34+)
InPlaceOrRecreate
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_resourceundefined根据实际使用情况调整CPU和内存请求。黄金路径默认启用该功能。
更新模式:
- —— 仅提供推荐配置(最安全,建议从该模式开始)
Off - —— 仅在Pod创建时设置资源
Initial - —— 重启Pod以应用新的资源配置
Auto - —— 尽可能在不重启的情况下更新资源(GKE 1.34+版本支持)
InPlaceOrRecreate
创建推荐模式的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_resourceundefinedMCP (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
undefinedEnable 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
--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-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
--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
--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
最佳实践
- Define resource requests: HPA and VPA rely on accurate requests. Always set them.
- Avoid metric conflicts: Do not use HPA and VPA on the same metric. Typical pattern: HPA on CPU, VPA on memory.
- Pod Disruption Budgets: Define PDBs for all production workloads to ensure availability during scaling events.
- HPA stabilization: HPA has a default 5-minute stabilization window. Tune
for faster response if needed.
behavior - VPA "Auto" caution: Auto mode restarts pods. Ensure your app handles SIGTERM gracefully. VPA requires at least 2 replicas for evictions by default.
- Use ComputeClasses: For workload-specific node targeting (Spot fallback, GPU, specific machine families), use ComputeClasses instead of node selectors.
- 定义资源请求:HPA和VPA依赖准确的资源请求,请务必配置。
- 避免指标冲突:不要在同一指标上同时使用HPA和VPA。典型搭配:HPA基于CPU,VPA基于内存。
- Pod中断预算(PDB):为所有生产环境工作负载定义PDB,确保扩缩容事件期间的可用性。
- HPA稳定窗口:HPA默认有5分钟稳定窗口,如需更快响应可调整配置。
behavior - VPA "Auto"模式注意事项:Auto模式会重启Pod,请确保应用能优雅处理SIGTERM信号。默认情况下,VPA需要至少2个副本才能执行驱逐操作。
- 使用ComputeClasses:如需针对特定工作负载选择节点(如Spot实例 fallback、GPU、特定机型系列),请使用ComputeClasses而非节点选择器。
Rightsizing Workflow
资源优化流程
- Deploy VPA in mode for 24+ hours
Off - Read recommendations:
kubectl describe vpa <NAME> - Compare values against current
targetrequests - Apply with 20% buffer:
new_request = target * 1.2 - Use patch format to update Deployment
| Condition | Recommendation | Risk |
|---|---|---|
| CPU request >5x P95 actual | Reduce to | Medium |
| Memory request >3x P95 actual | Reduce to | Medium |
| CPU request >2x P95 actual | Rightsizing with 20% buffer | Low |
| No resource limits set | Add limits to prevent noisy-neighbor | Low |
- 以模式部署VPA并运行24小时以上
Off - 查看推荐配置:
kubectl describe vpa <NAME> - 对比值与当前
target配置requests - 预留20%缓冲后应用配置:
新请求值 = target * 1.2 - 使用patch格式更新Deployment
| 场景 | 建议措施 | 风险等级 |
|---|---|---|
| CPU请求 > 实际P95值的5倍 | 调整为 | 中等 |
| 内存请求 > 实际P95值的3倍 | 调整为 | 中等 |
| CPU请求 > 实际P95值的2倍 | 预留20%缓冲进行资源优化 | 低 |
| 未设置资源限制 | 添加限制以避免“噪声邻居”问题 | 低 |