K8s Workloads: Mandatory Fields, YAML Templates & Commands

 

✅  Mandatory Fields Table (VERY IMPORTANT ⭐)

ResourceMandatory fields
PodapiVersion, kind, metadata.name, spec.containers[].name, image
ReplicaSetapiVersion, kind, metadata.name, spec.selector, spec.template, containers
DeploymentapiVersion, kind, metadata.name, spec.selector, spec.template, containers


✅ Side-by-Side Comparison (Pod vs RS vs Deployment)

FeaturePodReplicaSetDeployment
PurposeSingle containerMaintain replicasManage RS + rolling updates
Replicas
Self-healing
Rolling update
Rollback
Used in real projectsRareRare⭐ Mostly used
YAML complexitySimpleMediumMedium
CreatesContainersPodsRS + Pods


🔥  Memory Tricks (exam hacks)

🧠 Structure memory

Pod → containers RS → replicas + selector + template Deployment → same as RS + rollout features

🧠 Labels rule (most common mistake)

selector.matchLabels MUST match template.labels

🧠 apiVersion memory

Pod → v1 RS → apps/v1 Deployment → apps/v1


🚀 Bonus: Generate YAML quickly

Super useful trick:

kubectl create deployment nginx --image=nginx --dry-run=client -o yaml
kubectl run nginx --image=nginx --dry-run=client -o yaml

👉 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

#️⃣

kubectl create deployment nginx --image=nginx --dry-run=client -o yaml

✅ 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:

kind: Deployment

🔹 nginx

Name of deployment

Becomes:

metadata: name: nginx

🔹 --image=nginx

Container image

Becomes:

containers: - name: nginx image: nginx

🔹 --dry-run=client ⭐ (VERY IMPORTANT)

Means:

👉 "Don't actually create resource"
👉 "Just simulate locally"

If you remove this:

kubectl create deployment nginx --image=nginx

It will really create deployment ❌

With dry-run:
Only preview ✅


🔹 -o yaml

Output format

Other formats:

-o yaml -o json -o wide -o name

Here:
👉 print YAML


✅ Output looks like

apiVersion: apps/v1 kind: Deployment metadata: name: nginx spec: selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx

✅ Real-world usage

Save to file

kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > deploy.yaml

Edit

vi deploy.yaml

Add:

  • replicas

  • resources

  • ports

  • env

Apply

kubectl apply -f deploy.yaml

🔥 This is how pros do it



🟡 Command 2

#️⃣

kubectl run nginx --image=nginx --dry-run=client -o yaml

✅ What it does

👉 Generates Pod YAML

NOT Deployment
NOT ReplicaSet
Just single Pod


✅ Breakdown

🔹 run

Quick way to run a container

Creates:

kind: Pod

🔹 nginx

Pod name


🔹 --image=nginx

Container image


🔹 --dry-run=client

Preview only


🔹 -o yaml

Print YAML


✅ Output

apiVersion: v1 kind: Pod metadata: name: nginx spec: containers: - name: nginx image: nginx


🔥 Deployment vs Run (IMPORTANT DIFFERENCE)

CommandCreatesUse case
kubectl runPodtesting/debug
kubectl create deploymentDeploymentproduction apps

🎯 When to use which?

✅ For production

kubectl create deployment

Because:

  • replicas

  • scaling

  • rolling update

  • rollback


✅ For quick testing

kubectl run

Because:

  • fast

  • simple

  • temporary



🔥 Pro DevOps Trick (most useful)

Instead of writing YAML manually:

Step 1

kubectl create deployment myapp --image=nginx --dry-run=client -o yaml > deploy.yaml

Step 2

Edit file

Step 3

kubectl apply -f deploy.yaml

👉 100x faster
👉 zero syntax mistakes
👉 interview smart move



🧠 Easy memory line

run → Pod create deployment → Deployment dry-run → preview only -o yaml → print YAML

Comments

Popular posts from this blog

Managing Amazon EBS Volumes and Snapshots Across Regions

Git for Beginners: Complete Guide from Installation to First Push on GitHub

AWS - Amazon Web Services