https://drive.google.com/file/d/1H1E5ZmpeUGX-fgF4_kl85a0QnShOWGW1/view?usp=sharing
imperative-deployment.sh)#!/bin/sh
# A: Create Deployment with latest nginx
kubectl create deployment nginx-imperative --image=nginx:latest
# B: Scale to 3 replicas
kubectl scale deployment/nginx-imperative --replicas=3
# C & D: Add metadata annotations
kubectl annotate deployment/nginx-imperative environment=prod
kubectl annotate deployment/nginx-imperative organization=sales
π Key Insight:
This script uses imperative commands β you tell Kubernetes what to do step-by-step.
| Aspect | Imperative (kubectl create ...) |
Declarative (kubectl apply -f ...) |
|---|---|---|
| Approach | "Do this, then that" | "Hereβs the desired state β make it so" |
| Audit Trail | β Hard to track changes | β Full history in Git |
| Reproducibility | β Manual steps | β
git clone && kubectl apply |
| Team Collaboration | β Error-prone | β Code-reviewed YAML |
| Rollback | β Manual | β
git revert && kubectl apply |
| Use Case | Quick testing, learning | Production, GitOps, CI/CD |
β Kubernetes Best Practice:
Use declarative YAML for production
Use imperative for quick experiments
# Make script executable
chmod +x imperative-deployment.sh
# Run it
./imperative-deployment.sh
# Verify
kubectl get deployments
# NAME READY UP-TO-DATE AVAILABLE
# nginx-imperative 3/3 3 3
kubectl describe deployment nginx-imperative | grep -A 2 Annotations
# Annotations:
# environment: prod
# organization: sales
# Get the full Deployment spec
kubectl get deployment nginx-imperative -o yaml > nginx-imperative.yaml
# View key parts
grep -A 5 "replicas\\\\|image\\\\|annotations" nginx-imperative.yaml
π Youβll see:
replicas: 3 template: spec: containers: - image: nginx:latest annotations: environment: prod organization: sales
π‘ Now you have a YAML file β you could version-control it!
# Delete imperative Deployment
kubectl delete deployment nginx-imperative
# Recreate from YAML
kubectl apply -f nginx-imperative.yaml
# Result: Identical Deployment!
β This is the power of declarative:
Once you have YAML, youβre in GitOps territory.