Wednesday, August 27, 2025

Kubernetes: How a ReplicaSet Works

A ReplicaSet is designed to manage stateless applications. It focuses on ensuring a consistent number of identical pods are running.


1.  **Desired State:** You define a ReplicaSet with a pod template and a desired replica count (e.g., `replicas: 3`).

2.  **Pod Creation:** The ReplicaSet controller looks at the current number of running pods that match the template. If the count is less than the desired count, it creates new pods.

3.  **No Stable Identity:** The pods are given random, unique names (`app-1234-abc`, `app-1234-xyz`). They are considered interchangeable.

4.  **No Persistent Storage:** The pods do not have dedicated persistent storage. They are disposable; if a pod is terminated, the ReplicaSet creates a new one, but the data from the terminated pod is lost.

5.  **Scaling and Self-Healing:** If a pod crashes or is terminated, the ReplicaSet controller detects the change and automatically creates a new pod to maintain the desired count. If you manually scale up or down the ReplicaSet, it will add or remove pods accordingly.



Below is sequence diagram showing this 

sequenceDiagram

    participant User

    participant kubectl

    participant Kubernetes Master

    participant ReplicaSet Controller

    participant Kubelet

    participant Node

    participant Pod


    User->>kubectl: Apply ReplicaSet manifest (replicas: 3)

    kubectl->>Kubernetes Master: Create ReplicaSet

    Kubernetes Master->>ReplicaSet Controller: ReplicaSet is created

    ReplicaSet Controller->>Kubernetes Master: Creates Pod 1, Pod 2, Pod 3

    Kubernetes Master-->>Kubelet: Schedules Pods to Nodes

    Kubelet->>Node: Launches Pods

    Pod->>ReplicaSet Controller: Reports Ready

    

    Note over Pod: Pods are random, disposable

    

    loop Monitoring and Self-Healing

        Kubelet->>Kubernetes Master: Pod 2 dies

        Kubernetes Master->>ReplicaSet Controller: Pod 2 is missing

        ReplicaSet Controller->>Kubernetes Master: Creates a new Pod 4

        Kubernetes Master-->>Kubelet: Schedules Pod 4

        Kubelet->>Node: Launches Pod 4

    end




No comments:

Post a Comment