Skip to main content

Command Palette

Search for a command to run...

Day 24 - Kubernetes – Deployments, and ReplicaSets

Updated
β€’6 min read

🧱 1. From Docker Containers to Kubernetes

You can create containers using any platform β€” for example, Docker.

In Docker:

You usually run a container using commands like:

docker run -it image_name
docker run -d -p 8080:80 -v /data:/app --network mynet nginx

Here you specify options such as:

  • -p β†’ port mapping

  • -v β†’ volumes

  • --network β†’ network type

This is the command-line specification for running a container.


βš™οΈ 2. Kubernetes Simplifies This – YAML Manifest

Kubernetes introduced an enterprise-level approach to manage containers.

Instead of writing long commands, Kubernetes allows you to define everything in a YAML manifest file (for example, pod.yaml).

In the YAML file, you define:

  • Container image name

  • Ports to expose

  • Volumes

  • Network details

This YAML file acts as a running specification for your container.

So:

🟩 Pod YAML = Declarative specification to run your container in Kubernetes


πŸ“¦ 3. What is a Pod?

A Pod is the smallest deployable unit in Kubernetes.
It’s a wrapper around one or more containers.

A Pod defines how the container should run β€” image, ports, volumes, and other configurations.


🧩 Single vs Multiple Containers in a Pod

  • A Pod can contain a single container (most common).

  • It can also contain multiple containers if they are tightly coupled β€” for example:

    • One main application container

    • One sidecar container (for logging, proxy, API gateway, etc.)

πŸ•ΈοΈ Why multiple containers in a Pod?

Because:

  • They share the same network (can communicate using localhost)

  • They share the same storage volumes

πŸ“˜ Example use case:
Service Mesh β€” one main app container and one sidecar container for service routing.


⚑ 4. Why Do We Need Deployments?

Now that you can deploy an app using a Pod, the next question is:

Why move from Pod to Deployment?

Because Pods cannot auto-heal or auto-scale.

Kubernetes provides features like:

  • Auto-healing β†’ restart failed Pods automatically

  • Auto-scaling β†’ increase/decrease Pods based on load

Pods alone cannot do this.
To achieve these behaviors, Kubernetes uses a Deployment.


πŸš€ 5. What is a Deployment?

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

When you create a Deployment, it:

  1. Creates an internal resource called a ReplicaSet

  2. The ReplicaSet then creates and manages Pods

🧠 Flow:
Deployment β†’ ReplicaSet β†’ Pod


πŸ“„ Deployment YAML (simplified example)

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

Here:

  • replicas: 2 β†’ desired number of Pods

  • template: β†’ defines the Pod specification


🧩 6. What is a ReplicaSet?

A ReplicaSet is a Kubernetes controller that ensures the desired number of Pods are always running.

If you delete or lose a Pod:

  • The ReplicaSet will immediately recreate it.

Example:

If Deployment says replicas: 2:

  • ReplicaSet ensures 2 Pods are always running.

  • If one is deleted, a new one is automatically created.

β†’ This is called auto-healing.


πŸ” 7. How Deployments Enable Zero Downtime

If you update the replicas value from 2 β†’ 3 in your YAML and apply it again:

kubectl apply -f deployment.yaml

Kubernetes:

  • Notices the change

  • Automatically creates one new Pod (via ReplicaSet)

  • Keeps existing Pods running

βœ… This ensures zero downtime and smooth scaling.


🧠 8. Controller Concept in Kubernetes

A Controller in Kubernetes maintains the desired state of the cluster.

For example:

  • If Deployment YAML says 3 replicas β†’ the controller ensures 3 Pods are always running.

Controllers constantly watch and reconcile:

Desired State (YAML) = Actual State (cluster)

Examples of controllers:

  • ReplicaSet Controller

  • Deployment Controller

  • DaemonSet Controller

  • Custom Controllers (like ArgoCD, Admission Controllers)


🧩 9. Pod vs Container vs Deployment (Interview Key Question)

ConceptDescriptionKey Feature
ContainerBasic runtime unit (e.g., Docker)Runs app processes
PodWrapper around one or more containersAdds networking & storage sharing
DeploymentManages Pods via ReplicaSetAdds auto-healing & scaling

Summary:

Container β†’ Pod β†’ Deployment
Each layer adds management capability on top of the previous one.


🧩 10. Deployment vs ReplicaSet

ConceptDescriptionRole
DeploymentHigh-level abstractionDefines desired replicas & updates
ReplicaSetKubernetes ControllerActually creates & maintains Pods

πŸ“˜ Think of it like this:
Deployment = Manager
ReplicaSet = Worker ensuring Pods match Deployment instructions


🧩 11. Real-Time Demo Behavior (as explained)

  • kubectl apply -f pod.yaml β†’ creates one Pod

  • If you kubectl delete pod <name> β†’ Pod is gone; app stops

❌ No auto-healing in a Pod.


βœ… With Deployment:

  • kubectl apply -f deployment.yaml β†’ creates Deployment β†’ ReplicaSet β†’ Pod

  • If you delete one Pod:

      kubectl delete pod <name>
    

    Immediately, ReplicaSet recreates a new Pod (auto-healing)

  • If you increase replicas from 1 β†’ 3:

      kubectl apply -f deployment.yaml
    

    ReplicaSet creates two new Pods (scaling)

β†’ All without downtime.


βš™οΈ 12. Common kubectl Commands

ActionCommand
Create Pod/Deploymentkubectl apply -f <file>.yaml
List Podskubectl get pods
List Deploymentskubectl get deploy
List ReplicaSetskubectl get rs
Delete Podkubectl delete pod <name>
Watch live eventskubectl get pods -w
Describe a resourcekubectl describe pod <name>

πŸ“ˆ 13. Zero Downtime Example Summary

  • Deleted Pod β†’ instantly recreated (auto-healing)

  • Increased replicas β†’ new Pods created automatically (scaling)

  • All without affecting users β†’ zero downtime


🧩 14. Practical Assignment

  • Create a Deployment YAML with your own image

  • Apply it to cluster

  • Delete a Pod β†’ observe auto-healing

  • Increase replicas β†’ observe scaling

  • Use kubectl get rs to verify ReplicaSet creation


🧠 15. Key Interview Questions

  1. Difference between Container, Pod, and Deployment

  2. Difference between Deployment and ReplicaSet

  3. What is a Controller in Kubernetes?

  4. How does auto-healing work in Kubernetes?

  5. What ensures desired vs actual state?


βœ… 16. Summary of Key Concepts

ComponentFunctionExample Resource
ContainerRuns app processDocker
PodRuns 1 or more containerspod.yaml
ReplicaSetEnsures desired Pod countrs
DeploymentManages Pods via ReplicaSetdeployment.yaml
ControllerMaintains desired stateReplicaSet Controller

🟒 Final Flow:
Deployment β†’ ReplicaSet β†’ Pod β†’ Container

More from this blog

Dinesh's Blog

104 posts