ReplicaSet & Deployment

 📌 Kubernetes ReplicaSet & Deployment

🔹 What is a ReplicaSet?

A ReplicaSet (RS) is a Kubernetes controller that ensures a specified number of identical Pods are running at all times.

If a Pod:

  • Crashes

  • Is deleted

  • Node goes down

➡️ ReplicaSet automatically creates a new Pod to maintain the desired count.


🔹 Why ReplicaSet is Used

ReplicaSet is used to:

  • Maintain high availability

  • Ensure fault tolerance

  • Automatically heal Pods

  • Run multiple replicas of the same application


🔹 How ReplicaSet Works

  1. User defines desired number of replicas

  2. ReplicaSet uses labels & selectors

  3. It continuously compares:

    • Desired state vs Actual state

  4. If mismatch occurs → it creates or deletes Pods


🔹 ReplicaSet Key Concepts

1️⃣ Replicas

  • Number of Pod copies to run

  • Defined in spec.replicas

2️⃣ Pod Template

  • Blueprint for Pods

  • All Pods are identical

  • Defined under spec.template

3️⃣ Labels & Selectors

  • RS identifies Pods using labels

  • Selector must match Pod labels (mandatory)

⚠️ Mismatch will cause RS to fail


🔹 ReplicaSet YAML Structure (Important)

spec: replicas: 3 selector: matchLabels: app: paytm template: metadata: labels: app: paytm

🔹 What ReplicaSet Can Do

✅ Auto-heal Pods
✅ Scale up/down
✅ Maintain desired state
✅ Load balancing (via Service)


❌ ReplicaSet Limitations (Very Important)

❌ No rolling updates
❌ No rollback support
❌ No version tracking
❌ Updating image requires manual Pod deletion

⚠️ Not recommended for production directly


🔹 When to Use ReplicaSet

  • Rarely used directly

  • Mostly created and managed by Deployments

  • Useful for:

    • Learning Kubernetes basics

    • Understanding controllers


🔹 ReplicaSet Summary (Interview-Ready)

ReplicaSet ensures the desired number of Pods are always running using labels and selectors but lacks rolling update and rollback features.



📌 Kubernetes Deployment – Detailed Notes

🔹 What is a Deployment?

A Deployment is a higher-level Kubernetes controller that manages:

  • ReplicaSets

  • Pods

  • Application versions

  • Updates & rollbacks

It is the most commonly used workload in Kubernetes.


🔹 Why Deployment is Used

Deployment is used because it provides:

  • Zero-downtime deployments

  • Rolling updates

  • Easy rollback

  • Version control

  • Production-grade management

👉 Best choice for real-world applications


🔹 How Deployment Works (Flow)

Deployment → ReplicaSet → Pods
  1. Deployment creates a ReplicaSet

  2. ReplicaSet creates Pods

  3. On update:

    • New ReplicaSet is created

    • Old ReplicaSet is scaled down gradually


🔹 Deployment Key Features

1️⃣ Rolling Updates

  • Updates Pods gradually

  • No downtime

  • Default strategy

2️⃣ Rollback

  • Revert to previous version

  • Uses revision history

3️⃣ Versioning

  • Every update creates a new revision

  • Stored in Deployment history


🔹 Deployment YAML Highlights

spec: replicas: 3 selector: matchLabels: app: paytm template: spec: containers: - name: cont1 image: myapp:1.0

Changing the image automatically triggers a rollout.


🔹 Deployment Strategies

🔄 RollingUpdate (Default)

  • Gradual replacement

  • Zero downtime

strategy: type: RollingUpdate

🔁 Recreate

  • Deletes all Pods first

  • Causes downtime

strategy: type: Recreate

🔹 Deployment Rollout Commands

kubectl rollout status deploy movies kubectl rollout history deploy movies kubectl rollout undo deploy movies kubectl rollout undo deploy movies --to-revision=1

🔹 Scaling in Deployment

Manual scaling

kubectl scale deploy movies --replicas=10

Auto scaling (HPA)

kubectl autoscale deploy movies --cpu-percent=50 --min=2 --max=10

🔹 What Deployment Can Do

✅ Zero downtime upgrades
✅ Auto healing
✅ Rollback support
✅ Version history
✅ Auto scaling
✅ Production ready


🔹 When to Use Deployment

Use Deployment when:

  • Running stateless applications

  • You need rolling updates

  • You want rollback capability

  • Running apps in production


🔹 ReplicaSet vs Deployment (Key Comparison)

FeatureReplicaSetDeployment
Auto-heal
Scaling
Rolling update
Rollback
Versioning
Production use

Comments

Popular posts from this blog

Managing Amazon EBS Volumes and Snapshots Across Regions

Git for Beginners: Complete Guide from Installation to First Push on GitHub

AWS - Amazon Web Services