Sub topic 8
Scaling up and down
This chapter assumes you can apply a manifest with kubectl, you know what a Pod is, and you have read the earlier chapters on ReplicaSets and the Deployment manifest. You should already have the web Deployment from the previous chapter running with three replicas.
What scaling changes
Scaling means changing spec.replicas on a Deployment. Nothing else in the Pod template changes. The Deployment controller passes the new count to its current ReplicaSet. That ReplicaSet then creates or deletes Pods until the actual count matches the desired count.
Because the template stays the same, its hash stays the same. No new ReplicaSet appears and no rollout starts.
Scaling with kubectl
The imperative command is the quickest way to scale:
kubectl scale deployment/web --replicas=5
kubectl get rs -l app=webThe output should show one ReplicaSet with DESIRED set to 5. The ReplicaSet name is unchanged, which confirms that no new ReplicaSet was created.
To scale down, give a smaller number:
kubectl scale deployment/web --replicas=2The ReplicaSet picks which Pods to delete. It prefers Pods that are not yet running or ready, and Pods that started most recently. Each deleted Pod receives a termination signal and gets its grace period to shut down.
Scaling declaratively
kubectl scale changes the live object only. If your manifest in version control still says replicas: 3, the next kubectl apply -f deployment.yaml sets the count back to 3. To keep the two in step, edit spec.replicas in the file and apply it:
kubectl apply -f deployment.yamlTreat the file as the source of truth. Use kubectl scale for quick experiments or emergencies, then update the file afterward.
Scaling and rollouts together
You can scale during a rolling update. The Deployment controller spreads the extra replicas across the old and new ReplicaSets in proportion to their current sizes. This is called proportional scaling. It keeps a half-finished rollout from putting all the new capacity on one side.
Scaling to zero
Setting --replicas=0 deletes every Pod but keeps the Deployment and its ReplicaSets. This frees cluster resources while preserving the configuration, and scaling back up restores the workload.
kubectl scale deployment/web --replicas=0
kubectl scale deployment/web --replicas=3Scaling is not automatic
Everything above is manual. If load grows, the replica count does not change unless you change it. A later chapter covers the Horizontal Pod Autoscaler, which adjusts spec.replicas for you based on metrics. When an autoscaler manages a Deployment, remove replicas from the manifest so each kubectl apply does not overwrite the autoscaler's decision.
Exercise
You run kubectl scale deployment/web --replicas=6. Afterward, kubectl get rs -l app=web shows only one ReplicaSet. Is that expected, and why?
Show answer
Yes. Scaling changes only spec.replicas, not the Pod template. The template hash is unchanged, so the Deployment reuses its existing ReplicaSet and raises its desired count to 6. A new ReplicaSet appears only when the template changes.
Exercise
Your manifest says replicas: 3. You scale the live Deployment to 8 with kubectl scale. A teammate then runs kubectl apply -f deployment.yaml. How many replicas result, and how do you prevent a surprise like this?
Show answer
Three replicas result, because apply writes the manifest's value back. To prevent it, change replicas in the manifest and apply that, or remove the field from the manifest when an autoscaler owns the count.