Deployments

Deployments provide declarative updates for Pods and ReplicaSets. They’re the recommended way to manage stateless applications in Kubernetes.

Why Use Deployments?

Deployments offer several advantages over managing Pods directly:

  • Declarative updates - Define desired state
  • Rolling updates - Zero-downtime deployments
  • Rollback capability - Revert to previous versions
  • Scaling - Easily adjust replica count
  • Self-healing - Automatic Pod replacement

Creating a Deployment

Here’s a basic Deployment definition:

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

Create the Deployment:

Terminal window
kubectl apply -f deployment.yaml

Viewing Deployments

Check Deployment status:

Terminal window
# List Deployments
kubectl get deployments
# Detailed information
kubectl describe deployment nginx-deployment
# View ReplicaSets created by Deployment
kubectl get replicasets
# View Pods created by Deployment
kubectl get pods -l app=nginx

Scaling Deployments

Scale up or down easily:

Terminal window
# Scale using kubectl
kubectl scale deployment nginx-deployment --replicas=5
# Edit Deployment directly
kubectl edit deployment nginx-deployment

Update the YAML:

spec:
replicas: 5

Rolling Updates

Update container image:

Terminal window
# Update image
kubectl set image deployment/nginx-deployment nginx=nginx:1.22
# Check rollout status
kubectl rollout status deployment/nginx-deployment
# View rollout history
kubectl rollout history deployment/nginx-deployment

Update Strategies

RollingUpdate (Default)

Gradually replaces old Pods with new ones:

spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1 # Max new Pods during update
maxUnavailable: 0 # Max unavailable Pods

Recreate

Kills all Pods before creating new ones:

spec:
strategy:
type: Recreate

Rollback

Undo a deployment:

Terminal window
# Rollback to previous version
kubectl rollout undo deployment/nginx-deployment
# Rollback to specific revision
kubectl rollout undo deployment/nginx-deployment --to-revision=2
# Pause a rollout
kubectl rollout pause deployment/nginx-deployment
# Resume a rollout
kubectl rollout resume deployment/nginx-deployment

Advanced Configuration

Resource Limits

spec:
template:
spec:
containers:
- name: nginx
image: nginx:1.21
resources:
requests:
memory: "128Mi"
cpu: "250m"
limits:
memory: "256Mi"
cpu: "500m"

Environment Variables

env:
- name: ENVIRONMENT
value: "production"
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: db-secret
key: url

Health Checks

livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5

Labels and Selectors

Labels organize and select resources:

metadata:
labels:
app: nginx
environment: production
tier: frontend
spec:
selector:
matchLabels:
app: nginx
matchExpressions:
- key: environment
operator: In
values:
- production
- staging

Deployment Status

Monitor Deployment health:

Terminal window
# Check if Deployment is ready
kubectl get deployment nginx-deployment
# Expected output
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 5m
# Detailed conditions
kubectl get deployment nginx-deployment -o yaml | grep -A 5 conditions

Best Practices

  1. Set resource requests and limits - Ensure proper scheduling
  2. Use health checks - Enable automatic recovery
  3. Configure update strategy - Control rollout behavior
  4. Use specific image tags - Avoid latest tag
  5. Set Pod disruption budgets - Maintain availability
  6. Use proper labels - Enable filtering and organization
  7. Test rollouts - Use staging environments

Troubleshooting

Common issues:

Terminal window
# Pods not starting
kubectl describe deployment nginx-deployment
kubectl logs deployment/nginx-deployment
# Update stuck
kubectl rollout status deployment/nginx-deployment
kubectl get events --sort-by=.metadata.creationTimestamp
# Image pull errors
kubectl describe pod <pod-name>

Cleanup

Delete a Deployment:

Terminal window
# Delete Deployment (also deletes Pods)
kubectl delete deployment nginx-deployment
# Delete using YAML file
kubectl delete -f deployment.yaml

Next Steps

Now that you understand Deployments, let’s explore Services - how to expose your applications to network traffic.