Introduction to StatefulSets
We'll cover the following
In this chapter, you’ll learn how to use StatefulSets to deploy and manage stateful applications on Kubernetes.
For the purposes of this chapter, we’re defining a stateful application as an application that creates and saves valuable data. An example might be an app that saves data about client sessions and uses it for future client sessions. Other examples include databases and other data stores.
We’ll divide the chapter as follows:
- The theory of StatefulSets
- Hands-on with StatefulSets
The theory section will introduce you to the way StatefulSets work and what they bring to the table. But don’t worry if you don’t understand everything at first, we’ll cover most of it again when we walk through the hands-on section.
The theory of StatefulSets
It’s often useful to compare StatefulSets with Deployments. Both are first-class objects in the Kubernetes API and follow the typical Kubernetes controller architecture. These controllers run as reconciliation loops that watch the state of the cluster, via the API server, and are constantly moving the observed state of the cluster into sync with the desired state. Deployments and StatefulSets also support self-healing, scaling, updates, and more.
However, there are some vital differences. StatefulSets guarantee:
- Predictable and persistent Pod names
- Predictable and persistent DNS hostnames
- Predictable and persistent volume bindings
These three properties form the state of a Pod, sometimes referred to as the Pods sticky ID. This state/sticky ID is persisted across failures, scaling, and other scheduling operations, making StatefulSets ideal for applications where Pods are a little bit unique and not interchangeable.
As a quick example, failed Pods managed by a StatefulSet will be replaced by new Pods with the exact same Pod name, the exact same DNS hostname, and the exact same volumes. This is true even if the replacement Pod is started on a different cluster Node. The same is not true of Pods managed by a Deployment.
The following YAML snippet shows some of the fields in a typical StatefulSet manifest.
Get hands-on with 1200+ tech skills courses.