kubectl scale changes the desired replica count of a scalable controller. It is common in CI for scaling down a preview environment or scaling a job-runner deployment for a load test.
What it does
kubectl scale --replicas=N sets the replica field on a Deployment, ReplicaSet, StatefulSet, or ReplicationController. --current-replicas makes the change conditional (only scale if currently at M), which avoids racing another actor. It returns once the spec is updated, not once pods are Ready.
The most confusing "failure" is no error at all: you scale a Deployment that is also managed by a HorizontalPodAutoscaler, and the HPA immediately scales it back, so your replica count silently reverts. Scale the HPA bounds (or delete it) instead of the Deployment. "the object has been modified; please apply your changes to the latest version" on --current-replicas means another controller changed the count between read and write - re-read and retry. Remember scale does not wait for readiness; follow it with kubectl rollout status if the pipeline depends on the new pods being up.
Using this in CI
A runner has no kubeconfig, no cached context, and no interactive auth. Every kubectl invocation in CI needs the context supplied explicitly, and most confusing CI failures here are the command running against the wrong cluster or no cluster at all.
Terminal
# never rely on the ambient context on a runner
kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get pods
# confirm what you are actually connected to before mutating anything
kubectl config current-context
kubectl cluster-info
# fail fast instead of hanging on an unreachable API server
kubectl --request-timeout=30s get nodes
Frequently asked questions
kubectl scale: Usage, Options & Common CI Errors?
kubectl scale changes the desired replica count of a scalable controller. It is common in CI for scaling down a preview environment or scaling a job-runner deployment for a load test.
What it does?
kubectl scale --replicas=N sets the replica field on a Deployment, ReplicaSet, StatefulSet, or ReplicationController. --current-replicas makes the change conditional (only scale if currently at M), which avoids racing another actor. It returns once the spec is updated, not once pods are Ready.
Common errors in CI?
The most confusing "failure" is no error at all: you scale a Deployment that is also managed by a HorizontalPodAutoscaler, and the HPA immediately scales it back, so your replica count silently reverts. Scale the HPA bounds (or delete it) instead of the Deployment.