K8s Workloads: Mandatory Fields, YAML Templates & Commands
✅ Mandatory Fields Table (VERY IMPORTANT ⭐)
| Resource | Mandatory fields |
|---|---|
| Pod | apiVersion, kind, metadata.name, spec.containers[].name, image |
| ReplicaSet | apiVersion, kind, metadata.name, spec.selector, spec.template, containers |
| Deployment | apiVersion, kind, metadata.name, spec.selector, spec.template, containers |
✅ Side-by-Side Comparison (Pod vs RS vs Deployment)
| Feature | Pod | ReplicaSet | Deployment |
|---|---|---|---|
| Purpose | Single container | Maintain replicas | Manage RS + rolling updates |
| Replicas | ❌ | ✅ | ✅ |
| Self-healing | ❌ | ✅ | ✅ |
| Rolling update | ❌ | ❌ | ✅ |
| Rollback | ❌ | ❌ | ✅ |
| Used in real projects | Rare | Rare | ⭐ Mostly used |
| YAML complexity | Simple | Medium | Medium |
| Creates | Containers | Pods | RS + Pods |
🔥 Memory Tricks (exam hacks)
🧠 Structure memory
🧠 Labels rule (most common mistake)
🧠 apiVersion memory
🚀 Bonus: Generate YAML quickly
Super useful trick:
👉 No need to memorize full YAML — generate + edit 😄
🔥 Big Idea First (why we use this)
Normally:
❌ Manually writing YAML → error-prone
-
wrong indentation
-
forgot selector
-
wrong apiVersion
-
syntax mistakes
Instead:
✅ Let Kubernetes generate perfect YAML for you
Then:
👉 save → edit → apply
🟢 Command 1
#️⃣
✅ What it does (simple)
👉 Generates Deployment YAML
👉 Does NOT create anything
👉 Prints YAML to screen
✅ Breakdown word by word
🔹 kubectl
Kubernetes CLI tool
🔹 create deployment
Tells kubectl:
👉 "I want to create a Deployment resource"
So it auto builds:
🔹 nginx
Name of deployment
Becomes:
🔹 --image=nginx
Container image
Becomes:
🔹 --dry-run=client ⭐ (VERY IMPORTANT)
Means:
👉 "Don't actually create resource"
👉 "Just simulate locally"
If you remove this:
It will really create deployment ❌
With dry-run:
Only preview ✅
🔹 -o yaml
Output format
Other formats:
Here:
👉 print YAML
✅ Output looks like
✅ Real-world usage
Save to file
Edit
Add:
-
replicas
-
resources
-
ports
-
env
Apply
🔥 This is how pros do it
🟡 Command 2
#️⃣
✅ What it does
👉 Generates Pod YAML
NOT Deployment
NOT ReplicaSet
Just single Pod
✅ Breakdown
🔹 run
Quick way to run a container
Creates:
🔹 nginx
Pod name
🔹 --image=nginx
Container image
🔹 --dry-run=client
Preview only
🔹 -o yaml
Print YAML
✅ Output
🔥 Deployment vs Run (IMPORTANT DIFFERENCE)
| Command | Creates | Use case |
|---|---|---|
| kubectl run | Pod | testing/debug |
| kubectl create deployment | Deployment | production apps |
🎯 When to use which?
✅ For production
Because:
-
replicas
-
scaling
-
rolling update
-
rollback
✅ For quick testing
Because:
-
fast
-
simple
-
temporary
🔥 Pro DevOps Trick (most useful)
Instead of writing YAML manually:
Step 1
Step 2
Edit file
Step 3
👉 100x faster
👉 zero syntax mistakes
👉 interview smart move
Comments
Post a Comment