๐ Kubernetes Deployments
A Deployment is the most commonly used controller in Kubernetes. It manages ReplicaSets, allowing for:
- Declarative updates
- Rollbacks
- Scaling
- Easy rollout of changes
โ Why Use a Deployment?
Unlike a raw ReplicaSet, a Deployment offers high-level operations:
- Rollout new versions with zero downtime
- Automatically create new ReplicaSets
- Roll back to previous versions
๐ฆ Create a Deployment
Imperative Command
kubectl create deployment nginx --image=nginx
With Replicas
kubectl create deployment nginx --image=nginx --replicas=4
If
--replicasis not accepted, scale it after:
kubectl scale deployment nginx --replicas=4
โ๏ธ Generate YAML Without Creating (Dry Run)
kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > nginx-deployment.yaml
You can then customize and apply it:
kubectl apply -f nginx-deployment.yaml
๐งพ Sample Deployment YAML
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:1.19
ports:
- containerPort: 80
๐ Inspect & Manage Deployments
kubectl get deployments
kubectl describe deployment <name>
kubectl edit deployment <name>
kubectl delete deployment <name>
๐ Rollouts & Rollbacks
View rollout status
kubectl rollout status deployment <name>
Roll back to previous version
kubectl rollout undo deployment <name>
โ Common Deployment Issues
Pods stuck in ImagePullBackOff
kubectl describe pod <pod-name>
# Check the Events section
Example Error
spec:
containers:
- name: app
image: nonexistent-image # leads to ImagePullBackOff
๐ง Key Differences from ReplicaSet
| Feature | Deployment | ReplicaSet |
|---|---|---|
| Manages RS? | โ Yes | โ No |
| Rollbacks | โ Yes | โ No |
| Rolling updates | โ Yes | โ Manual only |
| Recommended? | โ Production default | ๐ซ Not preferred |
โ Summary
- Deployment is your go-to controller for managing stateless apps
- Use
kubectl applywith YAML for full control - Supports rollback, rollout, and declarative scaling
- Use
kubectl rollout statusto monitor updates - Use Deployments unless you have a stateful or special-case workload