Kubernetes Deployments vs StatefulSets

TL;DR

Deployment is a resource to deploy a stateless application, if using a PVC, all replicas will be using the same Volume and none of it will have its own state.

Persistence in Deployment with Replicas

Statefulsets is used for Stateful applications, each replica of the pod will have its own state, and will be using its own Volume.

Persistence in StatefulSets with Replicas

DaemonSet is a controller similar to ReplicaSet that ensures that the pod runs on all the nodes of the cluster. If a node is added/removed from a cluster, DaemonSet automatically adds/deletes the pod.

Persistence in Daemonsets

I have written about the detailed differences between Deployments, StatefulSets & Daemonsets, and how to deploy a sample application using these Resources K8s: Deployments vs StatefulSets vs DaemonSets.


Deployments and ReplicationControllers are meant for stateless usage and are rather lightweight. StatefulSets are used when state has to be persisted. Therefore the latter use volumeClaimTemplates / claims on persistent volumes to ensure they can keep the state across component restarts.

So if your application is stateful or if you want to deploy stateful storage on top of Kubernetes use a StatefulSet.

If your application is stateless or if state can be built up from backend-systems during the start then use Deployments.

Further details about running stateful application can be found in 2016 kubernetes' blog entry about stateful applications


  • Deployment - You specify a PersistentVolumeClaim that is shared by all pod replicas. In other words, shared volume.

    The backing storage obviously must have ReadWriteMany or ReadOnlyMany accessMode if you have more than one replica pod.

  • StatefulSet - You specify a volumeClaimTemplates so that each replica pod gets a unique PersistentVolumeClaim associated with it. In other words, no shared volume.

    Here, the backing storage can have ReadWriteOnce accessMode.

    StatefulSet is useful for running things in cluster e.g Hadoop cluster, MySQL cluster, where each node has its own storage.

Tags:

Kubernetes