Day 24 - Kubernetes β Deployments, and ReplicaSets
π§± 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:
Creates an internal resource called a ReplicaSet
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 Podstemplate:β 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)
| Concept | Description | Key Feature |
| Container | Basic runtime unit (e.g., Docker) | Runs app processes |
| Pod | Wrapper around one or more containers | Adds networking & storage sharing |
| Deployment | Manages Pods via ReplicaSet | Adds auto-healing & scaling |
Summary:
Container β Pod β Deployment
Each layer adds management capability on top of the previous one.
π§© 10. Deployment vs ReplicaSet
| Concept | Description | Role |
| Deployment | High-level abstraction | Defines desired replicas & updates |
| ReplicaSet | Kubernetes Controller | Actually 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 PodIf 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 β PodIf 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.yamlReplicaSet creates two new Pods (scaling)
β All without downtime.
βοΈ 12. Common kubectl Commands
| Action | Command |
| Create Pod/Deployment | kubectl apply -f <file>.yaml |
| List Pods | kubectl get pods |
| List Deployments | kubectl get deploy |
| List ReplicaSets | kubectl get rs |
| Delete Pod | kubectl delete pod <name> |
| Watch live events | kubectl get pods -w |
| Describe a resource | kubectl 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 rsto verify ReplicaSet creation
π§ 15. Key Interview Questions
Difference between Container, Pod, and Deployment
Difference between Deployment and ReplicaSet
What is a Controller in Kubernetes?
How does auto-healing work in Kubernetes?
What ensures desired vs actual state?
β 16. Summary of Key Concepts
| Component | Function | Example Resource |
| Container | Runs app process | Docker |
| Pod | Runs 1 or more containers | pod.yaml |
| ReplicaSet | Ensures desired Pod count | rs |
| Deployment | Manages Pods via ReplicaSet | deployment.yaml |
| Controller | Maintains desired state | ReplicaSet Controller |
π’ Final Flow:Deployment β ReplicaSet β Pod β Container