# Understanding Kubernetes: Part 7 -StatefulSet

<figure><img src="https://miro.medium.com/v2/resize:fit:700/1*T8lZvE8yyTI-ZOKJGSOSWA.png" alt="Kubernetes deployment" height="394" width="700"><figcaption></figcaption></figure>

> If you’ve been following our **Kubernetes series 2025**, welcome back! For new readers, check out Part 6: [Understanding Kubernetes: Part 6 -DaemonSets](https://medium.com/@techwithpatil/understanding-kubernetes-part-6-daemonsets-b9e675417e3a)

## What is a StatefulSet in Kubernetes? <a href="#eb8f" id="eb8f"></a>

A StatefulSet in Kubernetes is a resource used to manage stateful applications that require stable, unique network identities and persistent storage. Unlike a Deployment or ReplicaSet, a StatefulSet ensures the pods are created and scaled in a predictable order, making it ideal for applications like databases, distributed systems, and queues.

### For example: <a href="#a9c6" id="a9c6"></a>

If you are deploying a distributed database like Cassandra or a message queue like RabbitMQ, each pod in the StatefulSet needs a unique identity and persistent storage to maintain its state even if the pod restarts. A StatefulSet ensures that each pod gets a consistent hostname and access to its own storage volume.

### StatefulSet Capabilities: <a href="#id-78ce" id="id-78ce"></a>

* **Stable Network Identity**: Each pod has a predictable name (e.g., `app-0`, `app-1`).
* **Persistent Storage**: Each pod gets its own persistent volume, which is retained even after the pod is deleted.
* **Ordered Scaling and Updates**: Pods are created, deleted, and updated in a defined sequence.
* **Resilience**: Ensures data consistency and availability in stateful workloads.

### In my previous role: <a href="#e263" id="e263"></a>

As a Senior DevOps Engineer, I used StatefulSets to deploy a MongoDB cluster in our Kubernetes environment. Each pod required a unique hostname for the replica set configuration and its own persistent volume to store data. The StatefulSet ensured that the MongoDB pods (`mongo-0`, `mongo-1`, `mongo-2`) were created in order, with each pod accessing its dedicated storage. During a scale-up, the StatefulSet added new pods sequentially, maintaining the application’s stability. This setup helped us achieve high availability and data resilience for our backend services.

### Here’s a simple YAML for deploying a StatefulSet: <a href="#id-9ff4" id="id-9ff4"></a>

```
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongo
spec:
  serviceName: mongo-service
  replicas: 3
  selector:
    matchLabels:
      app: mongo
  template:
    metadata:
      labels:
        app: mongo
    spec:
      containers:
      - name: mongo
        image: mongo:5.0
        ports:
        - containerPort: 27017
        volumeMounts:
        - name: mongo-data
          mountPath: /data/db
  volumeClaimTemplates:
  - metadata:
      name: mongo-data
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 10Gi
```

This StatefulSet ensures a three-node MongoDB cluster with stable identities and persistent storage for each pod, making it a robust solution for managing stateful applications in Kubernetes.

## 🚀 Ready to Master Kubernetes? <a href="#c0ab" id="c0ab"></a>

Take your Kubernetes journey to the next level with the **Master Kubernetes: Zero to Hero course!** 🌟 Whether you’re a beginner or aiming to sharpen your skills, this hands-on course covers:

✅ Kubernetes Basics — Grasp essential concepts like nodes, pods, and services.\
✅ Advanced Scaling — Learn HPA, VPA, and resource optimization.\
✅ Monitoring Tools — Master Prometheus, Grafana, and AlertManager.\
✅ Real-World Scenarios — Build production-ready Kubernetes setups.

🎓 **What You’ll Achieve**

💡 Confidently deploy and manage Kubernetes clusters.\
🛡️ Secure applications with ConfigMaps and Secrets.\
📈 Optimize and monitor resources for peak performance.

🔥 Start Learning Now: \[Join the Master Kubernetes Course]\([**https://cloudops0.gumroad.com/l/k8s**](https://cloudops0.gumroad.com/l/k8s))

Don’t miss your chance to become a Kubernetes expert! 💻✨

🚀 Stay ahead in DevOps and SRE! 🔔 [**Subscribe now**](https://techwithpatil.medium.com/subscribe) and never miss a beat on Kubernetes and more. 🌟
