kubectl scale: Command Reference for CI/CD
Set the replica count up or down in a single command.
kubectl scale changes a workload desired replica count. CI uses it to park preview environments at zero or scale a worker pool for a load test. This reference covers the flags, conditional scaling, and the HPA conflict.
Common flags and usage
- scale deploy/<name> --replicas=N: set the replica count
- --current-replicas=M: only scale if currently at M (avoids races)
- --replicas=0: park a preview environment
- -f <manifest>: scale the workload defined in a file
- Returns when the spec is updated, not when pods are Ready
Example
# Park the preview env at the end of a PR pipeline
kubectl scale deploy/web --replicas=0
# Scale up for a load test, then gate on readiness
kubectl scale deploy/web --current-replicas=2 --replicas=10
kubectl rollout status deploy/web --timeout=120sIn CI
If a HorizontalPodAutoscaler manages the Deployment, it scales your manual change straight back, so the count silently reverts: adjust the HPA bounds instead. scale does not wait for readiness, so follow it with rollout status when the pipeline depends on the new pods.
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.
# 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 nodesKey takeaways
- An HPA overrides a manual scale; change the HPA bounds instead.
- --current-replicas makes scaling conditional and race-safe.
- scale returns on spec update, not readiness; gate with rollout status.