Sub topic 3
Scaling a Deployment by hand
This chapter assumes you can apply manifests with kubectl, read YAML, and know that a Deployment manages a ReplicaSet, which manages Pods. Manual scaling is the base that the Horizontal Pod Autoscaler (HPA) later automates, so it is worth understanding exactly what changes when you scale.
What scaling by hand changes
A Deployment's spec.replicas field states how many Pods you want. Scaling by hand means changing that number. The Deployment controller passes the new count to its current ReplicaSet, and the ReplicaSet creates or deletes Pods until the actual count matches.
Scaling does not trigger a rollout. A rollout happens only when the Pod template changes, for example a new image. A change to replicas alone leaves the Pod template untouched.
The Pods you scale must declare resource requests. The scheduler uses requests to decide which node has room. Here is the Deployment used in this chapter:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
resources:
requests:
cpu: 200m
memory: 128Mi
limits:
memory: 256MiScaling imperatively with kubectl
The quickest way is kubectl scale:
kubectl scale deployment/web --replicas=5
kubectl get deployment webThe output of kubectl get deployment web shows the READY column climbing, such as 3/5, then 5/5, as new Pods pass their readiness checks. If a Pod stays Pending, no node has enough unreserved CPU or memory for its requests. That is the situation the cluster autoscaler addresses in a later chapter.
Scaling declaratively
Imperative scaling is convenient but leaves your manifest out of date. If your manifest says replicas: 2 and you run kubectl apply again, Kubernetes sets the count back to 2. The declarative approach is to edit replicas in the file and apply it, so the file remains the source of truth in version control.
Use the imperative form for emergencies and experiments. Use the declarative form for anything that should persist.
Scaling flow
The diagram shows what happens after you change the replica count.
Common errors
- Manifest drift. You scaled with
kubectl scale, and a laterkubectl applyof an older file undoes it. Keep the file in sync. - Fighting the HPA. When an HPA targets the Deployment, it overwrites your manual replica count at its next evaluation. Change
minReplicasandmaxReplicason the HPA instead. - Setting
replicasin a manifest that an HPA manages. Each apply resets the count to the file's value, causing a sudden jump. Omitreplicasfrom the manifest when an HPA owns the count. - Pods stuck in
Pending. The new replicas request more than the cluster has free. Check withkubectl describe podand read the events. - Scaling to zero by accident.
--replicas=0stops all Pods and the service goes down. It is valid, but only do it on purpose.
Exercise
Exercise
A Deployment has replicas: 3 in its file in Git. During an incident you run kubectl scale deployment/web --replicas=8. The next day a teammate runs kubectl apply -f deployment.yaml with the unchanged file. How many replicas does the Deployment have afterwards, and what is the right way to keep 8?
Show answer
It returns to 3, because apply sets spec.replicas to the value in the file. To keep 8, edit replicas to 8 in the file, commit it, and apply it. Scaling does not restart the existing Pods, because the Pod template did not change.
Exercise
You run kubectl scale deployment/web --replicas=10, and kubectl get deployment web stays at 6/10. The remaining Pods are Pending. What is the most likely cause and the first command to confirm it?
Show answer
The nodes lack enough unreserved CPU or memory to satisfy the Pods' requests. Run kubectl describe pod <pending-pod> and look in the Events section for a FailedScheduling message such as Insufficient cpu.