Stateless & Stateful in Kubernetes

1. Starting Point: How Kubernetes Works at Its Core

Kubernetes is an orchestration engine. Its core mechanism is the declarative model — users declare “this is the desired state” in a manifest (YAML), and the system figures out how to get there on its own.

This declaration is stored via kubectl apply → API Server → etcd. This is the desired state. The Controller continuously compares this value against the actual current state and automatically reconciles any differences. This loop is called the reconciliation loop.

For this loop to run fast and light, Pods must be freely killable, scalable, and replaceable. This is where the need for Stateless design comes in.

Kubernetes philosophy: Pods can die at any time (Cattle, not Pets)


2. Why Stateless Is Necessary

If business data — like session or order data — lives inside a Pod, you can’t freely kill that Pod. Kill it, and the data is gone. And if you scale out, the new Pod has no access to the existing data, forcing you to rely on sticky sessions.

The solution is to move business data outside the Pod. By delegating to external storage like Redis, a DB, or S3, the Pod only processes requests and “remembers” nothing. This kind of Pod is Stateless.

This isn’t about eliminating state — it’s about Separation of Concerns, handing off the responsibility of managing state to a specialist.


3. Stateless vs. Stateful: A Comparison

image.png

StatelessStateful
DefinitionHandles state but doesn’t store itStores state and maintains it persistently
K8s ResourceDeploymentStatefulSet
Pod ReplacementFlexible — just spin up a new oneMust preserve same name and volume
ScalingFreely auto-scalable with HPARequires data sync and membership adjustments
Rolling UpdateZero-downtime deployment possibleData compatibility must be verified
Proportion80–90% of total workloadsData layer: DBs, caches, message queues, etc.
ExamplesAPI servers, web frontends, workersPostgreSQL, Kafka, Redis, Harbor

4. How StatefulSet Differs from Deployment

Why can’t Stateful workloads use a Deployment? Because data consistency and cluster membership must be guaranteed. StatefulSet provides four additional guarantees to address this:

ItemDeploymentStatefulSet
Pod NameRandom suffix (app-7f8b9c-xk2z)Fixed ordinal (db-0, db-1)
Volume BindingLoosely coupled with PodPer-Pod dedicated PVC via volumeClaimTemplates
Creation/Deletion OrderNo ordering guaranteeCreated 0→1→2; deleted in reverse order
Network IDNoneUnique DNS via Headless Service

5. Design Patterns by Environment

How you handle Stateful workloads depends on your environment.

Public Cloud (e.g., EKS) — Stateful workloads are separated out of the cluster using managed services (RDS, ElastiCache, MSK). The cluster is nearly 100% Stateless → easiest to operate.

Air-gapped / On-premises (e.g., RKE2) — Managed services aren’t available. DBs, Kafka, and Harbor must be run directly inside the cluster as StatefulSets → increased operational burden: PVC management, Velero backups, etc.


6. Watch Out for Confusion: Two Meanings of “State”

The word “state” is used at two different layers — and they’re completely separate concepts.

Infrastructure StateApplication State
WhatInfrastructure blueprint (replicas, image, port)Business data (sessions, DB, cache)
Where StoredetcdInside the Pod or external storage
Who ManagesKubernetes control planeDeveloper’s architectural decisions
Related ConceptReconciliation loopStateless / Stateful design

Where the two layers meet: Application state must be moved outside the Pod (Stateless) → Pods become disposable → the reconciliation loop for infrastructure state runs efficiently.


7. etcd: The Storage Backend for Infrastructure State

The desired state mentioned above is actually stored in etcd.

  • Kubernetes’ sole data store (Single Source of Truth)
  • All resources are stored as key-value pairs in the format /registry/{type}/{namespace}/{name}
  • Only the API Server communicates directly with etcd. Controllers, the Scheduler, and kubelets receive changes via watch events from the API Server
  • Distributed store based on Raft consensus → HA configuration with 3+ nodes recommended in production
  • If etcd goes down, the entire cluster goes dark → etcd backup is essential

Data Flow

kubectl apply → API Server (authn/authz/Admission) → stored in etcd → watch event → Controller detects desired ≠ current → creates/deletes Pod → Scheduler assigns node → kubelet runs container


image.png

image.png