[4/24] D is for Deployments: Managing Application Lifecycle
π This is Post #4 in the Kubernetes A-to-Z Series
Reading Order: β Previous: Pods β Next: Services β
Series Progress: 4/24 complete | Difficulty: Intermediate | Time: 20 min | Part 2/6: Core Workloads
Welcome to the fourth post in our Kubernetes A-to-Z Series! Now that you understand Pods, letβs explore Deployments - the most common way to manage applications in Kubernetes. Deployments provide declarative updates to applications, handling rolling updates, rollbacks, and scaling with minimal downtime.
What is a Deployment?
A Deployment is a Kubernetes resource that manages the lifecycle of your application. It provides declarative updates for Pods and ReplicaSets, ensuring your application runs reliably and can be updated with zero downtime.
Deployment vs Pod
Pod (Manual Management):
βββββββββββββββββββββββββββββββββββββββ
β Pod 1 β
β ββββββββββββ β
β βContainer β β
β β(my-app) β β
β ββββββββββββ β
βββββββββββββββββββββββββββββββββββββββ
Manual updates, no rollback capability
Deployment (Automated Management):
βββββββββββββββββββββββββββββββββββββββ
β Deployment Controller β
β βββββββββββββββββββββββββββββββ β
β β ReplicaSet β β
β β βββββββ βββββββ βββββββ β β
β β βPod1 β βPod2 β βPod3 β β β
β β βββββββ βββββββ βββββββ β β
β βββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββ
Automated updates, rollbacks, scaling
Key Deployment Features
- Rolling Updates: Update applications without downtime
- Rollbacks: Revert to previous versions instantly
- Scaling: Scale applications up or down easily
- Self-Healing: Automatically replace failed pods
- Version Management: Track and manage application versions
Creating Your First Deployment
Basic Deployment YAML
# nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.21
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
Creating and Managing Deployments
# Create deployment
kubectl apply -f nginx-deployment.yaml
# Get deployment information
kubectl get deployments
kubectl get deployment nginx-deployment
# Describe deployment
kubectl describe deployment nginx-deployment
# Get deployment status
kubectl rollout status deployment/nginx-deployment
# View deployment history
kubectl rollout history deployment/nginx-deployment
Deployment Strategies
1. Rolling Update (Default)
Updates pods gradually, one by one:
# rolling-update-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp-deployment
spec:
replicas: 5
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1 # Maximum pods that can be unavailable
maxSurge: 1 # Maximum pods that can be created above desired replicas
selector:
matchLabels:
app: webapp
template:
metadata:
labels:
app: webapp
spec:
containers:
- name: webapp
image: myapp:v1.0
ports:
- containerPort: 8080
2. Recreate Strategy
Terminates all old pods before creating new ones:
# recreate-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp-deployment
spec:
replicas: 3
strategy:
type: Recreate
selector:
matchLabels:
app: webapp
template:
metadata:
labels:
app: webapp
spec:
containers:
- name: webapp
image: myapp:v1.0
ports:
- containerPort: 8080
Performing Rolling Updates
Update Image Version
# Update deployment to new image version
kubectl set image deployment/webapp-deployment webapp=myapp:v2.0
# Check rollout status
kubectl rollout status deployment/webapp-deployment
# View rollout history
kubectl rollout history deployment/webapp-deployment
Update via YAML
# updated-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp-deployment
spec:
replicas: 5
selector:
matchLabels:
app: webapp
template:
metadata:
labels:
app: webapp
spec:
containers:
- name: webapp
image: myapp:v2.0 # Updated version
ports:
- containerPort: 8080
env:
- name: VERSION
value: "v2.0"
# Apply the update
kubectl apply -f updated-deployment.yaml
# Monitor the rollout
kubectl rollout status deployment/webapp-deployment
Rollback Capabilities
Rolling Back to Previous Version
# View rollout history
kubectl rollout history deployment/webapp-deployment
# Rollback to previous version
kubectl rollout undo deployment/webapp-deployment
# Rollback to specific revision
kubectl rollout undo deployment/webapp-deployment --to-revision=2
# Check rollback status
kubectl rollout status deployment/webapp-deployment
Rollback Example
# Simulate a bad deployment
kubectl set image deployment/webapp-deployment webapp=myapp:broken
# Check if pods are failing
kubectl get pods -l app=webapp
# Rollback immediately
kubectl rollout undo deployment/webapp-deployment
# Verify rollback
kubectl get pods -l app=webapp
kubectl describe deployment webapp-deployment
Advanced Deployment Features
1. Deployment with Probes
# deployment-with-probes.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp-deployment
spec:
replicas: 3
selector:
matchLabels:
app: webapp
template:
metadata:
labels:
app: webapp
spec:
containers:
- name: webapp
image: myapp:v1.0
ports:
- containerPort: 8080
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
2. Deployment with Resource Limits
# deployment-with-resources.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp-deployment
spec:
replicas: 3
selector:
matchLabels:
app: webapp
template:
metadata:
labels:
app: webapp
spec:
containers:
- name: webapp
image: myapp:v1.0
ports:
- containerPort: 8080
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
3. Deployment with ConfigMaps and Secrets
# deployment-with-config.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp-deployment
spec:
replicas: 3
selector:
matchLabels:
app: webapp
template:
metadata:
labels:
app: webapp
spec:
containers:
- name: webapp
image: myapp:v1.0
ports:
- containerPort: 8080
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: db-secret
key: url
- name: APP_CONFIG
valueFrom:
configMapKeyRef:
name: app-config
key: config.yaml
Scaling Deployments
Manual Scaling
# Scale deployment to 5 replicas
kubectl scale deployment webapp-deployment --replicas=5
# Scale multiple deployments
kubectl scale deployment webapp-deployment frontend-deployment --replicas=3
Autoscaling with HPA
# hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: webapp-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: webapp-deployment
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
# Apply HPA
kubectl apply -f hpa.yaml
# Check HPA status
kubectl get hpa webapp-hpa
# Watch HPA in action
kubectl get hpa webapp-hpa --watch
Deployment Patterns and Best Practices
1. Blue-Green Deployment
# Create blue deployment
kubectl apply -f deployment-blue.yaml
# Test blue version
kubectl port-forward deployment/webapp-blue 8080:8080
# Switch to green
kubectl apply -f deployment-green.yaml
kubectl set selector service/webapp app=webapp,version=green
2. Canary Deployment
# canary-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp-canary
spec:
replicas: 1 # Small percentage of traffic
selector:
matchLabels:
app: webapp
version: canary
template:
metadata:
labels:
app: webapp
version: canary
spec:
containers:
- name: webapp
image: myapp:v2.0
ports:
- containerPort: 8080
3. A/B Testing with Deployments
# ab-test-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp-version-a
spec:
replicas: 5
selector:
matchLabels:
app: webapp
version: A
template:
metadata:
labels:
app: webapp
version: A
spec:
containers:
- name: webapp
image: myapp:version-a
env:
- name: VERSION
value: "A"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp-version-b
spec:
replicas: 5
selector:
matchLabels:
app: webapp
version: B
template:
metadata:
labels:
app: webapp
version: B
spec:
containers:
- name: webapp
image: myapp:version-b
env:
- name: VERSION
value: "B"
Deployment Troubleshooting
Common Issues and Solutions
# Check deployment status
kubectl get deployment webapp-deployment
# Describe deployment for events
kubectl describe deployment webapp-deployment
# Check rollout status
kubectl rollout status deployment/webapp-deployment
# View rollout history
kubectl rollout history deployment/webapp-deployment
# Check replica sets
kubectl get replicasets -l app=webapp
# Check pods
kubectl get pods -l app=webapp
# Check pod logs
kubectl logs -l app=webapp --tail=50
# Check events
kubectl get events --field-selector involvedObject.name=webapp-deployment
Debugging Deployment Issues
# Check if deployment is stuck
kubectl rollout status deployment/webapp-deployment
# Force rollout restart
kubectl rollout restart deployment/webapp-deployment
# Pause rollout
kubectl rollout pause deployment/webapp-deployment
# Resume rollout
kubectl rollout resume deployment/webapp-deployment
Deployment Commands Reference
# Create deployment
kubectl create deployment webapp --image=myapp:v1.0
# Get deployments
kubectl get deployments
kubectl get deployment webapp-deployment
# Update deployment
kubectl set image deployment/webapp-deployment webapp=myapp:v2.0
kubectl patch deployment webapp-deployment -p '{"spec":{"replicas":5}}'
# Scale deployment
kubectl scale deployment webapp-deployment --replicas=5
# Rollout management
kubectl rollout status deployment/webapp-deployment
kubectl rollout history deployment/webapp-deployment
kubectl rollout undo deployment/webapp-deployment
kubectl rollout pause deployment/webapp-deployment
kubectl rollout resume deployment/webapp-deployment
kubectl rollout restart deployment/webapp-deployment
# Delete deployment
kubectl delete deployment webapp-deployment
Real-World Deployment Example
Complete Production Deployment
# production-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp-production
labels:
app: webapp
environment: production
spec:
replicas: 5
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
selector:
matchLabels:
app: webapp
environment: production
template:
metadata:
labels:
app: webapp
environment: production
spec:
containers:
- name: webapp
image: myregistry/webapp:v1.2.3
ports:
- containerPort: 8080
name: http
env:
- name: NODE_ENV
value: "production"
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: production-db
key: connection-string
resources:
requests:
memory: "512Mi"
cpu: "500m"
limits:
memory: "1Gi"
cpu: "1000m"
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
failureThreshold: 3
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
timeoutSeconds: 3
successThreshold: 1
volumeMounts:
- name: config
mountPath: /etc/config
readOnly: true
volumes:
- name: config
configMap:
name: webapp-config
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- webapp
topologyKey: kubernetes.io/hostname
Key Takeaways
- Deployments are the primary way to manage applications in Kubernetes
- Rolling updates allow zero-downtime deployments
- Rollbacks provide instant recovery from failed deployments
- Scaling can be manual or automatic with HPA
- Health probes ensure only healthy pods receive traffic
- Resource management prevents resource contention
- Deployment strategies include rolling, recreate, blue-green, and canary
Command Reference Cheatsheet
# Deployment Management
kubectl create deployment webapp --image=myapp:v1.0
kubectl get deployments
kubectl describe deployment webapp-deployment
kubectl scale deployment webapp-deployment --replicas=5
# Updates and Rollouts
kubectl set image deployment/webapp-deployment webapp=myapp:v2.0
kubectl rollout status deployment/webapp-deployment
kubectl rollout history deployment/webapp-deployment
kubectl rollout undo deployment/webapp-deployment
kubectl rollout pause deployment/webapp-deployment
kubectl rollout resume deployment/webapp-deployment
# Scaling and Autoscaling
kubectl scale deployment webapp-deployment --replicas=10
kubectl autoscale deployment webapp-deployment --min=2 --max=10 --cpu-percent=70
kubectl get hpa
# Debugging
kubectl get pods -l app=webapp
kubectl logs -l app=webapp --tail=50
kubectl get events --field-selector involvedObject.name=webapp-deployment
Next Steps
Now that you understand Deployments and application lifecycle management, youβre ready to explore Services in the next post. Weβll learn how to expose your applications to the network and implement service discovery for reliable communication between components.
Resources for Further Learning
- Official Kubernetes Deployment Documentation
- Deployment Strategies Guide
- Rolling Update Best Practices
- Production Deployment Patterns
Series Navigation:
- Previous: P is for Pods: The Basic Building Blocks of Kubernetes
- Next: S is for Services: Networking and Service Discovery
Complete Series: Kubernetes A-to-Z Series Overview