Skip to main content

Deployment

A Deployment is the standard Kubernetes controller for running stateless applications. It keeps the desired number of identical pod replicas running, replaces failed pods automatically, and rolls out new versions without downtime.

How a Deployment works

You declare a pod template and a replica count. The Deployment creates a ReplicaSet, which creates and owns the pods. When you change the template, such as a new image tag, the Deployment creates a new ReplicaSet and shifts replicas from the old one to the new one. The old ReplicaSet is kept at zero replicas so you can roll back.

flowchart TD D["Deployment
payment-api"] -->|"manages"| R2["ReplicaSet v3.2.0
3 replicas"] D -.->|"kept for rollback"| R1["ReplicaSet v3.1.0
0 replicas"] R2 --> P1["Pod 1"] R2 --> P2["Pod 2"] R2 --> P3["Pod 3"] classDef ctl fill:#326CE5,color:#ffffff classDef rs fill:#FFEDD5,color:#1E293B classDef pod fill:#D1FAE5,color:#1E293B class D ctl class R1,R2 rs class P1,P2,P3 pod

A production-ready spec

YAML
apiVersion: apps/v1
kind: Deployment
metadata:
name: payment-api
namespace: production
spec:
replicas: 3
selector:
matchLabels:
app: payment-api
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0 # never drop below desired capacity
template:
metadata:
labels:
app: payment-api
spec:
containers:
- name: payment-api
image: registry.example.com/payment-api:v3.1.0 # pinned tag
resources:
requests: { cpu: "250m", memory: "256Mi" }
limits: { memory: "512Mi" }
readinessProbe:
httpGet: { path: /health, port: 8080 }
periodSeconds: 5
livenessProbe:
httpGet: { path: /health, port: 8080 }
initialDelaySeconds: 30
terminationGracePeriodSeconds: 30

Daily commands

Bash
kubectl set image deployment/payment-api payment-api=registry.example.com/payment-api:v3.2.0 -n production
kubectl rollout status deployment/payment-api -n production
kubectl rollout history deployment/payment-api -n production
kubectl rollout undo deployment/payment-api -n production
kubectl rollout restart deployment/payment-api -n production
kubectl annotate deployment payment-api kubernetes.io/change-cause="v3.2.0 UPI retry" -n production

Deployment vs StatefulSet vs DaemonSet

Controller Use for
Deployment Stateless apps with interchangeable pods
StatefulSet Stable identity and per-pod storage
DaemonSet One pod per node
Remember

kubectl rollout undo is your fastest recovery path after a bad release, usually completing as quickly as a normal rollout.

Security

Never deploy with the latest tag. Rollbacks and audits become unreliable. Use a version or commit SHA.

Common Mistake

Omitting a readiness probe. Pods then receive traffic the moment the container starts, before the app is ready.

Frequently Asked Questions

Why does a Deployment manage a ReplicaSet instead of pods directly?

The extra layer makes versioned rollouts possible. Each template change creates a new ReplicaSet, and the old one stays at zero replicas so rollback just scales it back up.

What commonly causes outages during Deployment updates?

Missing readiness probes, and surge or unavailability settings that are too aggressive for small replica counts. Use maxUnavailable: 0 for production services that cannot lose capacity.

Does kubectl apply or kubectl set image trigger a rollout?

Either triggers one if the pod template changes. Changing only the replica count does not create a new ReplicaSet.

Found a mistake, an outdated step or a broken link?