Wednesday, August 27, 2025

Kubernets: How statefulsets work in Kubernetes?

A StatefulSet is designed for stateful applications that require stable network identity and persistent storage.


1.  **Desired State:** You define a StatefulSet with a pod template, a desired replica count, and a `volumeClaimTemplates` to manage storage.

2.  **Ordered Pod Creation:** The StatefulSet controller creates pods in a strict, ordered sequence, starting from index `0`. The first pod created will be `web-0`, followed by `web-1`, and so on.

3.  **Stable Identity:** Each pod gets a stable, predictable name and hostname based on its ordinal index (`web-0`, `web-1`).

4.  **Unique Persistent Storage:** For each pod, the StatefulSet controller creates a unique `PersistentVolumeClaim` (PVC) based on the `volumeClaimTemplates`. This PVC ensures that `web-0` always gets the same storage volume, and `web-1` gets its own, separate volume.

5.  **Ordered Scaling and Self-Healing:** The StatefulSet guarantees that pods are created and terminated in order. When scaling down, it terminates pods from the highest ordinal index first (`web-2`, then `web-1`). If a pod dies, the StatefulSet will replace it with a new pod that has the **same identity and connects to the same persistent volume**, ensuring data is not lost. 


sequenceDiagram

    participant User

    participant kubectl

    participant Kubernetes Master

    participant StatefulSet Controller

    participant Kubelet

    participant PersistentVolume Provisioner

    participant Storage

    participant Pod


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

    kubectl->>Kubernetes Master: Create StatefulSet

    Kubernetes Master->>StatefulSet Controller: StatefulSet is created

    

    StatefulSet Controller->>Kubernetes Master: Creates Pod 0

    Kubernetes Master->>PersistentVolume Provisioner: Request Persistent Volume for Pod 0

    PersistentVolume Provisioner->>Storage: Creates new volume

    Storage-->>PersistentVolume Provisioner: Volume created

    PersistentVolume Provisioner-->>Kubernetes Master: PVC created

    Kubernetes Master-->>Kubelet: Schedules Pod 0

    Kubelet->>Node: Launches Pod 0, mounts volume

    Pod->>StatefulSet Controller: Pod 0 Ready

    

    StatefulSet Controller->>Kubernetes Master: Creates Pod 1

    Note over Pod: Waits for Pod 0 to be ready before creating Pod 1

    Kubernetes Master->>PersistentVolume Provisioner: Request Persistent Volume for Pod 1

    PersistentVolume Provisioner->>Storage: Creates new volume

    Storage-->>PersistentVolume Provisioner: Volume created

    PersistentVolume Provisioner-->>Kubernetes Master: PVC created

    Kubernetes Master-->>Kubelet: Schedules Pod 1

    Kubelet->>Node: Launches Pod 1, mounts volume

    Pod->>StatefulSet Controller: Pod 1 Ready

    

    Note right of Pod: Pods have stable identity (pod-0, pod-1) and storage

    

    loop Scaling Down

        User->>kubectl: Scale down to 1 replica

        kubectl->>Kubernetes Master: Update StatefulSet

        Kubernetes Master->>StatefulSet Controller: Scale to 1

        StatefulSet Controller->>Kubernetes Master: Deletes Pod 1

        Kubernetes Master->>Kubelet: Terminates Pod 1

        Kubelet->>Node: Unmounts and deletes Pod 1

        Note over Pod: Pods are terminated in reverse ordinal order

    end



No comments:

Post a Comment