Kubernetes StatefulSet vs. Deployment with Use Cases (2024)

StatefulSets and Deployments are two Kubernetes API objects used to manage sets of identical Pods. They both make it easy to orchestrate multiple Pod replicas, but they have different features that specialize them for separate use cases.

Deployments offer declarative configuration to automate Pod updates and scaling operations, whereas StatefulSets include additional features that help you orchestrate stateful workloads in your cluster.

In this article, we’ll compare both types of objects and discuss when each should be used.

You will learn:

  1. Stateful vs. stateless applications
  2. What is a Kubernetes StatefulSet?
  3. What is a Kubernetes Deployment?
  4. StatefulSet vs. Deployment
  5. StatefulSet and Deployment use cases
  6. What is the difference between a StatefulSet, a Deployment, and a DaemonSet?

Stateful vs. stateless applications

The difference between StatefulSets and Deployments reflects the divide between stateful and stateless systems. As their name suggests, StatefulSets are designed to run your app’s stateful components, while Deployments are used for stateless ones.

A system is stateless when it doesn’t need to store any data within itself. Website and web app frontends are usually stateless, for example. On the other hand, applications such as databases are said to be stateful. They require persistent storage that outlives the lifecycle of individual container replicas.

Kubernetes is best known for managing stateless services. When an app is stateless, its Pods are fully interchangeable; scaling operations won’t result in the loss of any data. This is not true of stateful applications, however. To run a stateful service in Kubernetes, you’ll need to use a StatefulSet to ensure stable Pod replication and data persistence.

What is a Kubernetes StatefulSet?

Kubernetes StatefulSet is an API-object that’s purpose-built to support stateful application components. It creates a set of identically configured Pods from a spec you supply, but each Pod is assigned a non-interchangeable identity. Pods retain their identity if they have to be rescheduled or you scale the StatefulSet.

StatefulSets solve the challenges of running stateful services in Kubernetes. The persistent Pod identities permit storage volumes to be associated with specific Pods inside the StatefulSet. They also facilitate graceful scaling operations and rolling updates, where Pods are added and removed in a predictable order.

When to Use StatefulSets?

Let’s consider a simple example of running three replicas of a MySQL database server in Kubernetes. The deployment should be configured with one Pod in the primary role, handling read-write operations, and the remaining three Pods as MySQL read-only replicas.

Applications connecting to the database will always need to connect to the Pod that’s in the primary role in order to receive read-write access. This wouldn’t be possible if a Deployment or ReplicaSet was used, as scheduling or replication changes would generate new Pod identities. The application would have no way of knowing which Pod is the primary MySQL instance.

StatefulSets eliminate this problem. Each Pod in the StatefulSet is assigned a predictable and consistent network identity in the form <statefulset-name>-<pod-ordinal-index>. The four Pods in the MySQL deployment would be named as follows:

  • mysql-0 – First Pod, in the primary role
  • mysql-1 – Read-only replica
  • mysql-2 – Read-only replica

Now other applications can connect to mysql-0 to reliably interact with the MySQL primary instance. Because StatefulSets also guarantee ordered updates, the vital mysql-0 replica is only terminated if you scale down to zero. The read-only replicas will always be removed first.

Furthermore, the persistent storage characteristics of StatefulSets mean that each Pod will always have its own storage volume reattached, even after it’s rescheduled. Each replica, therefore, maintains its own copy of the database, ensuring reliable, independent data replication.

Similarly, Pods support stable network identities using a headless service that you must separately create.

Example: Creating a Kubernetes StatefulSet

The following manifest sets up a basic StatefulSet object to run three replicas of an application:

apiVersion: apps/v1kind: StatefulSetmetadata: name: appspec: selector: matchLabels: app: app replicas: 3 template: metadata: labels: app: app spec: containers: - name: app image: nginx:latest
$ kubectl apply -f statefulset.yamlstatefulset.apps/app created

The requested three Pods will be created for the StatefulSet:

