☸️ Imperative Commands (kubectl Fast Ops)

Video Overview

This note covers:

  • Why imperative commands are important
  • Creating deployments & services quickly
  • Using --dry-run to generate YAML
  • Testing connectivity using test pods

πŸ“Œ VERY IMPORTANT for CKA (time-saving tricks)


⚑ Concept 1: Why Imperative Commands?

Important


πŸ”§ Use Cases:

πŸ› οΈ 1. Troubleshooting

  • Quickly create test pods
  • Check connectivity
  • Validate network policies :contentReference[oaicite:0]{index=0}

⏱️ 2. CKA Exam

  • Faster than writing YAML manually
  • Generate YAML β†’ edit β†’ apply

πŸš€ 3. Real World

  • Temporary testing
  • Debugging deployments
  • Verifying configs

βš™οΈ Imperative vs Declarative (Quick Recall)

TypeMeaning
ImperativeHOW to do
DeclarativeWHAT you want

Quote

Imperative = speed ⚑
Declarative = production πŸ“œ


πŸ§ͺ Concept 2: Create Deployment Imperatively

Example

kubectl create deployment backend-deploy \
  --image=hashicorp/http-echo \
  --replicas=3 \
  --port=5678 \
  --dry-run=client -o yaml > backend-deploy.yaml

🧠 Key Flags:

  • --image β†’ container image
  • --replicas β†’ number of pods
  • --port β†’ container port (doc purpose)
  • --dry-run=client β†’ validate only
  • -o yaml β†’ output YAML

🎯 Benefit:

  • Generates ready YAML β†’ edit β†’ apply

✏️ Editing YAML (Important)

  • Remove unnecessary fields
  • Fix labels:
labels:
  app: backend

  • Add arguments if needed:
args:
  - "-text=Hello from backend"

πŸš€ Apply Deployment

kubectl apply -f backend-deploy.yaml

🌐 Concept 3: Expose Deployment (ClusterIP)

Example

kubectl expose deployment backend-deploy \
  --type=ClusterIP \
  --port=9090 \
  --target-port=5678 \
  --name=backend-svc \
  --dry-run=client -o yaml >> backend-deploy.yaml

🧠 Key Idea:

Quote

expose = create service from deployment


πŸ“¦ Concept 4: Frontend Deployment

kubectl create deployment frontend-deploy \
  --image=nginx \
  --replicas=3 \
  --port=80 \
  --dry-run=client -o yaml > frontend-deploy.yaml

🌐 Concept 5: NodePort Service (Frontend)

kubectl expose deployment frontend-deploy \
  --type=NodePort \
  --port=80 \
  --target-port=80 \
  --name=frontend-svc \
  --dry-run=client -o yaml >> frontend-deploy.yaml

⚠️ Important:

Warning

NodePort is NOT auto-set in YAML

πŸ‘‰ Add manually:

nodePort: 31000

πŸ§ͺ Concept 6: Test Pod (VERY IMPORTANT)

Success

Used for debugging & testing connectivity


βš™οΈ Command:

kubectl run test-pod \
  --image=nginx \
  -it --rm \
  --restart=Never \
  --labels="app=test" \
  -- /bin/sh

🧠 Flags:

  • -it β†’ interactive terminal
  • --rm β†’ delete after exit
  • --restart=Never β†’ standalone pod
  • --labels β†’ assign label

πŸ”— Test Connectivity

curl backend-svc:9090

βœ… Output:

Hello from backend

🧠 Concept 7: DNS Resolution (CoreDNS)

Important


πŸ“Œ Inside Pod:

cat /etc/resolv.conf

🧠 Output:

  • Nameserver β†’ Cluster DNS IP
  • Usually:
10.96.0.10

πŸ” Verify DNS Service:

kubectl get svc -n kube-system

🧠 Key Insight:

Quote

CoreDNS resolves service names β†’ IPs


πŸ” Flow Recap

Example

Pod β†’ backend-svc β†’ CoreDNS β†’ ClusterIP β†’ Pod

⚠️ Important Observations

Warning

  • Imperative commands are FAST
  • YAML is still needed for production
  • NodePort must be manually added
  • Labels are critical for selection

🧠 Final Takeaways

Summary

  • Use imperative for:

    • Testing
    • Debugging
    • Exam speed
  • Use declarative for:

    • Production
    • Version control

πŸ“Œ One-Line Summary

Quote

Use kubectl commands to move fast, YAML to stay correct ☸️

---x`