Deployments & ReplicaSets

💤0
Lv 10 XP
← 📦 Containers & Kubernetes · Kubernetes

Deployments & ReplicaSets

Intermediate ⭐ 80 XP ⏱ 18 min #kubernetes#deployments#scaling

Run scalable, self-healing, rolling-updated workloads with Deployments.

📖Theory

A Deployment manages a stateless app declaratively. It creates a ReplicaSet, which keeps the desired number of identical Pods running — recreating any that die. You change the Deployment (image, replicas) and Kubernetes reconciles.

Its headline feature is the rolling update: it gradually replaces old Pods with new ones, respecting maxSurge/maxUnavailable, so there’s no downtime. If a new version misbehaves, kubectl rollout undo rolls back to the previous ReplicaSet.

🌍Real-World Example
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.4.2
kubectl apply -f deploy.yaml
kubectl scale deploy/web --replicas=5
kubectl set image deploy/web web=myapp:1.5.0   # triggers a rolling update
kubectl rollout status deploy/web
kubectl rollout undo deploy/web                # roll back
✍️Hands-On Exercise
  1. Write a Deployment with 3 replicas of an app.
  2. Scale it to 5 and watch the new Pods appear.
  3. Update the image and observe the rolling update, then roll it back.
  4. Explain the relationship between a Deployment and a ReplicaSet.
🧾Cheat Sheet
TaskCommand
Applykubectl apply -f deploy.yaml
Scalekubectl scale deploy/x --replicas=N
Update imagekubectl set image deploy/x c=img
Rollout statuskubectl rollout status deploy/x
Roll backkubectl rollout undo deploy/x
Historykubectl rollout history deploy/x
💬Common Interview Questions
What does a Deployment do?

It declaratively manages a stateless app via a ReplicaSet — maintaining a desired replica count, self-healing dead Pods, and performing rolling updates and rollbacks.

How does a rolling update work?

Kubernetes incrementally replaces old Pods with new ones, bounded by maxSurge and maxUnavailable, so capacity stays up and there’s no downtime; failures can be rolled back to the prior ReplicaSet.

Deployment vs StatefulSet?

Deployment is for stateless, interchangeable Pods; StatefulSet gives stable network identities and persistent per-Pod storage for stateful apps like databases.

📚Official Documentation

📝 My notes on this topic

Auto-saves as you type