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
63
64
65
66
67
68
69
70
71

# Kubernetes Deployment: Rolling Updates, Rollbacks, and Real Examples

A **Kubernetes Deployment** is one of the most essential and widely used resources in the Kubernetes ecosystem. It simplifies the management of your application lifecycle  from deploying and scaling to updating and rolling back.

In this post, youll learn:

* What a Deployment is
* How it differs from ReplicaSet
* How to define and apply a Deployment using YAML
* How Kubernetes performs rolling updates and supports rollbacks
* Real-world `kubectl` commands and use cases

---

## Table of Contents

{% toc %}

---

## 1. What Is a Kubernetes Deployment?

A Deployment is a **higher-level controller** that manages a ReplicaSet for you.

It lets you:

* Run multiple identical Pods (replicas)
* Update your app version automatically
* Roll back if something goes wrong
* Ensure your app stays available during changes

In short, its your go-to tool for **production-grade app delivery**.

---

## 2. Deployment vs ReplicaSet

| Feature        | ReplicaSet            | Deployment                             |
| -------------- | --------------------- | -------------------------------------- |
| Manages Pods   | Yes                   | Yes (via ReplicaSet)                   |
| Rolling Update | No                    | Yes                                    |
| Rollback       | No                    | Yes (previous ReplicaSet is preserved) |
| Use Case       | Manual pod management | Declarative, versioned app management  |

---

## 3. Deployment YAML Example

Heres a minimal Deployment definition:

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

Key fields explained:

  • replicas: Number of desired Pods
  • selector: How the Deployment finds Pods to manage
  • template: The actual Pod configuration

4. Creating and Managing a Deployment

Create:

1
kubectl apply -f nginx-deployment.yaml

View status:

1
2
3
kubectl get deployments
kubectl get rs
kubectl get pods

5. Performing a Rolling Update

To update the container image:

1
kubectl set image deployment/nginx-deployment nginx=nginx:1.22

Kubernetes will automatically perform a rolling update — gradually replacing Pods one by one, without downtime.

You can monitor progress with:

1
kubectl rollout status deployment/nginx-deployment

6. Rollback to Previous Version

If something breaks, revert easily:

1
kubectl rollout undo deployment/nginx-deployment

View deployment history:

1
kubectl rollout history deployment/nginx-deployment

7. Rolling Update Strategy

Deployment uses this update strategy by default:

1
2
3
4
5
strategy:
  type: RollingUpdate
  rollingUpdate:
    maxSurge: 25%
    maxUnavailable: 25%

You can customize these values to balance availability and speed during updates.


8. FAQ (Answer Engine Optimization)

Q1. Does a Deployment delete old ReplicaSets after an update? No. Kubernetes keeps previous ReplicaSets to enable rollbacks.

Q2. Can I scale a Deployment manually? Yes. Use kubectl scale deployment/nginx-deployment --replicas=5.

Q3. Are Pods in a Deployment automatically restarted on failure? Yes, as long as the Deployment is healthy and the ReplicaSet remains.


9. Key Takeaways

TopicSummary
DeploymentManages app lifecycle using ReplicaSet
Rolling UpdateGradual upgrade with zero downtime
RollbackEasily revert to previous working versions
ScalingAdjust replicas easily with declarative configuration

10. Final Thoughts

A Kubernetes Deployment is the backbone of reliable application delivery in any modern cloud-native system.

By mastering Deployments, you can automate updates, ensure high availability, and maintain full control over your application versions — all using YAML and a few kubectl commands.