Scaling: Manual
The kubectl scale
command manually adjusts the number of pod replicas for a Deployment, ReplicaSet, StatefulSet, or Job.
This is useful when you want to:
- Increase replicas to handle more traffic.
- Reduce replicas to save resources.
- Perform manual scaling when Horizontal Pod Autoscaler (HPA) is not required
Basic Syntax Usage
kubectl scale <resource-type> <resource-name> --replicas=<number>
<resource-type>
: Type of resource (e.g.,deployment
,replicaset
,statefulset
,job
).<resource-name>
: The name of the resource you want to scale.--replicas=<number>
: Desired number of pod replicas.
Scaling Deployment
Lets create a deployment using nginx
image.
➜ kubectl create deployment my-nginx --image=nginx
deployment.apps/my-nginx created
It will create a deployment with replicas defaulted to 1
.
➜ kubectl get deployment my-nginx
NAME READY UP-TO-DATE AVAILABLE AGE
my-nginx 1/1 1 1 32s
Now lets scale the newly created deployment to 3
replicas using this command below.
➜ kubectl scale deployment my-nginx --replicas=3
deployment.apps/my-nginx scaled
Now if you check teh deployments again, we will have 3
replicas.
➜ kubectl get deployments my-nginx
NAME READY UP-TO-DATE AVAILABLE AGE
my-nginx 3/3 3 3
Others
To scale replicaset
, statefulset
, and job
is similar with scaling deployment, we only need to specify the resource type, resource name, and number of replicas in kubectl apply
command.
Note that this changes are not persisted. If you have yaml
file for your deployment definition you need to change the spec.replicas
value.