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/v1kind: Deploymentmetadata: name: nginx-deployment labels: app: nginxspec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.21 ports: - containerPort: 80Create the Deployment:
kubectl apply -f deployment.yamlViewing Deployments
Check Deployment status:
# List Deploymentskubectl get deployments
# Detailed informationkubectl describe deployment nginx-deployment
# View ReplicaSets created by Deploymentkubectl get replicasets
# View Pods created by Deploymentkubectl get pods -l app=nginxScaling Deployments
Scale up or down easily:
# Scale using kubectlkubectl scale deployment nginx-deployment --replicas=5
# Edit Deployment directlykubectl edit deployment nginx-deploymentUpdate the YAML:
spec: replicas: 5Rolling Updates
Update container image:
# Update imagekubectl set image deployment/nginx-deployment nginx=nginx:1.22
# Check rollout statuskubectl rollout status deployment/nginx-deployment
# View rollout historykubectl rollout history deployment/nginx-deploymentUpdate 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 PodsRecreate
Kills all Pods before creating new ones:
spec: strategy: type: RecreateRollback
Undo a deployment:
# Rollback to previous versionkubectl rollout undo deployment/nginx-deployment
# Rollback to specific revisionkubectl rollout undo deployment/nginx-deployment --to-revision=2
# Pause a rolloutkubectl rollout pause deployment/nginx-deployment
# Resume a rolloutkubectl rollout resume deployment/nginx-deploymentAdvanced 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: urlHealth Checks
livenessProbe: httpGet: path: /healthz port: 8080 initialDelaySeconds: 30 periodSeconds: 10
readinessProbe: httpGet: path: /ready port: 8080 initialDelaySeconds: 5 periodSeconds: 5Labels and Selectors
Labels organize and select resources:
metadata: labels: app: nginx environment: production tier: frontendspec: selector: matchLabels: app: nginx matchExpressions: - key: environment operator: In values: - production - stagingDeployment Status
Monitor Deployment health:
# Check if Deployment is readykubectl get deployment nginx-deployment
# Expected outputNAME READY UP-TO-DATE AVAILABLE AGEnginx-deployment 3/3 3 3 5m
# Detailed conditionskubectl get deployment nginx-deployment -o yaml | grep -A 5 conditionsBest Practices
- Set resource requests and limits - Ensure proper scheduling
- Use health checks - Enable automatic recovery
- Configure update strategy - Control rollout behavior
- Use specific image tags - Avoid
latesttag - Set Pod disruption budgets - Maintain availability
- Use proper labels - Enable filtering and organization
- Test rollouts - Use staging environments
Troubleshooting
Common issues:
# Pods not startingkubectl describe deployment nginx-deploymentkubectl logs deployment/nginx-deployment
# Update stuckkubectl rollout status deployment/nginx-deploymentkubectl get events --sort-by=.metadata.creationTimestamp
# Image pull errorskubectl describe pod <pod-name>Cleanup
Delete a Deployment:
# Delete Deployment (also deletes Pods)kubectl delete deployment nginx-deployment
# Delete using YAML filekubectl delete -f deployment.yamlNext Steps
Now that you understand Deployments, let’s explore Services - how to expose your applications to network traffic.