# StatefulSet in Kubernetes

A StatefulSet is a Kubernetes API object designed to manage stateful applications. It ensures that each pod in a workload has a unique, stable identity and persistent storage, even if the pod is rescheduled or restarted. StatefulSets are ideal for applications like databases and distributed systems that require consistent network identities and data persistence.

Let’s say you’re running a database or another application that needs stable, predictable storage and network identities. Stateless workloads, like web servers, work great with Kubernetes Deployments. But for applications where data persistence and order matter, StatefulSets come into play.

A StatefulSet is a Kubernetes API object designed for managing stateful applications. It handles unique challenges like giving each pod a stable identity, persistent storage, and deploying or updating pods in a specific order. Essentially, if your workload needs to remember “who it is” and “where it stores its stuff,” StatefulSets are what you need.

## Why Use StatefulSets?

Now, you might wonder: why not just use a Deployment? The answer lies in the nature of your workload. Stateless apps, like APIs or front-end web servers, don’t care about identity or storage persistence. But databases, distributed systems, and other stateful applications absolutely do.

StatefulSets give you a way to:

- Assign each pod a stable network identity, so they know who they are even after being rescheduled.
- Ensure persistent storage that sticks around even if the pod gets deleted.
- Deploy, scale, and delete pods in a controlled, predictable order, which is critical for applications like databases.

If your application needs these guarantees, a StatefulSet is the right tool for the job.

## How Do StatefulSets Work?

When you create a StatefulSet, Kubernetes assigns each pod a stable identifier (like `web-0`, `web-1`, `web-2`) and links them to their own PersistentVolume. Even if a pod gets deleted or rescheduled to another node, it will keep its identifier and storage.

Additionally, StatefulSets ensure that:

1. Pods are created or updated one at a time, in a defined order.
2. Pods are terminated in reverse order, again one at a time.

This orderly behavior makes them perfect for apps where initialization and termination sequences matter—like databases, which might need to maintain quorum or gracefully shut down.

## When Should You Use a StatefulSet?

Not every application needs a StatefulSet. In fact, most don’t. But here are some common scenarios where StatefulSets shine:

1. **Databases**: If you’re running MySQL, PostgreSQL, or MongoDB, each instance needs a unique identity and persistent storage.
2. **Distributed Systems**: Systems like [Zookeeper,](https://zookeeper.apache.org/) [Kafka](https://kafka.apache.org/), or [Elasticsearch](https://www.elastic.co/elasticsearch) require stable node identities to function properly.
3. **Caching Systems**: Stateful caching layers like [Redis](https://redis.io/) or [Memcached](https://memcached.org/) can benefit from predictable identities to maintain consistency.
4. **Messaging Queues**: Message brokers like [RabbitMQ](https://www.rabbitmq.com/) often rely on stateful configurations and durable storage.

If you’re dealing with applications like these, StatefulSets are a no-brainer.

## How Do You Use a StatefulSet?

Now that you know what StatefulSets are for, let’s talk about how to set one up. Here’s a quick walkthrough of a basic StatefulSet configuration

#### Example YAML

```yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "web"
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
          name: web
  volumeClaimTemplates:
  - metadata:
      name: web-data
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 1Gi
```

Here’s what’s happening:

- Each pod gets a unique [PersistentVolume](/content/finops-glossary/kubernetes-persistent-volumes/index.html) through the `volumeClaimTemplates` section.
- The `serviceName` links the StatefulSet to a headless service, providing stable DNS for each pod (`web-0`, `web-1`, etc.).
- The pods are deployed in a predictable, ordered sequence, ensuring stability.

To create this StatefulSet, just apply the file:

```none
kubectl apply -f statefulset.yaml
```

## What’s the Difference Between StatefulSets and Deployments?

If you’re trying to decide between a Deployment and a StatefulSet, it really comes down to whether your app needs state.

| **Feature** | **StatefulSet** | **Deployment** |
| --- | --- | --- |
| **Purpose** | Manages stateful workloads | Manages stateless workloads |
| **Pod Identity** | Stable, ordered (e.g., `web-0`) | Random pod names |
| **Storage** | Unique PersistentVolume for each pod | Shared or ephemeral storage |
| **Scaling** | Ordered scaling | All pods scaled simultaneously |
| **Use Case** | Databases, distributed systems | Web servers, APIs, batch processing |

If your app doesn’t need stable identities or persistent storage, a Deployment is the simpler, more lightweight option.

## Managing StatefulSets

Once your StatefulSet is up and running, here are a few common commands you’ll use:

- **Scale the StatefulSet**: `kubectl scale statefulset web --replicas=5`
- **Delete the StatefulSet (but keep the storage)**: `kubectl delete statefulset web --cascade=orphan`
- **Check the pods in the StatefulSet**: `kubectl get pods -l app=web`

## Best Practices for StatefulSets

Before you start using StatefulSets, keep these tips in mind:

1. **Pair with Headless Services**: This ensures each pod gets a stable DNS entry.
2. **Monitor Storage Usage**: Ensure your PersistentVolumeClaims have enough capacity for your application.
3. **Plan for Sequential Updates**: StatefulSets create or delete pods one at a time, so operations might take longer than with Deployments.

## Resources for Further Learning on StatefulSets

1. **[Official Kubernetes Documentation](https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#:~:text=StatefulSet%20is%20the%20workload%20API,on%20an%20identical%20container%20spec.)**
2. **[Kubernetes Tutorial](https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/)**
3. **YouTube Tutorial**

Kubernetes Tutorial: Why Do You Need StatefulSets in Kubernetes? - YouTube