$ kubectl get podsNAME READY STATUS RESTARTS AGEapp-0 1/1 Running 0 18sapp-1 1/1 Running 0 16sapp-2 1/1 Running 0 14s

Try changing your manifest’s spec.replicas field to 5, then reapply it to your cluster:

$ kubectl apply -f statefulset.yamlstatefulset.apps/app configured

You’ll see that two new Pods are created.

Their identities continue the previously established ordinal sequence:

$ kubectl get podsNAME READY STATUS RESTARTS AGEapp-0 1/1 Running 0 2m7sapp-1 1/1 Running 0 2m5sapp-2 1/1 Running 0 2m3sapp-3 1/1 Running 0 36sapp-4 1/1 Running 0 34s

Next, edit the manifest again to reduce the replica count back to 3.

Repeat the Kubectl command to update the StatefulSet in your cluster:

$ kubectl apply -f statefulset.yamlstatefulset.apps/app configured

You’ll see that Kubernetes terminates Pods in the reverse order of their creation.

The two extra Pods created by the previous change will be removed, leaving the original three Pods intact:

$ kubectl get podsNAME READY STATUS RESTARTS AGEapp-0 1/1 Running 0 13mapp-1 1/1 Running 0 12mapp-2 1/1 Running 0 12m

What is a Kubernetes Deployment?

Deployment is an API object used to manage Pods and ReplicaSets that are part of stateless applications. They support declarative configuration, rollouts, and rollbacks. You use them to automate Pod updates.

Because Deployments use a declarative management model, you only need to define what your desired state looks like. When you apply a Deployment manifest, Kubernetes will automatically compare the state it describes to the current version in your cluster. The Deployment controller will then reconcile the existing state to the new desired state, which results in Pods being added and removed as required.

In practical terms, it allows you to change the number of Pod replicas by adjusting the value in your Deployment’s manifest. Kubernetes will automatically add the correct number of new Pods, or remove existing ones, to achieve the rollout. Deployments also allow you to pause a rollout if you detect a problem and rollback to a previous state.

When to use Deployments?

Use a Deployment to run stateless applications that need to benefit from declarative updates and rollbacks. They permit you to rollout changes safely, without the threat of downtime.

Deployments automatically create Pods and ReplicaSets from a consistent specification you define. This removes much of the administrative burden of configuring, changing, and scaling containerized applications. The Deployment controller handles the orchestration task and will allocate each Pod to the most appropriate Node.

In addition, using a Deployment makes your application more resilient to any failures that occur in your cluster. The Deployment controller will ensure the specified number of Pods are kept running; they’ll be rescheduled automatically after Node failures.

Wrapping Pods in a Deployment is always preferable to running them “bare,” without management by a controller, because those Pods can’t be directly scaled or replicated across your cluster’s Nodes.

Example: Creating a Kubernetes Deployment

Consider the following simple manifest for a Kubernetes Deployment YAML file, which starts three replicas of a Pod running the NGINX web server:

apiVersion: apps/v1kind: Deploymentmetadata: name: nginxspec: selector: matchLabels: app: nginx replicas: 3 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest

Copy this manifest, save it to deployment.yaml in your working directory, and then use Kubectl to apply it to your cluster:

$ kubectl apply -f deployment.yamldeployment.apps/nginx created

Kubernetes will create three Pods to satisfy the declared replica count:

$ kubectl get podsNAME READY STATUS RESTARTS AGEnginx-654975c8cd-c5cqj 1/1 Running 0 13snginx-654975c8cd-lsw22 1/1 Running 0 13snginx-654975c8cd-tkf4s 1/1 Running 0 13s

Try changing the spec.replicas field of your manifest to 5, then reapply it to your cluster and check the updated Pod list:

$ kubectl apply -f deployment.yamldeployment.apps/nginx configured$ kubectl get podsNAME READY STATUS RESTARTS AGEnginx-654975c8cd-7tgnq 1/1 Running 0 43snginx-654975c8cd-c5cqj 1/1 Running 0 106snginx-654975c8cd-lsw22 1/1 Running 0 106snginx-654975c8cd-sfj7m 1/1 Running 0 43snginx-654975c8cd-tkf4s 1/1 Running 0 106s

