βΈοΈ Imperative Commands (kubectl Fast Ops)
Video Overview
This note covers:
- Why imperative commands are important
- Creating deployments & services quickly
- Using
--dry-runto 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)
| Type | Meaning |
|---|---|
| Imperative | HOW to do |
| Declarative | WHAT 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`