Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 272 of 8024. Kubernetes Architecture and Core ObjectsFree lesson

Managing Application Scaling with Replicas

Understand how to adjust the number of running pod instances dynamically by modifying the replica count in your deployment configuration.

Managing Application Scaling with Replicas

In this lesson, you will learn how to dynamically adjust the number of running pod instances for your application by updating the replica count within your Kubernetes Deployment.

The Role of the Deployment Controller

Previously, we discussed how ReplicaSets ensure a specific number of pods are running. However, in modern Kubernetes, we rarely manage ReplicaSets directly. Instead, we use Deployments. A Deployment acts as a manager for your ReplicaSet, providing a higher level of abstraction that handles updates and scaling.

When you define a Deployment object, the spec.replicas field serves as your "desired state." The Kubernetes control loop constantly compares this number against the current state of your cluster. If you decide your application needs more power to handle traffic, you simply update that number, and the controller takes care of the heavy lifting.

Scaling Your Deployment

To scale your application, you don't need to delete and recreate your objects. You simply modify the replicas field in your configuration file or use kubectl to update the live object.

For example, if you have a YAML file defining a Deployment with 2 replicas:

spec:
  replicas: 2
  selector:
    matchLabels:
      app: web-server
  template:
    # ... container definitions ...

If traffic spikes and you need 5 instances, you update the replicas field to 5 and apply the changes. Alternatively, you can use the command line for an immediate update:

kubectl scale deployment my-web-server --replicas=5

Once you issue this command, the Deployment controller observes the difference between the desired state (5) and the current state (2). It automatically instructs the ReplicaSet to spin up 3 additional pods to reach the new target. If you scale down later, the controller gracefully terminates the extra pods, ensuring your application always matches the exact footprint you’ve defined. This declarative approach allows you to scale your infrastructure up or down in seconds without manual intervention.

Key Takeaway: Scaling in Kubernetes is a declarative process where you update the replicas count in your Deployment, and the controller automatically reconciles the current number of running pods to match your desired state.