Deployments
A Deployment manages ReplicaSets and Pods, providing declarative updates and easy scaling.
The Deployment is the workhorse of Kubernetes and the object you will use most for stateless apps. It manages a ReplicaSet, which in turn manages Pods.
What it adds
- Rolling updates — change the image and Pods are replaced gradually with zero downtime.
- Rollbacks — revert to a previous version with one command.
- Scaling — change
replicasto add or remove Pods. - Self-healing — inherited from the ReplicaSet it manages.
Example
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.27
ports:
- containerPort: 80When to use it
- A team uses a Deployment to roll out a new Docker image tag to all replicas, with automatic rollback if health checks fail.
- An SRE uses kubectl rollout undo to revert a bad Deployment to the previous working revision in under 30 seconds.
- A developer uses Deployment revision history to audit which image versions have been deployed to production over time.
More examples
Deployment manifest
Defines a Deployment that manages a ReplicaSet of 3 pods, enabling declarative updates and rollback.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: myapp:1.0Update image and check rollout
Updates the container image, waits for the rollout to finish, and views the revision history for auditing.
kubectl set image deployment/web web=myapp:2.0
kubectl rollout status deployment/web
kubectl rollout history deployment/webRollback to previous revision
Reverts the Deployment to a previous revision, which Kubernetes accomplishes by scaling back the old ReplicaSet.
# Rollback to the immediately previous revision
kubectl rollout undo deployment/web
# Or to a specific revision
kubectl rollout undo deployment/web --to-revision=2
kubectl rollout status deployment/web
Discussion