Skip to main content

Command Palette

Search for a command to run...

Kubernetes Deployment & ReplicaSets Explained (With Auto-Healing, Scaling & Zero Downtime)

Published
β€’6 min read

Let’s say you create a Pod in Kubernetes and your application starts running perfectly.

Now delete that Pod.

Your application disappears.

No recovery. No replacement. No warning.

This simple experiment reveals an important truth:

Pods are not designed for production resilience.

A standalone Pod can run your container, but it cannot guarantee availability, scaling, or automatic recovery.

To build fault-tolerant and scalable applications in Kubernetes, we use Deployments, which internally rely on ReplicaSets to maintain the desired state of the system.

In this guide, we’ll clearly understand:

🧩 Container vs Pod vs Deployment
❌ Why Pods are not production-ready
πŸ” What ReplicaSet actually does
πŸ”₯ How Auto-Healing works
πŸ“ˆ How Scaling works
🧠 What Controllers are
πŸ›  Hands-on commands for practice

Let’s break it down step by step.


🧩 1️⃣ Container vs Pod vs Deployment

This is one of the most important Kubernetes concepts β€” and frequently asked in interviews.


🐳 Container

A container is the smallest runnable unit.

Created using Docker:

docker run -it -d -p 80:80 nginx

You manually manage:

  • Ports

  • Volumes

  • Networks

  • Lifecycle

πŸ‘‰ Containers alone do not provide auto-healing or scaling.


πŸ“¦ Pod (Smallest Deployment Unit in Kubernetes)

A Pod is:

  • A wrapper around one or more containers

  • The smallest deployment unit in Kubernetes

  • Defined using YAML

Example: Creating a Pod

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
    - name: nginx
      image: nginx
      ports:
        - containerPort: 80

Apply it:

kubectl apply -f pod.yaml
kubectl get pods

You can check details:

kubectl get pods -o wide

🚨 The Problem With Pods

Now delete the pod:

kubectl delete pod nginx

What happens?

❌ The pod is deleted
❌ No new pod is created
❌ Application becomes unreachable

πŸ‘‰ Pods do not auto-heal.

This is the reason we do NOT use standalone Pods in production.


2️⃣ Why Deployments Are Required

A Deployment is a higher-level Kubernetes resource that manages Pods.

It provides:

  • πŸ”₯ Auto-Healing

  • πŸ“ˆ Auto-Scaling

  • πŸ”„ Rolling Updates

  • βͺ Rollbacks

  • πŸ›‘ High Availability

In real-world production, we always use Deployments instead of directly creating Pods.


3️⃣ Deployment Architecture (How It Actually Works)

Before applying a Deployment, create a YAML file.

Step 1: Create deployment.yaml

vim deployment.yaml

Add the following content.

πŸ“Œ Note: This example is adapted from the official Kubernetes documentation. There is no need to memorize the syntax β€” simply modify the image name, replicas, and labels according to your requirements.

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

Step 2: Apply the Deployment

kubectl apply -f deployment.yaml

Kubernetes now creates:

Deployment β†’ ReplicaSet β†’ Pod

πŸ“Œ ReplicaSet

ReplicaSet is a Kubernetes Controller.

Its job:

  • Ensure the specified number of Pods are always running.

  • Maintain:

Desired State = Actual State

If:

  • Desired replicas = 3

  • Actual running pods = 2

ReplicaSet automatically creates 1 more pod.

This is called πŸ‘‰ Auto-Healing.


πŸ“Œ Pod

  • The actual running container instance

  • Created and managed by ReplicaSet


4️⃣ Demonstrating Auto-Healing (Live Observation)

To properly observe auto-healing, you need two terminals.


πŸ–₯ Terminal 1 β€” Watch Pods in Real Time

Run:

kubectl get pods -w

The -w flag means watch mode.
Kubernetes will continuously stream updates whenever pod status changes.

You might initially see something like:

NAME                                 READY   STATUS    RESTARTS   AGE
nginx-deployment-abc123-xyz01        1/1     Running   0          2m
nginx-deployment-abc123-xyz02        1/1     Running   0          2m
nginx-deployment-abc123-xyz03        1/1     Running   0          2m

πŸ–₯ Terminal 2 β€” Delete One Pod

