https://drive.google.com/file/d/1Mq5KYEMXCQpTEUPk2sIZfgXHgbiTcq-X/view?usp=sharing

πŸ” Current YAML Breakdown

apiVersion: apps/v1
kind: Deployment
meta
  name: nginx-declarative
  annotations:
    environment: prod
    organization: sales
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx          # ← Good: uses standard 'app' label
  template:
    meta
      labels:
        app: nginx         # ← Must match selector
    spec:
      containers:
      - name: nginx
        image: nginx:latest  # ⚠️ Problem!

βœ… What’s Good:

⚠️ What’s Missing (vs deployment-one.yml):

  1. :latest tag β†’ non-deterministic
  2. No resource requests/limits β†’ can’t use HPA, poor scheduling
  3. No liveness/readiness probes β†’ no health checks
  4. No update strategy β†’ uses default (maxSurge: 25%, maxUnavailable: 25%)
  5. No ports declaration β†’ optional but good practice

πŸ› οΈ Enhanced Production-Ready Version

Here’s how to upgrade this for production:

# declarative-deployment.yaml (ENHANCED)
apiVersion: apps/v1
kind: Deployment
meta
  name: nginx-declarative
  annotations:
    environment: prod
    organization: sales
    kubernetes.io/change-cause: "nginx 1.25 with health checks"
spec:
  replicas: 3
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1          # +1 extra during update
      maxUnavailable: 0    # zero downtime
  selector:
    matchLabels:
      app: nginx
  template:
    meta
      labels:
        app: nginx
        version: v1.25     # ← Optional: track version in labels
    spec:
      containers:
      - name: nginx
        image: nginx:1.25-alpine  # βœ… Specific, lightweight tag
        ports:
        - containerPort: 80
          name: http
        resources:
          requests:
            cpu: "100m"
            memory: "128Mi"
          limits:
            cpu: "200m"
            memory: "256Mi"
        livenessProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 10
          periodSeconds: 20
        readinessProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 5
          periodSeconds: 10

βœ… Key Improvements:


πŸ§ͺ k3s Lab: Deploy & Compare

πŸ”§ Step 1: Deploy the Basic Version

# Apply basic declarative Deployment
kubectl apply -f declarative-deployment.yaml

# Check Pods
kubectl get pods -l app=nginx

πŸ”§ Step 2: Upgrade to Enhanced Version

πŸ’‘ Save the enhanced YAML as declarative-deployment-enhanced.yaml

kubectl apply -f declarative-deployment-enhanced.yaml

# Watch zero-downtime rollout
kubectl rollout status deployment nginx-declarative

πŸ”§ Step 3: Verify Health Checks

# Simulate app crash (delete index.html)
kubectl exec <pod-name> -- rm /usr/share/nginx/html/index.html

# Watch liveness probe fail β†’ container restart
kubectl describe pod <pod-name> | grep -A 5 "Liveness"

πŸ”§ Step 4: Clean Up