☸️ Day 10: Replication Controller, ReplicaSet & Deployments

Video Overview

This note covers how Kubernetes manages workloads using:

  • Replication Controllers
  • ReplicaSets
  • Deployments
  • Selectors (Equality & Set-based)

πŸ“Œ Core idea: Maintain desired number of Pods automatically


πŸ“¦ Concept 1: Why Controllers?

Important

Pods alone are NOT reliable

❌ Problem:

  • Pod crashes β†’ no auto-restart
  • No scaling
  • No management

βœ… Solution:

  • Use Controllers
  • Ensure:
    • High availability
    • Self-healing
    • Scaling

πŸ” Concept 2: Replication Controller (RC)

Info

First method to manage multiple pods


🧠 What it does:

  • Maintains desired number of pods
  • If pod dies β†’ creates new one

πŸ“„ RC YAML Structure

apiVersion: v1
kind: ReplicationController
 
metadata:
  name: nginx-rc
 
spec:
  replicas: 3
 
  selector:
    app: nginx
 
  template:
    metadata:
      labels:
        app: nginx
 
    spec:
      containers:
        - name: nginx-container
          image: nginx

🎯 Key Concept: Labels & Selectors

Important

RC works ONLY based on labels

  • Selector:
app: nginx
  • It manages:
    • Pods having label app=nginx

⚠️ Important Behavior

Warning

  • Existing pods with matching labels are also managed
  • RC may NOT create new pods if matching ones already exist

βš™οΈ Useful Commands (RC)

kubectl get rc
kubectl describe rc nginx-rc
kubectl scale rc nginx-rc --replicas=4
kubectl set image rc nginx-rc nginx-container=nginx:1.22
kubectl edit rc nginx-rc

πŸš€ Concept 3: ReplicaSet (RS)

Important

Modern replacement of Replication Controller


🧠 Key Difference:

  • Supports Set-based selectors (more powerful)

πŸ“„ RS YAML

apiVersion: apps/v1
kind: ReplicaSet
 
metadata:
  name: nginx-rs
 
spec:
  replicas: 3
 
  selector:
    matchLabels:
      app: nginx
 
  template:
    metadata:
      labels:
        app: nginx
 
    spec:
      containers:
        - name: nginx-container
          image: nginx

πŸ” Concept 4: Types of Selectors


πŸ”Ή 1. Equality-Based Selector

Example

matchLabels:
  app: nginx
  • Exact match required

πŸ”Ή 2. Set-Based Selector

Example

matchExpressions:
  - key: app
    operator: In
    values:
      - nginx
      - apache

πŸ”‘ Operators:

OperatorMeaning
InValue must be in list
NotInValue must NOT be in list
ExistsKey must exist
DoesNotExistKey must NOT exist

πŸ’‘ Example (Advanced)

matchExpressions:
  - key: app
    operator: In
    values: [nginx, apache]
 
  - key: environment
    operator: NotIn
    values: [development]
 
  - key: tier
    operator: Exists
 
  - key: debug
    operator: DoesNotExist

Important

ALL conditions must be satisfied (AND logic)


⚠️ RC vs RS

FeatureRCRS
Equality Selectorβœ… Yesβœ… Yes
Set-Based Selector❌ Noβœ… Yes
Usage Today❌ Deprecated⚠️ Rarely used

πŸš€ Concept 5: Deployment (MOST IMPORTANT)

Success

Deployment = Highest-level controller (used in real world)


🧠 What it does:

  • Manages ReplicaSets
  • Provides:
    • Rolling updates
    • Rollbacks
    • Version control

πŸ“„ Deployment YAML

apiVersion: apps/v1
kind: Deployment
 
metadata:
  name: nginx-deployment
  annotations:
    kubernetes.io/change-cause: "Initial release"
 
spec:
  replicas: 3
 
  selector:
    matchLabels:
      app: nginx
 
  template:
    metadata:
      labels:
        app: nginx
 
    spec:
      containers:
        - name: nginx-container
          image: nginx:1.19

πŸ”„ Concept 6: Rolling Updates

Important

Zero-downtime updates


πŸ”§ Update Image

kubectl set image deployment nginx-deployment nginx-container=nginx:1.20

πŸ“œ Check History

kubectl rollout history deployment nginx-deployment

🏁 Output Example

  • Revision 1 β†’ nginx:1.19
  • Revision 2 β†’ nginx:1.20

πŸ” Concept 7: Rollback

Important

Rollback to Previous

kubectl rollout undo deployment nginx-deployment

Rollback to Specific Version

kubectl rollout undo deployment nginx-deployment --to-revision=1

🧠 How Deployment Works Internally

Info

  • Each update creates:
    • New ReplicaSet
  • Old ReplicaSet:
    • Scaled down to 0
  • New ReplicaSet:
    • Scaled up

πŸ“Š Example

RevisionImageReplicas
RS1nginx:1.190
RS2nginx:1.200
RS3nginx:1.213

⚠️ Imperative vs Declarative (Reminder)

Warning

  • Imperative β†’ quick fixes
  • Declarative β†’ production

🧠 Final Takeaways

Summary

  • RC β†’ old method
  • RS β†’ improved (selectors)
  • Deployment β†’ real-world usage
  • Labels + Selectors = core concept
  • Deployment manages ReplicaSets

πŸ“Œ One-Line Summary

Quote

Deployment β†’ manages ReplicaSets β†’ manages Pods ☸️