Posted in

Understanding StatefulSets in Kubernetes & When to Use Them

StatefulSets are a great way to manage your application’s state. They offer predictable network identifiers and persistent storage that allows you to scale up or down individual instances with ease. However, there are situations when you might not want to use StatefulSets for some applications. For example, if your application has only static configuration files and does not require any stateful services like databases or queues then it can be done without using StatefulSets. There’s indeed a lot to discuss when it comes to comprehending SatefulSets, so let’s just begin.

What is a StatefulSet?

Before we start discussing StatefulSets, let us first understand what a ‘StatefulSet’ is.

A stateful set (SS) is a Kubernetes resource that manages a replicated group of pods that are based on the same template and ensures that all the pods are identical and have the same configuration. It provides high availability for your workloads by ensuring that if one container fails another can take over without any interruption in service or data loss.

StatefulSet Pod Members have Unique Identities, Stable Network Identities and Maintain their States over Time

As the name implies, a StatefulSet is a set of pods whose members have unique identities, stable network identities, and can maintain their states over time.

StatefulSets are useful for applications that require persistent storage. For example, you could use a StatefulSet with kube-stateful-networking to store & serve data from an S3 bucket or object store in Kubernetes.

A StatefulSet is also useful for applications that require ordered startup/shutdown of pods (like log rotation). A lot of workloads like databases or caches need to be started up before they can be used by other services (e.g., web servers). Since each instance has its own IP address assigned by DHCP by default it would be impossible for them all to connect at once without some sort of discovery mechanism between them(e.g., RA) or DNS setup, etc.

Pods in a StatefulSet Provide Predictable Network Identifiers

Apart from the features discussed above, the pods in a StatefulSet also provide predictable network identifiers which are ordered based on their allocation index and each pod gets its own hostname based on the StatefulSet name and its ordinal in the list of pods.

The hostname of each pod in a StatefulSet will be set to subdomain.statefulsetname.namespace.svc .cluster-domain-name (e.g., “my-stateful-set-0 .my_stateful_set/default/svc”).

Each Pod gets its own Persistent Storage that is Retained Even after the Pod is Restarted or Deleted

The first thing to understand about StatefulSets is that they use persistent storage. This means that each pod has its own, separate, persistent volume (PV) for storing data. This is different from Deployments and ReplicaSets which use shared volumes between pods in a StatefulSet.

  • Persistent Storage: Each pod gets its own PVs and retains them even after the pod is restarted or deleted (unless you delete the entire StatefulSet manually).
  • Persistent Volumes: The state of these volumes persists across restarts/deletions of individual pods within a StatefulSet

StatefulSets Simplify scaling up or down Individual Instances for your Application without Affecting other Members of the Cluster

StatefulSets provide an easy way to scale up and down individual instances for your application without affecting other members of the cluster (i.e., it makes scaling easy for stateless applications like WordPress or MySQL).

It provides an easy way to scale up and down individual instances for your application without affecting other members of the cluster (i.e., it makes scaling easy for stateless applications like WordPress or MySQL).

When do you really require StatefulSets?

StatefulSets are useful for applications that require persistent storage and ordered startup/shutdown of pods. For example, if an application needs to store information about its state in the cluster (e.g., counters), then it might use a StatefulSet that stores those values so they can be accessed later.

A similar use case applies when you want predictable network identifiers; this is especially important if your app has multiple components communicating over different networks at different times, such as load balancing between servers or services running in different locations within your datacenter/cloud provider’s infrastructure (e.g., Kubernetes vs AWS). In this scenario, each component may need its own unique hostname for better isolation and resilience against failure or misconfiguration of other parts of the system-but if each component were assigned its own IP address from outside sources such as DHCP servers then there would likely be conflicts between them over which one should be used first by another component at any given time (if they’re not using DNS names).

Scenario 1 – When Your Application Requires Persistent Storage?

Stateful sets are a good option if your application needs to store data. They provide the same level of durability and availability as replication controllers but with an additional benefit: they can be used to store your state data. In addition, it is easy to use them with Kubernetes‘ storage classes and other features that you may want to add on top of them (e.g., using S3).

However, if your application does not require persistent storage then there are other options such as Deployment or PersistentVolumeClaims that will suit better for those cases where storing data is not required for high availability purposes

Scenario 2 – For Ordered Startup/Shutdown of Pods?

StatefulSets are unsuitable for applications requiring ordered startup and pod shutdowns.

StatefulSets are unsuitable when you need to scale up and down individual instances for your application without affecting other cluster members.

Conclusion

StatefulSets are ideal when you need to scale up and down individual instances for your application. This is especially useful in situations where the pods in a cluster are stateless, like WordPress or MySQL. In such cases, you can use StatefulSets to ensure that each of these applications will start up and shut down predictably without affecting any other members of the cluster (i.e., it makes scaling easy for stateless applications like WordPress).

Priya has about 7 years of experience in Market Research. Currently, she is working for Valasys Media, as a content writer, which is amongst the top B2B Media Publishers across the globe. She has been preparing several personalized reports for our clients & has done a lot of research on market segmentation, cluster analysis of audiences & inbound methodologies. She has worked with government institutes as well as corporate houses in several projects. She possesses various interests and believes in a data-driven approach to problem solving. She holds a post-graduation in science also writes extensively on all things about life besides marketing, science, data science and statistics. She is a firm believer in higher realities and that there’s always more to life than we understand. She is a psychic healer and a tarot practitioner, who believes in a spiritual way of living and practices Yoga and meditation. When not writing you can find her enjoying music or cooking. 

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.