Skip to content

Stateful workloads in Kubernetes

In the context of containerized applications, a stateful workload is any application that requires persistent data to function correctly.^[400-devops-06-kubernetes-basics-readme.md] Unlike stateless services which can be replaced or restarted without losing data, stateful applications rely on the retention of data across container termination events^[400-devops-06-kubernetes-basics-readme.md].

Data Persistence

A core concept in [[Containers]] is their ephemeral nature. When a container is terminated, everything inside the container is lost^[400-devops-06-kubernetes-basics-readme.md]. Therefore, running stateful workloads requires decoupling the application lifecycle from the data lifecycle.

This is typically achieved using volumes or mounts. For example, a database like [[MySQL]] stores its data in a specific directory (e.g., /var/lib/mysql), and this directory must be mapped to persistent storage to ensure data survives container restarts^[400-devops-06-kubernetes-basics-readme.md].

In Kubernetes, this persistence is managed through: * Persistent Volumes: Represents physical storage resources^[400-devops-06-kubernetes-basics-readme.md]. * StorageClass: Defines the types of storage available (e.g., standard, fast SSD)^[400-devops-06-kubernetes-basics-readme.md].

Kubernetes Workloads

Kubernetes provides specific controllers to manage these applications:

[[StatefulSets]]

For applications requiring stable identities and persistent storage, Kubernetes uses the StatefulSet controller^[400-devops-06-kubernetes-basics-readme.md]. This workload API object is designed to manage stateful applications, managing the deployment and scaling of a set of Pods while providing guarantees about the ordering and uniqueness of these Pods^[400-devops-06-kubernetes-basics-readme.md].

[[Deployments]]

For stateless applications or those that do not require strict identity guarantees, the Deployment controller is typically used^[400-devops-06-kubernetes-basics-readme.md].

Sources

  • 400-devops-06-kubernetes-basics-readme.md