You can see that two new Pods have been created to match the revised replica count.

Now revert the replica count to 3 and apply the updated manifest once more:

$ kubectl apply -f deployment.yamldeployment.apps/nginx configured$ kubectl get podsNAME READY STATUS RESTARTS AGEnginx-654975c8cd-7tgnq 1/1 Running 0 3m4snginx-654975c8cd-c5cqj 1/1 Running 0 4m7snginx-654975c8cd-sfj7m 1/1 Running 0 3m4s

The output shows that Kubernetes has randomly terminated two Pods to leave three remaining. This demonstrates how Pods in Deployments are considered to be interchangeable, so their identities and order of creation are not tracked.

💡 You might also like:

  • 15 Kubernetes Best Practices to Follow
  • How to Maintain Operations Around Kubernetes Cluster

StatefulSet vs. Deployment

The decision whether to use a StatefulSet or a Deployment should be based on the characteristics of the service you’re deploying in your cluster. Here’s a quick rundown of their main features and differences:

FeatureStatefulSetDeployment
Stateful/Stateless applicationsStatefulStateless
Pod identitiesPods are assigned a persistent identifier, derived from the StatefulSet’s name and their ordinal creation index.Pods are assigned random identifiers, derived from the Deployment’s name and a unique random string.
Pod interchangeabilityPods in a StatefulSet are not interchangeable. It’s expected that each Pod has a specific role, such as always running as a primary or read-only replica for a database application.All Pods are identical, so they’re interchangeable and can be replaced at any time.
Rollout orderingPods are guaranteed to be created and removed in sequence. When you scale down the StatefulSet, Kubernetes will terminate the most recently created Pod.No ordering is supported. When you scale down the Deployment, Kubernetes will terminate a random Pod.
Storage accessEach Pod in the StatefulSet is assigned its own Persistent Volume (PV) and Persistent Volume Claim (PVC).All Pods share the same PV and PVC.

Kubernetes StatefulSet and Deployment use cases

StatefulSets and Deployments both manage Pods, but they’re targeted at specific use cases.

Choose a StatefulSet in the following scenarios:

  • You’re running a stateful application that requires stable, persistent storage, such as a replicated deployment of a database, file server, key-value store, or messaging queue.
  • Pods are non-interchangeable because they each have specific roles, such as being a primary, replica, or worker.
  • You require predictable sequenced rollouts, with terminations that occur in reverse order.

Choose a Deployment when the following criteria apply:

  • Your application is stateless; it does not require persistent storage, or all Pods can share the same storage volume.
  • You require multiple replicas of a Pod to be created from one declaratively defined configuration.
  • You need controlled rollouts and rollbacks, based on changes you make to your declared state.

What is the Difference Between a StatefulSet, a Deployment, and a DaemonSet?

As we’ve discussed in this article, StatefulSets are used to run stateful applications in Kubernetes, whereas Deployments support declarative updates for stateless Pods. DaemonSetsare a third kind of Pod controller that you can also use.

A DaemonSet ensures that all or some of the Nodes in your cluster are always running a replica of a Pod. When new Nodes join the cluster, active DaemonSets will automatically start new Pods on those Nodes. This behavior means DaemonSets are ideal for monitoring systems, logging agents, and hardware integrations, where each Node in your cluster must permanently run an instance of the application.

Key points

In this article, we’ve explained the differences between the Kubernetes StatefulSet and Deployment objects. You’ve learned how you can use the objects to easily run both stateful and stateless applications inside your clusters.

A StatefulSet should be chosen for stateful components that require predictable Pod identities, ordered rollouts, and stable storage access. Select a Deployment instead when your service is stateless, doesn’t require persistent storage, and won’t be affected by changes to Pod identities.

If you need any assistance with managing your Kubernetes projects, take a look at Spacelift. It brings with it a GitOps flow, so your Kubernetes Deployments are synced with your Kubernetes Stacks, and pull requests show you a preview of what they’re planning to change. It also has an extensive selection of policies, which lets you automate compliance checks and build complex multi-stack workflows. You can check it for free by creating a trial account.

