LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
Kubernetes

Deploying Your First Workload: Pods & ReplicaSets

When you start learning Kubernetes (K8s), you quickly realize that it does not run Docker containers directly. Instead, Kubernetes wraps your containers inside a higher-level structure called a Pod. However, deploying a naked Pod into a production cluster is dangerous—if the node it lives on crashes, the Pod dies forever. That is where ReplicaSets come in, acting as the vigilant guardians of your application's high availability.

Deploying Your First Workload: Pods & ReplicaSets

When you start learning Kubernetes (K8s), you quickly realize that it does not run Docker containers directly. Instead, Kubernetes wraps your containers inside a higher-level structure called a Pod. However, deploying a naked Pod into a production cluster is dangerous—if the node it lives on crashes, the Pod dies forever. That is where ReplicaSets come in, acting as the vigilant guardians of your application's high availability.

What you will learn
  • The architectural difference between a Container, a Pod, and a ReplicaSet.
  • How to write declarative YAML files to define and launch your workloads.
  • How Labels and Selectors glue Kubernetes resources together.
  • How to test self-healing by intentionally breaking a running application.

1. The Kubernetes Workload Hierarchy

To deploy an application successfully, you must understand the relationship between the code you write and the environment that keeps it running.

Concept What it is in simple terms Analogy
Container Your application code packaged with its dependencies (e.g., a Docker image). A single engine
Pod The smallest deployable unit in Kubernetes. It holds one or more containers that share storage and a network IP. A car holding the engine
ReplicaSet A controller that ensures a specified number of exact Pod copies are always running at any given time. A fleet manager replacing broken cars

2. Practical Implementation & Code

In Kubernetes, we manage resources declaratively. We don't tell the system how to run our app; we write a YAML file describing what we want, and Kubernetes makes it happen.

Writing the ReplicaSet YAML

Example 1: my-first-replicaset.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend-replicaset
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web-frontend
  template:
    metadata:
      labels:
        app: web-frontend
    spec:
      containers:
      - name: nginx-container
        image: nginx:latest
        ports:
        - containerPort: 80

Expected output (after running kubectl apply -f my-first-replicaset.yaml)

replicaset.apps/frontend-replicaset created
Important: Notice how the selector.matchLabels perfectly matches the template.metadata.labels. This is critical. The ReplicaSet uses this label to identify which Pods it owns. If these labels don't match, the ReplicaSet will fail to manage the Pods.

3. Architecture / Workflow Visualized

When you submit that YAML file to the Kubernetes API, a control loop begins. The ReplicaSet acts like a thermostat. You set the desired state to "3 Pods". If a server crashes and you drop to 2 Pods, the ReplicaSet detects the temperature drop and fires up a new Pod to get back to 3.

ReplicaSet Self-Healing Workflow

ReplicaSet (Desired: 3) Pod 1 app: web-frontend Pod 2 app: web-frontend Pod 3 app: web-frontend

The ReplicaSet constantly compares the "Desired State" (3) with the "Actual State" via label selectors. The dashed lines represent continuous reconciliation loops.

4. Step-by-Step Execution Breakdown

What actually happens in the background when you type kubectl apply -f my-first-replicaset.yaml?

Step 1: The Request: The command converts your YAML into JSON and sends it to the Kubernetes API Server via an HTTP POST request.
Step 2: State Storage: The API Server authenticates you, validates the request, and saves this new "desired state" into etcd, the cluster's main database.
Step 3: The Controller Wakes Up: The ReplicaSet Controller sees a new entry in etcd. It notices 0 pods currently match the label app: web-frontend, but the desired state is 3. It immediately requests the API Server to create 3 new Pods.
Step 4: Scheduling & Running: The Kubernetes Scheduler assigns these 3 new pending Pods to healthy worker nodes. The kubelet on each node instructs the container runtime (like containerd) to pull the Nginx image and start the containers.
Watch out: Never create a naked Pod for a production app without a ReplicaSet (or Deployment). If a naked Pod crashes, it stays dead. Kubernetes only revives Pods that are managed by a controller.

5. Hands-On Practice

If you have access to a Minikube or cloud Kubernetes cluster, follow these steps to see the magic in action.

Beginner Exercise 1: Create and View

Apply the YAML file provided in Section 2, then run kubectl get rs and kubectl get pods.

Hint: You should see 1 ReplicaSet and exactly 3 Pods running.

Intermediate Exercise 2: Prove Self-Healing

Delete one of your running Pods manually using kubectl delete pod [pod-name]. Immediately run kubectl get pods.

Expected result: You will see the Pod you deleted terminating, but a brand new Pod will already be in the "ContainerCreating" phase. The ReplicaSet replaced it instantly.

Advanced Exercise 3: Label Hijacking

Manually create a standalone Pod that has the label app: web-frontend. What happens to the ReplicaSet?

Hint: Use kubectl run fake-pod --image=nginx --labels="app=web-frontend". Because the ReplicaSet desires exactly 3 Pods, discovering a 4th Pod with matching labels forces it to terminate the youngest one!

6. Interview Questions

If you are applying for a DevOps, Cloud, or Backend Architecture role, expect to answer questions about these fundamental concepts.

Q1. Why do we deploy Pods instead of deploying containers directly?

Answer: Pods act as a logical host for containers. By deploying Pods, Kubernetes allows closely related processes (like an application container and a logging sidecar container) to share the same local network namespace (localhost) and storage volumes natively, without needing complex port mapping or networking setups.

Q2. How does a ReplicaSet know which Pods belong to it?

Answer: Through Labels and Selectors. The ReplicaSet does not track Pods by their unique IDs or names. It simply uses a label selector (defined in the YAML) to query the API server for all Pods possessing a specific label. If the count matches the desired replicas, it does nothing. If it is too low, it creates more; if it is too high, it terminates some.

Conclusion

Pods and ReplicaSets represent the core building blocks of Kubernetes architecture. By understanding that Pods wrap your containers, and ReplicaSets protect your Pods through continuous reconciliation loops, you unlock the primary benefit of orchestration: high availability and self-healing. In real-world enterprise deployments, you will rarely write a ReplicaSet directly; instead, you will write a Deployment, which manages ReplicaSets for you to allow zero-downtime rolling updates. But mastering the ReplicaSet is the prerequisite to mastering the entire system.

Official Documentation

Upgradix by Shrash Studio – Learn Technology. Build Your Future.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.