Kubernetes Workloads
Deployments — Rolling Updates and Rollbacks
Updating applications, rollouts, strategies, and history checks.
Interview: Deployments are one of the most heavily tested workloads. Understand the difference between the RollingUpdate and Recreate strategies, how maxSurge and maxUnavailable control container rollouts, and how to roll back changes using undo commands.
Introduction to Deployments
A Deployment is a higher-level abstraction that sits on top of ReplicaSets. It provides declarative updates for Pods and ReplicaSets. You describe a desired state in a Deployment manifest, and the Deployment controller manages the transition from the current state to the desired state.
Deployment Strategies
When updating container versions (e.g. changing the image tag), Deployments use two main strategies:
- Recreate: Terminates all running Pods first, before spinning up the new version. This results in service downtime, but guarantees that two different versions of the code never run simultaneously.
- RollingUpdate (Default): Gradually replaces old Pods with new ones, ensuring zero downtime. You configure rolling updates using two parameters:
maxSurge: The maximum number of Pods that can be created above the desired replica count during the update (e.g. 25%).maxUnavailable: The maximum number of Pods that can be unavailable during the update process (e.g. 25%).
Under the Hood: Double ReplicaSet Swapping
When you update a Deployment's container configuration, the Deployment controller automatically creates a new ReplicaSet representing the new version. It then scales up the new ReplicaSet (adding new pods) while scaling down the old ReplicaSet (removing old pods).
Rollback and History Management CLI
If an update introduces a bug or causes container crashes, you can inspect rollout history and roll back changes instantly:
# View the active status of a rollout update
kubectl rollout status deployment/web-deployment
View the deployment's rollout revision history
kubectl rollout history deployment/web-deployment
Roll back the deployment to the immediately preceding version
kubectl rollout undo deployment/web-deployment
Roll back to a specific revision from the history log
kubectl rollout undo deployment/web-deployment --to-revision=2
Deployment Manifest Example
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 4
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app: api-server
template:
metadata:
labels:
app: api-server
spec:
containers:
- name: main-api
image: my-registry/api:v2.0.0
ports:
- containerPort: 8080