Kubernetes 03 - Scaling ReplicaSets

In Kubernetes, ReplicaSets are used to ensure that a specified number of pod replicas are running at any given time. This feature is crucial for maintaining high availability and fault tolerance in your applications.

What are ReplicaSets?

ReplicaSets in Kubernetes are like automatic managers for your application’s pods. They ensure that a specified number of identical pods (replicas) are running at all times. This is crucial for:

  • High availability: If a pod fails, ReplicaSet quickly replaces it.
  • Load balancing: Multiple replicas can handle increased traffic.
  • Easy scaling: You can increase or decrease the number of replicas as needed.

Creating a Deployment

Let’s start by creating a simple deployment using the Apache HTTP Server (httpd) image:

kubectl create deployment my-apache --image httpd

This command tells Kubernetes to:

  1. Create a new deployment named “my-apache”
  2. Use the “httpd” image to create pods

After running this, let’s check the status of our deployment:

kubectl get all

You’ll see output similar to this:

NAME                             READY   STATUS    RESTARTS   AGE
pod/my-apache-5bd7979764-j6h2j   1/1     Running   0          6m7s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   6m56s

NAME                        READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/my-apache   1/1     1            1           6m7s

NAME                                   DESIRED   CURRENT   READY   AGE
replicaset.apps/my-apache-5bd7979764   1         1         1       6m7s

Let’s break down this output:

  • We have one pod running our Apache server.
  • The deployment is managing this pod.
  • A ReplicaSet is ensuring we have the desired number of pods (currently 1).

Scaling

Now, let’s scale our deployment to run three replicas instead of one:

kubectl scale deploy/my-apache --replicas=3

This command tells Kubernetes to:

  1. Find the deployment named “my-apache”
  2. Change the number of desired replicas to 3

You can also use kubectl scale deploy my-apache --replicas=3 or kubectl scale deployment my-apache --replicas=3. In Kubernetes, deploy, deployment, and deployments are interchangeable in many commands.

After scaling, let’s check the status again:

kubectl get all

You should now see something like this::

NAME                             READY   STATUS    RESTARTS   AGE
pod/my-apache-5bd7979764-j6h2j   1/1     Running   0          9m42s
pod/my-apache-5bd7979764-vsfft   1/1     Running   0          6s
pod/my-apache-5bd7979764-zdfkd   1/1     Running   0          6s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   10m

NAME                        READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/my-apache   3/3     3            3           9m42s

NAME                                   DESIRED   CURRENT   READY   AGE
replicaset.apps/my-apache-5bd7979764   3         3         3       9m42s

This is what happened behind the scenes:

  1. The kubectl scale command updated the deployment configuration.
  2. Kubernetes’ Control Manager noticed the change in desired replica count.
  3. It instructed the ReplicaSet to create two new pods.
  4. The Scheduler assigned these new pods to available nodes.
  5. The Kubelet on each node created the new containers.

ReplicaSets and scaling are powerful features in Kubernetes that help maintain the desired state of your application. By using these tools, you can ensure your application remains available and responsive, even as demand fluctuates or if issues arise with individual pods.




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • Dependency Injection
  • CPU Cache
  • Understanding Linear Blended Skinning in 3D Animation
  • Starvation in Operating Systems
  • Virtual Memory