1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

# Kubernetes Deployment: Automating Pod Rollout and Updates

Kubernetes **Deployment** is the go-to resource for automating the **lifecycle of Pods** — handling **initial deployment, scaling, updating, and rollback**.

If you're building production-ready Kubernetes applications, you must understand how Deployments work.

---

## Table of Contents

{% toc %}

---

## 1. What Is a Deployment?

A **Deployment** is a higher-level abstraction over ReplicaSets. It declaratively manages:

* How many Pod replicas to run
* What container image to use
* How to **update** and **rollback** versions automatically

Think of it as the **control tower** for your Pod-based application.

---

## 2. Deployment vs ReplicaSet

| Feature         | ReplicaSet    | Deployment                       |
| --------------- | ------------- | -------------------------------- |
| Pod Management  | Manual        | Declarative & automated          |
| Rolling Updates | Not supported | Supported (default behavior)     |
| Rollback        | Not available | Yes, with `kubectl rollout undo` |
| Usage Priority  | Low           | High (recommended for most use)  |

While ReplicaSets ensure a number of Pods run, Deployments **control what those Pods should look like and how to change them over time.**

---

## 3. Basic Deployment YAML

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: nginx
          image: nginx:1.21
          ports:
            - containerPort: 80

Key Fields:

  • replicas: Desired number of Pods
  • selector: Links the Deployment to Pods
  • template: Pod specification

4. Creating and Viewing a Deployment

1
2
3
kubectl apply -f deployment.yaml
kubectl get deployments
kubectl get pods

Once applied, the Deployment creates a ReplicaSet, which then launches the Pods.


5. Updating a Deployment (Rolling Updates)

You can update a container image like this:

1
kubectl set image deployment/my-deployment nginx=nginx:1.23

This triggers a rolling update, where Pods are updated one at a time, ensuring zero downtime.

Monitor update progress

1
kubectl rollout status deployment/my-deployment

6. Rolling Back to Previous Version

If something goes wrong:

1
kubectl rollout undo deployment/my-deployment

You can safely revert to the last known good version.


7. Scaling Your Deployment

To increase the number of Pods:

1
kubectl scale deployment/my-deployment --replicas=5

Deployments support horizontal scaling just like ReplicaSets — but with added features.


8. FAQ (Answer Engine Optimization)

Q1. Does a Deployment create a ReplicaSet? Yes. Each Deployment creates and manages one or more ReplicaSets.

Q2. Is downtime expected during updates? No. Deployments use rolling updates to avoid downtime.

Q3. Can I rollback to any previous revision? By default, only the last revision is stored. Use revisionHistoryLimit to control how many versions are kept.


9. Advanced Tips for Production

  • Use strategy.rollingUpdate.maxUnavailable to control update batch sizes
  • Set minReadySeconds to ensure Pods are stable before the next rollout
  • Use kubectl rollout history to list previous versions

10. Summary

TopicSummary
What is DeploymentManages ReplicaSets and Pod lifecycle
Rolling UpdateSmooth, zero-downtime version transition
RollbackRevert easily to previous working configuration
ScalingAdjust Pod count on the fly