Skip to main content
Kubernetes intermediate Lesson 3 of 8

Deployments and Rollouts

Learn how Deployments manage replica sets, rolling updates, rollbacks, and rollout strategies in Kubernetes.

What is a Deployment?

A Deployment provides declarative updates for stateless applications. You describe the desired state and Kubernetes drives the cluster to match it — handling replica counts, rolling updates, and rollbacks automatically.

Deployment
    └── ReplicaSet (v1)
            ├── Pod 1
            ├── Pod 2
            └── Pod 3

When you update the image, Kubernetes creates a new ReplicaSet and gradually shifts Pods from old to new.

Creating a Deployment

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
  labels:
    app: api
spec:
  replicas: 3
  selector:
    matchLabels:
      app: api                 # must match template labels
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
        - name: api
          image: nginx:1.25
          ports:
            - containerPort: 80
          resources:
            requests:
              cpu: "100m"
              memory: "64Mi"
            limits:
              cpu: "250m"
              memory: "128Mi"
          readinessProbe:
            httpGet:
              path: /
              port: 80
            initialDelaySeconds: 5
            periodSeconds: 5
# Apply the deployment
kubectl apply -f deployment.yaml

# Watch pods come up
kubectl get pods --watch

# See the deployment status
kubectl get deployments
kubectl rollout status deployment/api-deployment

# Inspect the ReplicaSet it created
kubectl get replicasets

Scaling

# Scale to 5 replicas imperatively
kubectl scale deployment api-deployment --replicas=5

# Or edit the YAML and re-apply
# (change replicas: 3  →  replicas: 5)
kubectl apply -f deployment.yaml

# Watch pods scale up
kubectl get pods -w

Rolling Updates

A rolling update replaces Pods gradually so your app stays available.

spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1        # allow 1 extra pod above desired count
      maxUnavailable: 0  # never have fewer than desired count
# Update the container image (triggers a rolling update)
kubectl set image deployment/api-deployment api=nginx:1.26

# Or edit the YAML, change image tag, then:
kubectl apply -f deployment.yaml

# Watch the rollout in real time
kubectl rollout status deployment/api-deployment

# See detailed rollout events
kubectl describe deployment api-deployment

Rollback

# Check rollout history
kubectl rollout history deployment/api-deployment

# See details of a specific revision
kubectl rollout history deployment/api-deployment --revision=2

# Roll back to the previous revision
kubectl rollout undo deployment/api-deployment

# Roll back to a specific revision
kubectl rollout undo deployment/api-deployment --to-revision=1

# Confirm the rollback took effect
kubectl rollout status deployment/api-deployment
kubectl get pods

Pause and Resume a Rollout

Useful when you need to make multiple changes without triggering a rollout on each one.

# Pause the rollout
kubectl rollout pause deployment/api-deployment

# Make several changes
kubectl set image deployment/api-deployment api=nginx:1.27
kubectl set resources deployment/api-deployment -c api --limits=cpu=500m,memory=256Mi

# Resume — triggers a single rollout with all changes
kubectl rollout resume deployment/api-deployment

Recreate Strategy

Suitable for apps that cannot run two versions simultaneously (e.g., schema migrations).

spec:
  strategy:
    type: Recreate   # terminate ALL old pods first, then create new ones

Deployment vs ReplicaSet vs Pod

ObjectUse directly?Purpose
PodRarelySingle instance, no self-healing
ReplicaSetRarelyMaintains N replicas — managed by Deployment
DeploymentYes, alwaysManages rollouts + rollbacks via ReplicaSets
# Never create ReplicaSets directly — use Deployments
# kubectl get all shows the full hierarchy
kubectl get all -l app=api

Useful Day-to-Day Commands

# Edit a live deployment in your editor
kubectl edit deployment api-deployment

# Restart all pods (triggers a rolling restart)
kubectl rollout restart deployment/api-deployment

# Delete a deployment (and all its pods)
kubectl delete deployment api-deployment

# Delete from file
kubectl delete -f deployment.yaml

Learning Outcomes

After this tutorial you can:

  • Write a Deployment manifest with replicas, selector, and strategy
  • Perform rolling updates and rollbacks with kubectl rollout
  • Scale deployments imperatively and declaratively
  • Explain Deployment → ReplicaSet → Pod hierarchy

Frequently Asked Questions

How is a Deployment different from a Pod?
A Pod is a single running workload unit. A Deployment manages Pods indirectly by creating and updating ReplicaSets to match the desired replica count.
What happens to traffic during a rolling update?
New pods start and pass their readiness probes before old pods are terminated. At no point does replica count drop to zero, so traffic continues uninterrupted.