In another terminal, delete one of the pods:

kubectl delete pod nginx-deployment-abc123-xyz01

πŸ‘€ What You Will Observe in Terminal 1

Immediately, the output begins changing dynamically.

First, the deleted pod transitions:

nginx-deployment-abc123-xyz01   1/1   Terminating   0   2m

Almost instantly, Kubernetes creates a replacement pod:

nginx-deployment-abc123-new456   0/1   Pending              0   0s
nginx-deployment-abc123-new456   0/1   ContainerCreating    0   1s
nginx-deployment-abc123-new456   1/1   Running              0   3s

Meanwhile, the old pod fully exits.


What Just Happened?

You deleted a pod.

The ReplicaSet detected that the running count dropped below the desired replicas and immediately created a replacement.

The new pod moved through:

Pending β†’ ContainerCreating β†’ Running

All of this happened automatically.

Even before the old pod fully terminated, a new one was already being scheduled.


Why This Matters

This is self-healing in action.

Kubernetes continuously ensures:

Desired replicas = Actual running replicas

If there’s any mismatch, it corrects it automatically β€” without manual intervention.

This behavior is the foundation of production-grade Kubernetes systems.


5️⃣ Scaling the Application

One of the key advantages of using a Deployment is the ability to scale your application easily.

You can increase or decrease the number of running Pods without recreating the entire setup.


πŸ”Ή Option 1: Modify the YAML

Edit your deployment.yaml and change:

replicas: 3

Apply the changes:

kubectl apply -f deployment.yaml

Kubernetes will create additional Pods automatically to match the updated replica count.


πŸ”Ή Option 2: Scale Using a Command

You can also scale directly from the command line:

kubectl scale deployment nginx-deployment --replicas=5

Verify:

kubectl get pods

You will now see five running Pods.

Scaling helps with:

  • Load balancing

  • Handling increased traffic

  • Improving fault tolerance


πŸ” Deleting a Pod in a Scaled Setup

Now that multiple replicas are running, delete one Pod:

kubectl delete pod <pod-name>

What happens?

  • The Pod enters Terminating

  • ReplicaSet detects the drop in count

  • A new Pod is created immediately

  • Total replicas remain unchanged

Even in scaled environments, Kubernetes maintains the desired number of Pods automatically.

This ensures continuous availability.


6️⃣ What Is a Kubernetes Controller?

Kubernetes works on a declarative model.

You define the desired state in YAML, and Kubernetes ensures the cluster matches that state.

A Controller is responsible for:

  • Continuously monitoring resources

  • Comparing desired state with actual state

  • Taking corrective action if there is a mismatch

ReplicaSet is one such built-in controller.

It ensures:

Desired replicas = Actual running replicas

If anything changes, it reconciles the difference automatically.


7️⃣ Important kubectl Commands

Here are some commonly used commands when working with Deployments:

kubectl get pods
kubectl get deployments
kubectl get rs
kubectl get all
kubectl get all -A

To delete a Deployment:

kubectl delete deployment nginx-deployment

This removes:

  • The Deployment

  • The ReplicaSet

  • All associated Pods


8️⃣ Container vs Pod vs Deployment

ContainerPodDeployment
Docker runtime unitSmallest Kubernetes unitHigh-level abstraction
Manual managementGroups containersManages Pods
No healingNo healingAuto-healing
No scalingNo scalingScaling + rolling updates

Deployment vs ReplicaSet

DeploymentReplicaSet
User-facing abstractionController resource
Manages updatesMaintains replica count
Supports rollbacksEnsures desired Pods

9️⃣ Production Best Practice

In production environments:

❌ Do not create standalone Pods
βœ… Always use Deployments

Because:

Deployment β†’ Management Layer
ReplicaSet β†’ Enforcement Layer
Pod β†’ Execution Layer

Deployments provide resilience, scalability, and controlled updates β€” all essential for real-world systems.


🏁 Final Key Takeaways

  • Pods run containers but lack resilience.

  • Deployments manage Pods using ReplicaSets.

  • ReplicaSets maintain replica count.

  • Controllers enforce desired state.

  • Scaling ensures high availability.

  • Kubernetes is declarative and self-correcting.

More from this blog

codeops-labs.hashnode.dev

16 posts