Skip to content
Latchkey

kubectl scale: Set Replicas in CI

kubectl scale sets the desired replica count on a Deployment, ReplicaSet, StatefulSet, or ReplicationController.

Pipelines scale workloads to zero before a migration or up for a load test. scale changes the count directly without editing the manifest.

What it does

kubectl scale updates spec.replicas on the target workload. It can be made conditional with --current-replicas (only scale if the current count matches) or --resource-version (optimistic concurrency), which guards against racing another change.

Common usage

Terminal
kubectl scale deployment/api --replicas=5
kubectl scale statefulset/db --replicas=0 -n data
# only scale if currently at 3 (avoids racing)
kubectl scale deploy/api --current-replicas=3 --replicas=6
# scale everything matching a label
kubectl scale deploy -l tier=web --replicas=2

Options

FlagWhat it does
--replicas=<n>Desired replica count
--current-replicas=<n>Precondition: only scale if current count matches
--resource-version=<v>Precondition on the resourceVersion
-l, --selectorScale all workloads matching the label
--timeout=<dur>How long to wait for the scale to register

In CI

scale only sets the count; it does not wait for pods. Follow it with kubectl rollout status or kubectl wait if the job needs the new replicas ready. If an HPA manages this workload, a manual scale will be reverted on the next reconcile, so pause or remove the HPA first.

Common errors in CI

"Error from server (Conflict): ... the object has been modified" means another writer changed it; drop the --current-replicas/--resource-version precondition or retry. "Error from server (NotFound): deployments.apps \"api\" not found" means a wrong name or namespace. "error: cannot scale ... expected x replicas, got y" means the --current-replicas precondition did not hold.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →