Managing Application Updates with Kubernetes Deployments
/ 3 min read
Understanding Kubernetes Deployments
A Deployment provides declarative updates for Pods and ReplicaSets, making it the preferred way to manage stateless applications in Kubernetes.
Basic Deployment Configuration
Simple Deployment
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.14.2 ports: - containerPort: 80Deployment Strategies
1. Rolling Update (Default)
spec: strategy: type: RollingUpdate rollingUpdate: maxSurge: 25% maxUnavailable: 25%2. Recreate Strategy
spec: strategy: type: RecreateScaling Deployments
Manual Scaling
spec: replicas: 5Horizontal Pod Autoscaling
apiVersion: autoscaling/v2kind: HorizontalPodAutoscalermetadata: name: nginx-hpaspec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: nginx-deployment minReplicas: 1 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 50Health Checks
Liveness Probe
spec: template: spec: containers: - name: nginx livenessProbe: httpGet: path: /healthz port: 8080 initialDelaySeconds: 3 periodSeconds: 3Readiness Probe
spec: template: spec: containers: - name: nginx readinessProbe: httpGet: path: /ready port: 8080 initialDelaySeconds: 5 periodSeconds: 5Resource Management
Resource Requests and Limits
spec: template: spec: containers: - name: nginx resources: requests: memory: "64Mi" cpu: "250m" limits: memory: "128Mi" cpu: "500m"Update Strategies
Progressive Updates
spec: progressDeadlineSeconds: 600 minReadySeconds: 5 revisionHistoryLimit: 10Rollback Configuration
spec: revisionHistoryLimit: 10Labels and Selectors
Using Multiple Labels
metadata: labels: app: nginx environment: production tier: frontendspec: selector: matchLabels: app: nginx tier: frontendPod Disruption Budget
apiVersion: policy/v1kind: PodDisruptionBudgetmetadata: name: nginx-pdbspec: minAvailable: 2 selector: matchLabels: app: nginxBest Practices
1. Resource Management
- Always set resource requests and limits
- Use horizontal pod autoscaling
- Implement pod disruption budgets
2. Health Checks
- Configure both liveness and readiness probes
- Set appropriate timing parameters
- Use appropriate probe types
3. Update Strategy
- Use rolling updates when possible
- Set appropriate surge and unavailable parameters
- Maintain revision history
4. Labels and Annotations
- Use meaningful labels
- Implement proper label hierarchy
- Document with annotations
Troubleshooting
Common issues and solutions:
-
Failed Rollouts
- Check events
- Review pod logs
- Verify image availability
- Check resource constraints
-
Scaling Issues
- Verify HPA configuration
- Check metrics availability
- Review resource usage
- Check cluster capacity
-
Health Check Failures
- Verify probe endpoints
- Check timing parameters
- Review container logs
- Test endpoints manually
Deployment Commands
Common kubectl Commands
# Create deploymentkubectl create deployment nginx --image=nginx
# Scale deploymentkubectl scale deployment nginx --replicas=5
# Update imagekubectl set image deployment/nginx nginx=nginx:1.16.1
# Roll back deploymentkubectl rollout undo deployment/nginx
# Check rollout statuskubectl rollout status deployment/nginx
# View rollout historykubectl rollout history deployment/nginxAdvanced Configurations
Init Containers
spec: template: spec: initContainers: - name: init-myservice image: busybox:1.28 command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']Volume Mounts
spec: template: spec: containers: - name: nginx volumeMounts: - name: config-volume mountPath: /etc/nginx/conf.d volumes: - name: config-volume configMap: name: nginx-configSeries Navigation
- Previous: Kubernetes Security
- Next: Kubernetes StatefulSets
Conclusion
Kubernetes Deployments are a powerful way to manage application lifecycles. Understanding their features and best practices is crucial for successful application deployment and management.