https://drive.google.com/file/d/1Mq5KYEMXCQpTEUPk2sIZfgXHgbiTcq-X/view?usp=sharing
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:
- Uses standard
applabel (better thanrun)- Includes annotations for metadata
- Clean, version-controllable YAML
β οΈ Whatβs Missing (vs deployment-one.yml):
:latesttag β non-deterministic- No resource requests/limits β canβt use HPA, poor scheduling
- No liveness/readiness probes β no health checks
- No update strategy β uses default (
maxSurge: 25%,maxUnavailable: 25%)- No ports declaration β optional but good practice
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:
- Specific image tag (
1.25-alpine)- Resource requests/limits
- Liveness + Readiness probes
- Zero-downtime strategy
- Standardized labels
# Apply basic declarative Deployment
kubectl apply -f declarative-deployment.yaml
# Check Pods
kubectl get pods -l app=nginx
π‘ 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
# 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"