kuberneteskubernetes-statefulsetkubernetes-deploymentstatefulset

Why not to use Kubernetes StatefulSet for stateless applications?


I know why use StatefulSet for stateful applications. (e.g. DB or something) In most cases, I can see like "You want to deploy stateful app to k8s? Use StatefulSet!" However, I couldn't see like "You want to deploy stateless app to k8s? Then, DO NOT USE StatefulSet" ever.

Even nobody says "I don't recommend to use StatefulSet for stateless app", many stateless apps is deployed through Deployment, like it is the standard.

The StatefulSet has clear pros for stateful app, but I think Deployment doesn't for stateless app. Is there any pros in Deployment for stateless apps? Or is there any clear cons in StatefulSet for stateless apps?

I supposed that StatefulSet cannot use LoadBalancer Service or StatefulSet has penalty to use HPA, but all these are wrong.

I'm really curious about this question.

P.S. Precondition is the stateless app also uses the PV, but not persists stateful data, for example logs.

I googled "When not to use StatefulSet", "when Deployment is better than StatefulSet", "Why Deployment is used for stateless apps", or something more questions.

I also see the k8s docs about StatefulSet either.


Solution

  • Different Priorities

    What happens when a Node becomes unreachable in a cluster?

    Deployment - Stateless apps

    You want to maximize availability. As soon as Kubernetes detects that there are fewer than the desired number of replicas running in your cluster, the controllers spawn new replicas of it. Since these apps are stateless, it is very easy to do for the Kubernetes controllers.

    StatefulSet - Stateful apps

    You want to maximize availability - but not you must ensure data consistency (the state). To ensure data consistency, each replica has its own unique ID, and there are never multiple replicas of this ID, e.g. it is unique. This means that you cannot spawn up a new replica, unless that you are sure that the replica on the unreachable Node are terminated (e.g. stops using the Persistent Volume).

    Conclusion

    Both Deployment and StatefulSet try to maximize the availability - but StatefulSet cannot sacrifice data consistency (e.g. your state), so it cannot act as fast as Deployment (stateless) apps can.

    These priorities does not only happens when a Node becomes unreachable, but at all times, e.g. also during upgrades and deployments.