Deployments โ Managing Replica Sets
A Deployment provides declarative updates for Pods and ReplicaSets. You describe the desired state, and the Deployment controller changes the actual state to match.
Deployments are the most common way to run stateless applications in Kubernetes. They give you rolling updates, rollbacks, and self-healing out of the box.
How Deployments Work
Deployment (you define this)
โ
โผ
ReplicaSet (created automatically)
โ
โผ
Pods (the actual running instances)
โโโโโโโโ โโโโโโโโ โโโโโโโโ
โ Pod1 โ โ Pod2 โ โ Pod3 โ
โโโโโโโโ โโโโโโโโ โโโโโโโโ
You tell the Deployment: "I want 3 replicas of my app"
The Deployment creates a ReplicaSet
The ReplicaSet ensures exactly 3 Pods are running
If a Pod crashes, the ReplicaSet creates a new one
Creating a Deployment
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.25
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
replicas โ How many identical pods to maintain.
selector โ Which pods this Deployment manages. Must match the pod template labels.
template โ The pod specification that will be used to create new pods.
Managing Deployments
# Apply a deployment
kubectl apply -f deployment.yaml
# Check status
kubectl get deployments
kubectl rollout status deployment/nginx-deployment
# Update the image (triggers a rolling update)
kubectl set image deployment/nginx-deployment nginx=nginx:1.26
# View rollout history
kubectl rollout history deployment/nginx-deployment
# Rollback to the previous version
kubectl rollout undo deployment/nginx-deployment
# Scale up/down
kubectl scale deployment nginx-deployment --replicas=5
Deployment Strategies
Rolling Update (default) โ Gradually replaces old pods with new ones. Zero downtime.
Recreate โ Kills all old pods before creating new ones. Simple but causes downtime.
# Rolling Update strategy (default)
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1 # Max pods above desired count
maxUnavailable: 0 # Max pods that can be unavailable