In this lesson, you will learn how to dynamically adjust your application's capacity by modifying the replicas count within your active ReplicaSet configuration.
Why Scale Horizontally?
When your application experiences a surge in traffic, a single Pod might become a bottleneck. Instead of upgrading the underlying server (vertical scaling), Kubernetes allows you to scale "horizontally" by adding more identical Pods. Since you are already using a ReplicaSet to maintain your desired state, scaling is as simple as updating the spec.replicas field.
Updating the Replica Count
Because Kubernetes follows a declarative API model, you don't need to manually create or delete individual Pods. You simply tell the ReplicaSet controller your new goal, and it handles the rest.
If you have a running ReplicaSet defined in a YAML file, you can scale it by changing the replicas value:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: web-server
spec:
replicas: 3 # Change this value to 5 to scale up, or 1 to scale down
selector:
matchLabels:
app: frontend
template:
# ... Pod template details
Once you apply this change using kubectl apply -f your-file.yaml, the Kubernetes control loop detects the discrepancy between the "current state" (3 Pods) and the "desired state" (5 Pods). The ReplicaSet controller automatically provisions the two additional Pods to meet your requirements.
Real-time Scaling with Kubectl
You can also perform this action imperatively without modifying your source file by using the kubectl scale command:
kubectl scale --replicas=5 rs/web-server
This command instructs the API server to update the object's definition immediately. The controller will notice the update and initiate the creation of new Pods (or termination of existing ones if you scale down) to align the cluster state with your request. This capability allows you to respond rapidly to changing workloads, ensuring your application remains available and performant during peak demand.
Key Takeaway: Scaling horizontally in Kubernetes is achieved by updating the replicas field, prompting the ReplicaSet controller to automatically reconcile the number of active Pods to match your new desired capacity.