kubectl scale

调整 Deployment、ReplicaSet 或 StatefulSet 的副本数

语法

kubectl scale <resource> <name> --replicas=<count> [flags]

参数

参数说明示例级别
--replicas 目标副本数 kubectl scale deployment web --replicas=5 常用
--current-replicas 前置条件:当前副本数必须匹配才执行 kubectl scale deployment web --current-replicas=3 --replicas=5 进阶
--timeout 等待扩缩容完成的超时时间 kubectl scale deployment web --replicas=10 --timeout=60s 进阶

示例

扩容到 5 个副本

kubectl scale deployment web-app --replicas=5
水平扩展应对流量增长

缩容到 1 个副本

kubectl scale deployment web-app --replicas=1
低峰期节省资源

缩容到 0(停止服务)

kubectl scale deployment web-app --replicas=0
不删除 Deployment 但停止所有 Pod

条件扩容

kubectl scale deployment web-app --current-replicas=3 --replicas=5
只有当前是 3 副本时才扩到 5,避免并发操作冲突

常见错误

the server could not find the requested resource 确认资源类型支持 scale(Deployment/ReplicaSet/StatefulSet)
cannot scale a resource that doesn't exist 检查资源名称和命名空间是否正确

技巧

相关命令