Kubernetes Deployment

A Deployment manages stateless application rollouts in Kubernetes, providing declarative updates, rolling upgrades, and rollback through ReplicaSets.

A Deployment is the standard way to run and manage stateless applications in Kubernetes. It wraps a ReplicaSet and adds a crucial layer on top: the ability to declaratively update Pods using a rolling strategy, pause and resume rollouts, and roll back to a previous revision if something goes wrong. For the vast majority of workloads, a Deployment is the correct top-level resource to use.

What a Deployment Manages

Deployment
  └── ReplicaSet (current revision, e.g., nginx-7d4b5f6c9)  ← new Pods
  └── ReplicaSet (previous revision, e.g., nginx-5f9c3d7b2)  ← scaled down, kept for rollback

When you update the Pod template (e.g., a new image version), the Deployment controller creates a new ReplicaSet and incrementally scales it up while scaling down the old one. The old ReplicaSet is retained (scaled to zero) for rollback purposes, up to revisionHistoryLimit revisions (default: 10).

Deployment Manifest

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: default
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1        # at most 1 extra Pod above desired during update
      maxUnavailable: 0  # no Pods unavailable during update (zero-downtime)
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.27
          ports:
            - containerPort: 80
          resources:
            requests:
              cpu: "100m"
              memory: "64Mi"
            limits:
              cpu: "200m"
              memory: "128Mi"
          readinessProbe:
            httpGet:
              path: /
              port: 80
            initialDelaySeconds: 5
            periodSeconds: 5

Update Strategies

Strategy Behavior Use Case
RollingUpdate (default) Gradually replaces old Pods with new ones; controlled by maxSurge and maxUnavailable Zero-downtime updates for stateless apps
Recreate Terminates all existing Pods before creating new ones; causes downtime Apps that cannot run two versions simultaneously

Essential kubectl Commands

# Apply the Deployment
kubectl apply -f nginx-deployment.yaml

# Check rollout status
kubectl rollout status deployment/nginx-deployment
# Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas updated...
# deployment "nginx-deployment" successfully rolled out

# Update the image (triggers a rolling update)
kubectl set image deployment/nginx-deployment nginx=nginx:1.27.1

# View rollout history
kubectl rollout history deployment/nginx-deployment
# REVISION  CHANGE-CAUSE
# 1         <none>
# 2         <none>

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

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

# Scale the Deployment
kubectl scale deployment/nginx-deployment --replicas=5

# Pause a rollout (to canary-test)
kubectl rollout pause deployment/nginx-deployment

# Resume a paused rollout
kubectl rollout resume deployment/nginx-deployment

# Check the underlying ReplicaSets
kubectl get replicasets -l app=nginx
# NAME                          DESIRED   CURRENT   READY   AGE
# nginx-deployment-7d4b5f6c9    3         3         3       5m
# nginx-deployment-5f9c3d7b2    0         0         0       1h

Annotating the Change Cause

kubectl annotate deployment/nginx-deployment kubernetes.io/change-cause="Upgraded to nginx 1.27.1"

This populates the CHANGE-CAUSE column in kubectl rollout history, making it much easier to reason about rollout history.

Common Mistakes

  • Setting maxUnavailable: 1 without considering traffic. During a rolling update, up to 1 Pod may be absent, reducing capacity. For low-traffic apps this is fine, but high-traffic services should prefer maxUnavailable: 0 and maxSurge: 1.
  • Not setting a readinessProbe. Without it, Kubernetes marks new Pods as ready immediately. A Deployment with a broken image will roll out fully before traffic errors are detected.
  • Using kubectl edit directly on a Deployment in production. This creates a manual change outside of version control. Always apply changes through Git and kubectl apply.
  • Ignoring the old ReplicaSets. They are kept for rollback. If you delete them manually, you lose rollback capability for those revisions.
  • Forgetting spec.selector is immutable. Attempting to change the label selector after creation will be rejected. You must delete and recreate the Deployment.

Best Practices

  • Always pin the container image to a specific version tag or digest, not latest.
  • Set maxUnavailable: 0 and maxSurge: 1 for zero-downtime rolling updates on production services.
  • Define a readinessProbe so the rollout waits for new Pods to be healthy before proceeding.
  • Annotate deployments with kubernetes.io/change-cause for meaningful rollout history.
  • Keep revisionHistoryLimit at a reasonable value (3-5) to avoid accumulating stale ReplicaSets.

Interview Questions

1. What is a Kubernetes Deployment and how does it differ from a ReplicaSet?

A Deployment manages stateless application lifecycle on top of a ReplicaSet. It adds rolling updates, rollbacks, and revision history. A ReplicaSet alone only ensures a fixed number of replicas run; it cannot perform rolling updates. You should always use Deployment as the user-facing resource for stateless apps.

2. How does a Deployment perform a rolling update?

When you update the Pod template (e.g., a new image), the Deployment controller creates a new ReplicaSet with the updated spec. It then incrementally scales the new ReplicaSet up and scales the old one down, controlled by maxSurge (how many extra Pods are allowed) and maxUnavailable (how many can be unavailable). Kubernetes waits for new Pods to pass their readiness check before proceeding.

3. How do you roll back a Deployment?

Run kubectl rollout undo deployment/<name> to go back to the previous revision, or kubectl rollout undo deployment/<name> --to-revision=N for a specific one. The Deployment scales up the retained old ReplicaSet and scales down the current one. Rollback history is limited by spec.revisionHistoryLimit.

4. What are maxSurge and maxUnavailable?

maxSurge defines how many Pods above the desired count can exist during a rolling update (can be an absolute number or a percentage). maxUnavailable defines how many Pods below the desired count are acceptable during the update. Setting maxUnavailable: 0 ensures zero downtime — no old Pod is removed until a new Pod is ready.

5. What happens when you run kubectl apply on an existing Deployment with a changed image?

kubectl sends the updated manifest to the kube-apiserver. The Deployment controller detects that spec.template has changed and starts a rolling update: it creates a new ReplicaSet with the new image, scales it up pod-by-pod, and scales down the old ReplicaSet, respecting maxSurge and maxUnavailable constraints throughout.

6. What is the Recreate strategy and when should you use it?

The Recreate strategy terminates all existing Pods before creating new ones, causing a brief period of downtime. Use it only when your application cannot tolerate running two versions simultaneously — for example, when a database schema migration is incompatible with the old application version. For most stateless services, RollingUpdate is preferred.

7. How would you perform a zero-downtime deployment?

Configure the Deployment with strategy.rollingUpdate.maxUnavailable: 0 and maxSurge: 1. Add a readinessProbe to the container so Kubernetes only marks a new Pod ready after the application has started accepting traffic. With this configuration, a new Pod must pass its readiness check before any old Pod is removed, ensuring continuous availability throughout the update.

Summary

  • A Deployment is the standard resource for running stateless applications in Kubernetes; it manages one or more ReplicaSets to enable rolling updates and rollbacks.
  • Rolling updates are controlled by maxSurge and maxUnavailable; setting maxUnavailable: 0 achieves zero-downtime deployments.
  • Old ReplicaSets are retained for rollback, up to spec.revisionHistoryLimit revisions.
  • A readinessProbe is essential for safe rolling updates — without it, broken Pods are promoted to production immediately.
  • Use kubectl rollout status, history, and undo to observe and control the rollout lifecycle.

Continue Learning

Previous: ReplicaSet | Next: Service

Was this guide helpful?

CodeWithVenu is free to read, with no paywall. If this guide saved you time or helped you prepare, a small voluntary contribution helps me write more practical tutorials, architecture guides, and interview-prep content.