βΈοΈ 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
- Pods having label
β οΈ 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:
| Operator | Meaning |
|---|---|
| In | Value must be in list |
| NotIn | Value must NOT be in list |
| Exists | Key must exist |
| DoesNotExist | Key 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: DoesNotExistImportant
ALL conditions must be satisfied (AND logic)
β οΈ RC vs RS
| Feature | RC | RS |
|---|---|---|
| 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-deploymentRollback 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
| Revision | Image | Replicas |
|---|---|---|
| RS1 | nginx:1.19 | 0 |
| RS2 | nginx:1.20 | 0 |
| RS3 | nginx:1.21 | 3 |
β οΈ 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 βΈοΈ