Manage Kubernetes Easier and Faster

Spacelift allows you to automate, audit, secure, and continuously deliver your infrastructure. It helps overcome common state management issues and adds several must-have features for infrastructure management.

Start free trial

Kubernetes StatefulSet vs. Deployment with Use Cases (2024)

FAQs

Why use StatefulSet instead of deployment? ›

Unlike a Deployment, a StatefulSet maintains a sticky identity for each of its Pods. These pods are created from the same spec, but are not interchangeable: each has a persistent identifier that it maintains across any rescheduling.

When to use Kubernetes StatefulSet? ›

Use a StatefulSet each time you deploy a service with non-interchangeable Pods. Without StatefulSets, critical replicas could be replaced after you scale your deployment or Pods get rescheduled.

How does StatefulSet use differ from the use of stateless pod with persistent volumes? ›

Given this difference, Deployment is more suited to work with stateless applications. As far as a Deployment is concerned, Pods are interchangeable. While a StatefulSet keeps a unique identity for each Pod it manages. It uses the same identity whenever it needs to reschedule those Pods.

What is the best deployment strategy in Kubernetes? ›

Top Kubernetes Deployment Strategies
  1. Recreate Deployment. A recreate deployment strategy is an all-or-nothing process that lets you update an application immediately, with some downtime. ...
  2. Rolling Deployment. ...
  3. Blue/Green Deployment (Red/Black Deployment) ...
  4. Canary Deployment. ...
  5. A/B Testing. ...
  6. Shadow Deployment.

Can I delete a pod in StatefulSet? ›

Deleting the Pods in a StatefulSet will not delete the associated volumes. This is to ensure that you have the chance to copy data off the volume before deleting it.

Why does StatefulSet need headless service? ›

Using a headless service inside a StatefulSet in Kubernetes enables direct communication between individual pods while maintaining stable network identities and DNS-based service discovery.

When you deploy a stateful service, it is deployed as? ›

Kubernetes uses the StatefulSet controller to deploy stateful applications as StatefulSet objects. Pods in StatefulSets are not interchangeable: each Pod has a unique identifier that is maintained no matter where it is scheduled.

When should we not use Kubernetes? ›

While it is possible to have a monolithic application in one container, the container would require multiple processes. This isn't considered good practice — one container should only house one process. If you are not containerized, then Kubernetes is not for you ... yet.

What are the different ways to deploy a stateful application in Kubernetes? ›

3 Stateful Deployment Options in Kubernetes. There are three main options for running stateful workloads in a Kubernetes cluster: running it outside the cluster, as a cloud service alongside your cluster, or within the Kubernetes cluster.

How are the pods that make up a StatefulSet distinguished from one another? ›

This identity is based on a unique ordinal index that is assigned to each Pod by the StatefulSet controller. The Pods' names take the form <statefulset name>-<ordinal index> . Since the web StatefulSet has two replicas, it creates two Pods, web-0 and web-1 .

What is the difference between Kubernetes replica set and StatefulSet? ›

StatefulSet is also a Controller, but unlike Kubernetes Deployment, it doesn't create ReplicaSet rather, it creates the pod with a unique naming convention. Each pod receives DNS name according to the pattern: <statefulset name>-<ordinal index>. For example, for StatefulSet with the name mysql, it will be mysql-0.

What is the difference between stateless and stateful use cases? ›

The key difference between stateful and stateless applications is that stateless applications don't “store” data whereas stateful applications require backing storage. Stateful applications like the Cassandra, MongoDB and mySQL databases all require some type of persistent storage that will survive service restarts.

What is the difference between StatefulSet and deployment? ›

Application type: Deployments are designed for stateless applications where each instance can be treated as identical. StatefulSets are built for stateful applications that require persistent storage and stable network identities.

What is the simplest way to deploy Kubernetes? ›

