Deployment Walkthrough
Deployment Fundamentals
What is a Kubernetes Deployment?
A Deployment provides declarative updates for Pods and ReplicaSets, enabling automatic scaling and self-healing mechanisms.
graph TD
A[Deployment Configuration] --> B[Pod Template]
A --> C[Replica Count]
A --> D[Update Strategy]
B --> E[Container Image]
B --> F[Resource Limits]
Creating a Basic Deployment
Sample Nginx Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Deployment Commands
## Create deployment
kubectl apply -f nginx-deployment.yaml
## List deployments
kubectl get deployments
## Describe deployment
kubectl describe deployment nginx-deployment
Scaling Strategies
Manual Scaling
## Scale deployment to 5 replicas
kubectl scale deployment nginx-deployment --replicas=5
Autoscaling
## Set up horizontal pod autoscaler
kubectl autoscale deployment nginx-deployment \
--min=2 --max=10 --cpu-percent=50
Deployment Update Strategies
Strategy |
Description |
Use Case |
RollingUpdate |
Gradual replacement of pods |
Zero-downtime deployments |
Recreate |
Terminate all pods before creating new ones |
Stateful applications |
Rolling Update Example
## Update deployment image
kubectl set image deployment/nginx-deployment \
nginx=nginx:1.16.1
Monitoring Deployment
Checking Rollout Status
## Watch deployment rollout
kubectl rollout status deployment/nginx-deployment
## View rollout history
kubectl rollout history deployment/nginx-deployment
Rollback Mechanism
## Rollback to previous revision
kubectl rollout undo deployment/nginx-deployment
Advanced Deployment Techniques
Blue-Green Deployment
graph LR
A[Blue Environment] -->|Switch Traffic| B[Green Environment]
B -->|Rollback if Needed| A
Canary Deployment
graph LR
A[Stable Version] --> B[Canary Version]
B -->|Gradual Traffic Shift| A
LabEx Deployment Insights
LabEx provides interactive deployment scenarios that help developers understand complex Kubernetes deployment strategies through hands-on exercises.
Best Practices
- Use declarative configuration
- Implement health checks
- Define resource limits
- Use consistent tagging
- Implement proper monitoring
Conclusion
Kubernetes deployments offer powerful mechanisms for managing containerized applications, providing flexibility, scalability, and reliability in modern cloud-native environments.