StatefulSet resources help manage stateful applications. They are similar to Deployment resources, but unlike a Deployment resource, they also keep track of state and require Volume and Service resources to operate. StatefulSet resources maintain a sticky identity for each pod. This means that the volume mounted on one pod cannot be used by the other. In aStatefulSet resource, Kubernetes orders pods by numbering them instead of generating a random hash. Pods within a StatefulSet resource are also rolled out and scaled-in in order. If a particular pod goes down and is recreated, the same volume is mounted to the pod.
The following diagram illustrates a StatefulSet resource:
Figure 6.8 – StatefulSet resource
A StatefulSet resource has a stable and unique network identifier, therefore, it requires a headless Service resource. Headless Services are Service resources that do not have a cluster IP. Instead, the Kubernetes DNS resolves the Service resource’s FQDN directly to the pods.
As a StatefulSet resource is supposed to persist data, it requires Persistent Volumes to operate.
Therefore, let’s look at how to manage volumes using Kubernetes.
Managing Persistent Volumes
Persistent Volumes are Kubernetes resources that deal with storage. They can help you manage and mount hard disks, SSDs, filestores, and other block and network storage entities. You can provision Persistent Volumes manually or use dynamic provisioning within Kubernetes. When you use dynamic provisioning, Kubernetes will request the cloud provider via the cloud controller manager to provide the required storage. Let’s look at both methods to understand how they work.