Deployments & Strategies

Welcome to the Deployments & Strategies chapter. This section covers fundamental concepts and best practices necessary to master this topic in depth.

1. The Logic Hierarchy

You rarely create Pods directly. You create Deployments.

  1. Deployment: Manages Releases (Updates, Rollbacks).
  2. ReplicaSet: Manages Scale (Ensures N copies are running).
  3. Pod: The actual application.

Deployment → manages → ReplicaSet → manages → Pod


2. Update Strategies

1. RollingUpdate (Default)

Updates Pods incrementally. Zero Downtime.

  • MaxUnavailable: How many pods can be down during update (e.g., 25%).
  • MaxSurge: How many extra pods can be created (e.g., 25%).

2. Recreate

Kills ALL old pods, then starts ALL new pods.

  • Downtime: Yes.
  • Use Case: Database schema changes where versions cannot coexist.

3. Interactive: Rolling Update Simulator

Watch how v1 is replaced by v2 without downtime.

Cluster State


4. Rollbacks

Mistake in v2? Undo it instantly. kubectl rollout undo deployment/my-app

Kubernetes keeps a history of ReplicaSets. It basically scales v1 RS up and v2 RS down.


5. Code Example: Deployment YAML

Diff

 apiVersion: apps/v1
 kind: Deployment
 metadata:
   name: backend
 spec:
   replicas: 3
   template:
   spec:
     containers:
-      - image: my-app:v1
+      - image: my-app:v2

Full YAML

apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend
spec:
  replicas: 3
  selector:
    matchLabels:
      app: backend
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: backend
    spec:
      containers:
      - name: server
        image: nginx:1.14.2
        ports:
        - containerPort: 80