CodeBlanks Logo Icon CodeBlanks

Scaling

Learn to scale a ReplicaSet with kubectl scale.

Scaling

A ReplicaSet is also scaled with kubectl scale. But if you directly scale a ReplicaSet that belongs to a Deployment, the Deployment immediately undoes it, since the desired state is in the Deployment. Scaling is always done through the top-level owner:

kubectl scale rs/web-7c5b9f6d8 --replicas=10    # the Deployment pulls it back to 3
kubectl scale deploy/web --replicas=10          # the right way

Conditional scaling

--current-replicas scales only if the current count is the expected value; it prevents accidental overwrites when two people make changes at the same time. Several deployments can also be scaled with one command:

kubectl scale deploy/web --current-replicas=3 --replicas=5
kubectl scale deploy web api --replicas=2
kubectl scale deploy -l katman=arka --replicas=0

Practice Scale at the right level

Write two commands: scale the web deployment to 6 only if its current replica count is 3; scale all deployments labelled katman=arka down to 1 replica.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl scale deploy/web --current-replicas=3 --replicas=6
kubectl scale deploy -l katman=arka --replicas=1

Expected output

deployment.apps/web scaled
deployment.apps/api scaled
deployment.apps/worker scaled

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What happens if you scale a ReplicaSet owned by a Deployment to 10 replicas?

  • The Deployment soon pulls it back to its own replicas value Correct answer
  • 10 replicas stay permanently
  • The Deployment is deleted
  • It errors

Explanation: Controllers continuously enforce the desired state.

2 Scale only if the current count is 3.

Question
kubectl scale deploy/web ____=3 --replicas=5
Answer
kubectl scale deploy/web --current-replicas=3 --replicas=5

Explanation: If the condition doesn't hold, the command errors.

3 Match.

Term Meaning
scale rs/... The owner undoes it
scale deploy/... The right level
--current-replicas Conditional scale
-l katman=arka Bulk scale by label

All lessons in Intermediate