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.

A Deployment manages a ReplicaSet, which manages three PodsDeploymentReplicaSetPodPodPod
Deployment → ReplicaSet → Pods. You edit the Deployment; it handles the rest.

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 replicas to add or remove Pods.
  • Self-healing — inherited from the ReplicaSet it manages.

Example

Example · yaml
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: 80

When 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.

Example · yaml
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.0

Update image and check rollout

Updates the container image, waits for the rollout to finish, and views the revision history for auditing.

Example · bash
kubectl set image deployment/web web=myapp:2.0
kubectl rollout status deployment/web
kubectl rollout history deployment/web

Rollback to previous revision

Reverts the Deployment to a previous revision, which Kubernetes accomplishes by scaling back the old ReplicaSet.

Example · bash
# 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

  • Be the first to comment on this lesson.
Deployments — Kubernetes | SoundsCode