Prerequisites for Kubernetes Deployment
  1. STEP 1: Installing of Kubernetes on master and work nodes:
  2. STEP 2: Clone a repository.
  3. STEP 3: Building an image from Dockfile.
  4. STEP 4: Push the Docker image to a Registry.
  5. Login to the Docker hub.
  6. Push the image to register.
  7. Command - Line Interface:
  8. Yaml Manifest.
Apr 12, 2024

What is the main advantage of using deployments in Kubernetes? ›

Kubernetes services let you grow without needing to rearchitect your infrastructure. Kubernetes schedules and automates container deployment across multiple compute nodes, whether on the public cloud, onsite VMs or physical on-premises machines. Its automatic scaling lets teams scale up or down to meet demand faster.

Why use ReplicaSet instead of deployment? ›

A ReplicaSet ensures that a specified number of pod replicas are running at any given time. However, a Deployment is a higher-level concept that manages ReplicaSets and provides declarative updates to Pods along with a lot of other useful features.

Can you use a deployment for stateful applications? ›

Kubernetes uses the StatefulSet controller to deploy stateful applications as StatefulSet objects. Pods in StatefulSets are not interchangeable: each Pod has a unique identifier that is maintained no matter where it is scheduled.

What is the difference between daemonset and StatefulSet? ›

Understanding Kubernetes Deployments, StatefulSets, and DaemonSets is essential for effectively managing applications in a Kubernetes cluster. Deployments are ideal for stateless applications, StatefulSets for stateful applications requiring persistent storage, and DaemonSets for running background tasks on every node.

What is the difference between StatefulSet and operator in Kubernetes? ›

You would often use Statefulset if you want your application to have some persistence. Operators on the other hand are a pattern used in Kubernetes to extend the normal functionality by adding custom resource definitions (CRD) that are handled by a given operator.

Top Articles
How to Pay Toward the Principal on a Car Loan | LendingTree
Over 90% of derivative traders lost money: Why you must avoid the trap of derivative trading
11 beste sites voor Word-labelsjablonen (2024) [GRATIS]
Pet For Sale Craigslist
It's Official: Sabrina Carpenter's Bangs Are Taking Over TikTok
Ets Lake Fork Fishing Report
Otterbrook Goldens
Hotels Near 500 W Sunshine St Springfield Mo 65807
Jonathan Freeman : "Double homicide in Rowan County leads to arrest" - Bgrnd Search
Gameday Red Sox
How Far Is Chattanooga From Here
Autozone Locations Near Me
Globe Position Fault Litter Robot
Slmd Skincare Appointment
Lonadine
Cooking Fever Wiki
Walmart Windshield Wiper Blades
Overton Funeral Home Waterloo Iowa
Canvas Nthurston
Craigslist Toy Hauler For Sale By Owner
Daylight Matt And Kim Lyrics
Full Standard Operating Guideline Manual | Springfield, MO
UPS Store #5038, The
Schedule An Oil Change At Walmart
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Mega Personal St Louis
Directions To Cvs Pharmacy
At 25 Years, Understanding The Longevity Of Craigslist
3 Ways to Drive Employee Engagement with Recognition Programs | UKG
Napa Autocare Locator
Acuity Eye Group - La Quinta Photos
Dreamcargiveaways
Tamilrockers Movies 2023 Download
Unlock The Secrets Of "Skip The Game" Greensboro North Carolina
Koninklijk Theater Tuschinski
Trizzle Aarp
Sept Month Weather
10 Rarest and Most Valuable Milk Glass Pieces: Value Guide
Vindy.com Obituaries
Content Page
30 Years Of Adonis Eng Sub
The Complete Uber Eats Delivery Driver Guide:
Stoughton Commuter Rail Schedule
300+ Unique Hair Salon Names 2024
Motorcycle For Sale In Deep East Texas By Owner
Mmastreams.com
Roller Znen ZN50QT-E
Sj Craigs
De Donde Es El Area +63
Vrca File Converter
Craigslist Yard Sales In Murrells Inlet
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated:

Views: 6016

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.