Skip to main content

Command Palette

Search for a command to run...

Day - 18 Kubernetes StatefulSets – Managing Stateful Applications

Updated
β€’4 min read

πŸ”Ή Introduction

So far in Kubernetes, we've deployed applications using Deployments.

Deployments work perfectly for:

  • Web Applications

  • APIs

  • Microservices

  • Stateless Workloads

But what about applications like:

  • MySQL

  • PostgreSQL

  • MongoDB

  • Cassandra

  • Elasticsearch

These applications need:

βœ” Persistent Storage
βœ” Stable Network Identity
βœ” Ordered Deployment and Scaling

This is where StatefulSets come into the picture.


πŸ”Ή What is a Stateful Application?

A Stateful Application is an application that stores data and maintains identity.

Examples:

  • Databases

  • Message Queues

  • Distributed Storage Systems

Unlike stateless applications, stateful applications must remember information even after restarts.


Stateless vs Stateful

Stateless Stateful
No data stored Stores data
Any Pod can replace another Each Pod has unique identity
Easy scaling Requires ordered scaling
Deployment used StatefulSet used

πŸ”Ή What is a StatefulSet?

A StatefulSet is a Kubernetes workload resource used to manage stateful applications.

It provides:

βœ” Stable Pod Names
βœ” Persistent Storage
βœ” Ordered Deployment
βœ” Ordered Scaling
βœ” Ordered Deletion


πŸ”Ή Why Not Use Deployments?

Imagine running a database with Deployments.

Pods may get names like:

mysql-a8d4f
mysql-k7t2x
mysql-b9s1c

After restart:

mysql-z4y8p

Identity changes every time.

For databases, this can cause problems.


πŸ”Ή StatefulSet Pod Naming

StatefulSets provide predictable names.

Example:

mysql-0
mysql-1
mysql-2

Even after restart:

mysql-0
mysql-1
mysql-2

The identity remains the same.


πŸ”Ή StatefulSet Architecture

StatefulSet
      β”‚
 β”Œβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”
 β”‚    β”‚    β”‚
 β–Ό    β–Ό    β–Ό
mysql-0
mysql-1
mysql-2
 β”‚     β”‚     β”‚
PVC   PVC   PVC
 β”‚     β”‚     β”‚
PV    PV    PV

Each Pod gets its own storage.


πŸ”Ή StatefulSet Example

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:8

πŸ”Ή Headless Service

StatefulSets usually work with a Headless Service.

Example:

apiVersion: v1
kind: Service
metadata:
  name: mysql

spec:
  clusterIP: None

  selector:
    app: mysql

Notice:

clusterIP: None

This makes the Service Headless.


πŸ”Ή Why Headless Service?

It provides direct DNS records for Pods.

Example:

mysql-0.mysql.default.svc.cluster.local

mysql-1.mysql.default.svc.cluster.local

mysql-2.mysql.default.svc.cluster.local

Each Pod gets a stable hostname.


πŸ”Ή Ordered Deployment

When Kubernetes creates Pods:

mysql-0
↓
mysql-1
↓
mysql-2

Pods start one by one.


πŸ”Ή Ordered Scaling

Scaling from 3 β†’ 5 replicas:

mysql-3
↓
mysql-4

Created sequentially.


πŸ”Ή Ordered Deletion

Deleting Pods:

mysql-4
↓
mysql-3
↓
mysql-2

Removed in reverse order.


πŸ”Ή Persistent Storage with StatefulSets

Each Pod automatically receives its own Persistent Volume Claim.

Example:

mysql-0 β†’ PVC-0
mysql-1 β†’ PVC-1
mysql-2 β†’ PVC-2

Data survives Pod recreation.


πŸ”Ή Real-World Example

Imagine an e-commerce company using MySQL.

Requirements:

βœ” Database data persistence

βœ” Stable hostnames

βœ” Reliable failover

StatefulSets provide all these capabilities.


πŸ”Ή Benefits of StatefulSets

Stable Identity

Pods keep the same names.

Persistent Storage

Data survives restarts.

Ordered Operations

Safer deployment and scaling.

Database Friendly

Perfect for stateful workloads.


πŸ”Ή Common Interview Questions

What is a StatefulSet?

A Kubernetes resource for managing stateful applications.


Difference Between Deployment and StatefulSet?

Deployment StatefulSet
Stateless Apps Stateful Apps
Dynamic Pod Names Stable Pod Names
Shared Behavior Unique Identity
Web Apps Databases

Why Do StatefulSets Need Headless Services?

To provide stable DNS identities for Pods.


Can StatefulSets Use Persistent Volumes?

Yes.

Each Pod gets its own Persistent Volume Claim.


πŸ”Ή Best Practices

βœ” Use StatefulSets for databases

βœ” Use Persistent Volumes

βœ” Configure backups

βœ” Use Headless Services

βœ” Monitor storage usage


πŸ”Ή Key Insight

Stateful applications require more than just containers.

They need:

  • Stable identities

  • Persistent storage

  • Ordered operations

StatefulSets provide all three.


πŸ”Ή Conclusion

StatefulSets are essential when running databases and other stateful workloads in Kubernetes.

They ensure:

βœ” Stable Pod Identity

βœ” Persistent Data

βœ” Reliable Scaling

βœ” Predictable Operations

"Containers may be temporary, but stateful applications require permanence."


πŸš€ Next Blog

Day 19: Kubernetes DaemonSets – Running Pods on Every Node