StatefulSets vs Deployments: Running Stateful Applications in Kubernetes

StatefulSets vs Deployments: Running Stateful Applications in Kubernetes

Kubernetes is designed to manage both stateless and stateful applications, but not all workloads can be treated the same way.

For example:

A microservice-based web app can scale dynamically without worrying about specific pod identity → Use Deployments

A database like MySQL or Kafka requires persistent storage and stable networkingUse StatefulSets

So, what’s the difference between Deployments and StatefulSets, and when should you use each? Let’s explore! 🚀


🔹 What is a Kubernetes Deployment?

A Deployment is used to manage stateless applications—workloads that don’t need to maintain a specific identity or persistent storage across pod restarts.

✔ Automatically scales up/down by creating and terminating pods.

✔ Replaces failed pods with new instances that don’t retain identity.

Uses random pod names (e.g., nginx-abcde).

💡 Best for:

🔹 Microservices (e.g., web servers, APIs)

🔹 Stateless applications that can be horizontally scaled

🔹 Applications that don’t need persistent storage


🔹 What is a Kubernetes StatefulSet?

A StatefulSet is used for stateful applications—workloads that require:

Stable and unique network identity (each pod gets a unique, predictable name).

Persistent storage that survives restarts and pod rescheduling.

Ordered scaling and rolling updates (pods start and terminate in a defined sequence).

💡 Best for:

🔹 Databases (MySQL, PostgreSQL, MongoDB)

🔹 Distributed systems (Kafka, Zookeeper, ElasticSearch)

🔹 Apps requiring persistent storage (message queues, stateful services)


🔹 Key Differences Between Deployments and StatefulSets

Article content

💡 Deployments treat pods as interchangeable, while StatefulSets assign each pod a stable identity.


🔹 Scenario 1: Deploying a Stateless Web Application (Using Deployments)

📌 Overview

We will deploy an Nginx web server using Deployments, demonstrating scalability and automatic pod replacement.

💡 Use Case:

Handles multiple HTTP requests without worrying about persistent state

Can scale up/down dynamically


Step 1: Define a Deployment for Nginx

Create nginx-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80        

Apply the Deployment:

kubectl apply -f nginx-deployment.yaml        

Check running pods:

kubectl get pods        

Expected Output:

NAME                               READY   STATUS    RESTARTS   AGE
nginx-deployment-abcde             1/1     Running   0          5s
nginx-deployment-xyz12             1/1     Running   0          5s
nginx-deployment-pqrst             1/1     Running   0          5s        

💡 Note: Each pod gets a random name, and they can be replaced freely.

A stateless application like Nginx can handle web traffic without worrying about persistent storage!


Step 2: Scaling the Deployment

Scale up to 5 replicas:

kubectl scale deployment nginx-deployment --replicas=5        

Check running pods again:

kubectl get pods        

New pods are created dynamically without affecting existing ones!


🔹 Scenario 2: Deploying a Stateful Database (Using StatefulSets)

📌 Overview

We will deploy a MySQL database using StatefulSets, ensuring each pod has stable storage and network identity.

💡 Use Case:

Databases requiring persistent storage

Pods must retain their identity and order of execution


Step 1: Define a Persistent Volume for MySQL

Create mysql-pv.yaml:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: "/mnt/data"        

Apply it:

kubectl apply -f mysql-pv.yaml        

Each MySQL pod will get persistent storage!


Step 2: Deploy a StatefulSet for MySQL

Create mysql-statefulset.yaml:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  serviceName: mysql
  replicas: 3
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "password"
        ports:
        - containerPort: 3306
        volumeMounts:
        - name: mysql-data
          mountPath: "/var/lib/mysql"
  volumeClaimTemplates:
  - metadata:
      name: mysql-data
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 5Gi        

Apply the StatefulSet:

kubectl apply -f mysql-statefulset.yaml        

Each MySQL pod now has persistent storage!


Step 3: Verify Stateful Behavior

Check running pods:

kubectl get pods        

Expected Output:

NAME     READY   STATUS    RESTARTS   AGE
mysql-0  1/1     Running   0          10s
mysql-1  1/1     Running   0          15s
mysql-2  1/1     Running   0          20s        

💡 Each pod has a predictable name (mysql-0, mysql-1, mysql-2) and starts in order.

StatefulSets ensure ordered, stable deployment for databases!


🔹 Key Takeaways

Article content

💡 Use Deployments for stateless workloads and StatefulSets for stateful applications!


🚀 Let’s Discuss!

Which one do you use more—Deployments or StatefulSets? Have you faced challenges running databases or stateful workloads in Kubernetes? Let’s discuss in the comments!

Follow Bavithran M for more DevOps, Kubernetes, and cloud-native insights.

Found this useful? Share it with your network!

Ashu Sharma

Helping SaaS Founders Scale Faster with AI-Driven Automation | Custom Dashboards & Streamlined Workflows for Better Decisions

2mo

This is a great post, Bavithran! The explanation of StatefulSets vs. Deployments is clear and concise. I'm particularly interested in how these choices impact application architecture, especially when considering scaling and persistent storage. While my expertise lies primarily in full-stack development (JavaScript, React, Node.js, MongoDB) and AI-driven applications, I often work with teams building scalable web applications that ultimately need to integrate with Kubernetes. If you or anyone in your network is working on a project that requires a robust, user-friendly front-end or needs help integrating AI features for improved user experience – perhaps even building a new or updating an existing website – I'm open to freelance opportunities, so feel free to connect if you (or anyone in your network) has projects that could benefit from my expertise. You can check out my portfolio here: [https://ashish-sharma-portfolio-phi.vercel.app](https://ashish-sharma-portfolio-phi.vercel.app) Let's connect and see how I can add value!

Like
Reply
RISWAN RAJA A

🌐 Cloud DevOps | ☁️ Azure | 🛠️ Terraform | 🐳 Docker | 🚀 Kubernetes | ⚙️ Infrastructure Automation Enthusiast | 📈 Driving Scalability & Innovation

2mo

Well structured, makes sense

Like
Reply
Bavithran M

Senior Cloud & DevOps Engineer | AWS & Azure Certified | Kubernetes & Automation Advocate | Training | Mentoring | Uplifting IT Professionals

2mo

#connections

Like
Reply

To view or add a comment, sign in

More articles by Bavithran M

Insights from the community

Others also viewed

Explore topics