What is a ReplicaSet in Kubernetes

Bittu SharmaBittu Sharma
3 min read

A ReplicaSet ensures that a specified number of pod replicas are running at all times. If a pod crashes or a node fails, the ReplicaSet automatically spins up a new pod to maintain the desired state.

👉 Think of it as a controller that guarantees availability and scaling of your application pods.

Key features of ReplicaSet:

  • Ensures desired number of pod replicas are always running.

  • Replaces failed or deleted pods.

  • Provides basic scaling functionality.


🔹 What is a Deployment in Kubernetes?

While ReplicaSet is useful, managing it directly can be complex. That’s where Deployment comes in.

A Deployment is a higher-level abstraction that manages ReplicaSets and Pods for you. It not only maintains replicas but also supports rolling updates, rollbacks, and versioned deployments.

👉 In short:

  • ReplicaSet = Manages replicas of pods.

  • Deployment = Manages ReplicaSets (and indirectly pods).


🔹 When to Use Deployment vs ReplicaSet?

  • Use ReplicaSet only if you need fine-grained control and don’t require rolling updates.

  • Use Deployment in most cases since it’s more powerful, flexible, and production ready.


🛠️ Step-by-Step Example: Kubernetes Deployment with ReplicaSet

Let’s walk through deploying a simple Nginx web server on Kubernetes.


1. Create a Deployment YAML

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

Explanation:

  • replicas: 3 → Kubernetes will maintain 3 pods.

  • selector → Tells Deployment which pods to manage.

  • template → Defines the pod (container image, ports, etc.).


2. Apply the Deployment

Run the command:

kubectl apply -f nginx-deployment.yaml

3. Verify Deployment and ReplicaSet

Check Deployment:

kubectl get deployment

Check ReplicaSet:

kubectl get rs

Check Pods:

kubectl get pods

👉 You should see 3 pods running, managed by a ReplicaSet created automatically by the Deployment.


4. Scaling the Deployment

To scale pods from 3 → 5:

kubectl scale deployment nginx-deployment --replicas=5

Now check pods again:

kubectl get pods

5. Updating the Deployment

Let’s update the container image to nginx:1.21

kubectl set image deployment/nginx-deployment nginx=nginx:1.21

Check rollout status:

kubectl rollout status deployment/nginx-deployment

6. Rollback Deployment

If the update fails, rollback with:

kubectl rollout undo deployment/nginx-deployment

🔎 Checking Logs of a Pod

To view logs of one of the running pods:

kubectl logs <pod-name>

Example:

kubectl logs nginx-deployment-5f5f6d79b9-abcde

📌 Key Takeaways

  • ReplicaSet ensures your desired number of pods are always running.

  • Deployment is a higher-level controller that manages ReplicaSets and provides updates, rollbacks, and scaling.

  • In practice, you’ll almost always use Deployment instead of directly creating a ReplicaSet.

  • With just a few commands, you can scale, update, and roll back applications running in Kubernetes.


✅ Conclusion

Kubernetes Deployments and ReplicaSets are essential for running resilient, scalable applications. With Deployments, you don’t have to manually manage ReplicaSets—it takes care of everything while offering advanced features.

Now that you’ve learned the basics, try deploying more complex applications (like multi-container apps or databases) and practice scaling and rolling updates.

Follow me on LinkedIn

Follow me on GitHub

0
Subscribe to my newsletter

Read articles from Bittu Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Bittu Sharma
Bittu Sharma

I am Bittu Sharma, a DevOps & AI Engineer with a keen interest in building intelligent, automated systems. My goal is to bridge the gap between software engineering and data science, ensuring scalable deployments and efficient model operations in production.! 𝗟𝗲𝘁'𝘀 𝗖𝗼𝗻𝗻𝗲𝗰𝘁 I would love the opportunity to connect and contribute. Feel free to DM me on LinkedIn itself or reach out to me at bittush9534@gmail.com. I look forward to connecting and networking with people in this exciting Tech World.