Skip to main content
Kubernetes Introduction
CHAPTER 06 Intermediate

Deployments and ReplicaSets

Updated: May 15, 2026
25 min read

# CHAPTER 6

Deployments and ReplicaSets

1. Introduction

In Chapter 4, we learned the golden rule: Never deploy naked Pods. A standalone Pod is mortal—if the physical server it is running on catches fire, the Pod dies and is permanently erased from existence. To achieve High Availability, Self-Healing, and Zero-Downtime deployments, Kubernetes provides higher-level controllers. In this chapter, we will master the Deployment controller and its underlying engine, the ReplicaSet, which together form the backbone of modern cloud applications.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Differentiate between a Pod, a ReplicaSet, and a Deployment.
  • Understand how a ReplicaSet ensures Self-Healing and Scalability.
  • Author a Deployment YAML manifest.
  • Execute a Zero-Downtime Rolling Update to a new application version.
  • Execute a Rollback if a deployment fails.

3. Beginner-Friendly Explanation

Imagine a fast-food restaurant manager.
  • The Pod: A single cashier. If the cashier gets sick and goes home, the register is empty.
  • The ReplicaSet: The Assistant Manager. Their *only* job is to ensure there are always exactly 3 cashiers working. If a cashier gets sick, the Assistant Manager instantly hires a new one to maintain the count of 3.
  • The Deployment: The General Manager. When the restaurant changes uniforms from Red to Blue, the General Manager doesn't fire all 3 cashiers at once (causing downtime). They tell the Assistant Manager: "Hire 1 new cashier in a Blue uniform. Once they are working, fire 1 Red cashier. Repeat until everyone is Blue."

*A Deployment manages a ReplicaSet. A ReplicaSet manages Pods.*

4. What is a ReplicaSet?

A ReplicaSet is a controller whose sole purpose is to maintain a stable set of replica Pods running at any given time. If you define replicas: 3, the ReplicaSet constantly monitors the cluster.
  • If it sees 2 Pods, it creates 1.
  • If it sees 4 Pods (maybe you manually created one by mistake), it ruthlessly assassinates 1 to maintain the exact desired state of 3.

5. What is a Deployment?

While ReplicaSets handle *scaling*, Deployments handle *updates*. You rarely interact directly with a ReplicaSet. You create a Deployment, and the Deployment automatically creates the ReplicaSet for you.

When you update your application's Docker image from v1 to v2, the Deployment orchestrates a Rolling Update:

  1. 1. It creates a brand new, secondary ReplicaSet for v2.
  1. 2. It spins up a v2 Pod.
  1. 3. It gracefully terminates a v1 Pod.
  1. 4. It repeats this process one by one until the v1 ReplicaSet is scaled to 0, and the v2 ReplicaSet is scaled to your desired capacity.
*Result: Your customers experience absolute zero downtime during a version upgrade!*

6. Anatomy of a Deployment YAML

Notice how the spec.template block is actually just a Pod YAML nested inside the Deployment!
yaml
12345678910111213141516171819
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-webapp-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webapp
  template: # This is the blueprint for the Pods the Deployment will create
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: nginx
        image: nginx:1.24
        ports:
        - containerPort: 80

7. Mini Project: Scale and Update an App

Let's witness orchestration in action.

Step-by-Step Tutorial:

  1. 1. Save the YAML from Section 6 into a file named deployment.yaml.
  1. 2. Apply the blueprint:

bash
1
kubectl apply -f deployment.yaml
  1. 3. Check the status. You will see 1 Deployment, 1 ReplicaSet, and 3 Pods!
bash
123
kubectl get deployments
kubectl get rs
kubectl get pods
  1. 4. Test Self-Healing: Manually delete one of your Pods (kubectl delete pod <pod-name>). Immediately run kubectl get pods again. You will see Kubernetes instantly creating a new Pod to replace the murdered one!
  1. 5. Test Scaling: We need to handle a traffic spike! Scale the deployment to 5 replicas:
bash
1
kubectl scale deployment my-webapp-deployment --replicas=5
  1. 6. Test Rolling Update: Let's upgrade our Nginx version from 1.24 to 1.25:
bash
1
kubectl set image deployment/my-webapp-deployment nginx=nginx:1.25
  1. 7. Watch the rollout happen live!
bash
1
kubectl rollout status deployment/my-webapp-deployment

8. Real-World Scenarios

A retail company is deploying a critical Black Friday feature. They initiate a Deployment update. Suddenly, the QA team realizes the new v2 code contains a critical bug that crashes the checkout cart. Because they used a Deployment, the DevOps engineer doesn't panic. They simply type kubectl rollout undo deployment/shopping-cart, and Kubernetes instantly rolls the cluster back to the perfectly stable v1 ReplicaSet, fixing the production outage in seconds.

9. Best Practices

  • Never Modify Pods Managed by a Deployment: If you kubectl exec into a Pod managed by a deployment and manually update a configuration file, your changes will be destroyed the next time the Pod restarts. Always update the deployment.yaml file and let the controller handle the rollout.

10. Common Mistakes

  • Selector Mismatches: A Deployment uses spec.selector.matchLabels to find the Pods it is supposed to manage. If this selector does not EXACTLY match the labels defined in spec.template.metadata.labels, the Deployment will fail to create, throwing cryptic errors about label mismatches.

11. Exercises

  1. 1. If you run kubectl get rs, you often see multiple ReplicaSets for the same application, but all of them except one have a DESIRED count of 0. Explain why.
  1. 2. Outline the steps to perform an emergency rollback of a Deployment to a previous version.

12. FAQs

Q: If I use Deployments, do I ever need to write a Pod YAML file again? A: Rarely. In production, almost 100% of your stateless applications will be wrapped in Deployments. The only time you write naked Pods is usually for temporary, one-off debugging tasks.

13. Interview Questions

  • Q: Explain the hierarchy and relationship between a Deployment, a ReplicaSet, and a Pod. Why does Kubernetes utilize this layered controller architecture?
  • Q: Detail the mechanics of a Kubernetes Rolling Update. How does the Deployment controller guarantee that an application remains available to users during a version transition?

14. Summary

In Chapter 6, we achieved true Container Orchestration. We learned that naked Pods are fragile, but Pods governed by a Deployment are immortal. We utilized ReplicaSets to guarantee strict horizontal scaling and absolute self-healing against node failures. Finally, we executed enterprise-grade deployment strategies, demonstrating how to seamlessly upgrade application versions across a cluster with zero downtime via Rolling Updates, and how to instantly recover from disastrous bugs via Rollbacks.

15. Next Chapter Recommendation

We have 5 Nginx Pods running beautifully. But how does a customer actually view the website? Pod IPs change constantly! We need a stable front door. Proceed to Chapter 7: Kubernetes Services and Networking.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·