{"id":498318,"date":"2022-10-05T03:48:54","date_gmt":"2022-10-05T00:48:54","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/what-are-kubernetes-statefulsets-when-should-you-use-them\/"},"modified":"2022-10-05T03:48:54","modified_gmt":"2022-10-05T00:48:54","slug":"what-are-kubernetes-statefulsets-when-should-you-use-them","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/what-are-kubernetes-statefulsets-when-should-you-use-them\/","title":{"rendered":"#What Are Kubernetes StatefulSets? When Should You Use Them?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a3a11cdb2bc5\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a3a11cdb2bc5\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-kubernetes-statefulsets-when-should-you-use-them\/#%E2%80%9CWhat_Are_Kubernetes_StatefulSets_When_Should_You_Use_Them%E2%80%9D\" >&#8220;What Are Kubernetes StatefulSets? When Should You Use Them?&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-kubernetes-statefulsets-when-should-you-use-them\/#What_Are_StatefulSets\" >What Are StatefulSets?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-kubernetes-statefulsets-when-should-you-use-them\/#StatefulSet_Use_Cases\" >StatefulSet Use Cases<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-kubernetes-statefulsets-when-should-you-use-them\/#Creating_a_StatefulSet\" >Creating a StatefulSet<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-kubernetes-statefulsets-when-should-you-use-them\/#Rolling_Updates\" >Rolling Updates<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-kubernetes-statefulsets-when-should-you-use-them\/#Limitations\" >Limitations<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-kubernetes-statefulsets-when-should-you-use-them\/#When_Not_To_Use_a_StatefulSet\" >When Not To Use a StatefulSet<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-kubernetes-statefulsets-when-should-you-use-them\/#Summary\" >Summary<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CWhat_Are_Kubernetes_StatefulSets_When_Should_You_Use_Them%E2%80%9D\"><\/span>&#8220;What Are Kubernetes StatefulSets? When Should You Use Them?&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-806255\" data-pagespeed-no-defer=\"\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/Kubernetes-New.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Graphic with the Kubernetes logo\" width=\"1202\" height=\"677\"\/><\/p>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/controllers\/statefulset\">StatefulSets<\/a> are Kubernetes objects used to consistently deploy stateful <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lication components. Pods created as part of a StatefulSet are given persistent identifiers that they retain even when they\u2019re rescheduled.<\/p>\n<p>A StatefulSet can deploy applications that need to reliably identify specific replicas, rollout updates in a pre-defined order, or stably access storage volumes. They\u2019re applicable to many different use cases but are most commonly used for databases and other types of persistent data store.<\/p>\n<p>In this article you\u2019ll learn what StatefulSets are, how they work, and when you should use them. We\u2019ll also cover their limitations and the situations where other Kubernetes objects are a better choice.<\/p>\n<h2 id=\"what-are-statefulsets\"><span class=\"ez-toc-section\" id=\"What_Are_StatefulSets\"><\/span>What Are StatefulSets?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Making Pods part of a StatefulSet instructs Kubernetes to schedule and scale them in a guaranteed manner. Each Pod gets allocated a unique identity which any replacement Pods retain.<\/p>\n<p>The Pod name is suffixed with an ordinal index that defines its order during scheduling operations. A StatefulSet called <code>mysql<\/code> containing three replicas will create the following named Pods:<\/p>\n<p>Pods use their names as their hostname so other services that need to reliably access the second replica of the StatefulSet can connect to <code>mysql-2<\/code>. Even if the specific Pod that runs <code>mysql-2<\/code> gets rescheduled later on, its identity will pass to its replacement.<\/p>\n<p>StatefulSets also enforce that Pods are removed in reverse order of their creation. If the StatefulSet is scaled down to one replica, <code>mysql-3<\/code> is guaranteed to exit first, followed by <code>mysql-2<\/code>. This behavior <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/controllers\/statefulset\/#limitations\">doesn\u2019t apply<\/a> when the entire StatefulSet is deleted and can be disabled by setting a StatefulSet\u2019s <code>podManagementPolicy<\/code> field to <code>Parallel<\/code>.<\/p>\n<h2 id=\"statefulset-use-cases\"><span class=\"ez-toc-section\" id=\"StatefulSet_Use_Cases\"><\/span>StatefulSet Use Cases<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>StatefulSets are normally used to run replicated applications where individual Pods have different roles. As an example, you could be deploying a MySQL database with a primary instance and two read-only replicas. A regular ReplicaSet or Deployment would not be appropriate because you couldn\u2019t reliably identify the Pod running the primary replica.<\/p>\n<p>StatefulSets address this by guaranteeing that each Pod in the ReplicaSet maintains its identity. Your other services can reliably connect to <code>mysql-1<\/code> to interact with the primary replica. ReplicaSets also enforce that new Pods are only started when the previous Pod is running. This ensures the read-only replicas get created after the primary is up and ready to expose its data.<\/p>\n<p>The purpose of StatefulSets is to accommodate non-interchangeable replicas inside Kubernetes. Whereas Pods in a stateless application are equivalent to each other, stateful workloads require an intentional approach to rollouts, scaling, and termination.<\/p>\n<p>StatefulSets integrate with <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/blog\/2019\/04\/04\/kubernetes-1.14-local-persistent-volumes-ga\">local persistent volumes<\/a> to support persistent storage that sticks to each replica. Each Pod gets access to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/controllers\/statefulset\/#stable-storage\">its own volume<\/a> that will be automatically reattached when the replica\u2019s rescheduled to another node.<\/p>\n<h2 id=\"creating-a-statefulset\"><span class=\"ez-toc-section\" id=\"Creating_a_StatefulSet\"><\/span>Creating a StatefulSet<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Here\u2019s an example YAML manifest that defines a stateful set for running MySQL with a primary node and two replicas:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yaml\">\n<pre class=\"de1\"><strong class=\"co3\">apiVersion<\/strong><strong class=\"sy2\">: <\/strong>v1<strong class=\"co3\">\nkind<\/strong><strong class=\"sy2\">: <\/strong>Service<strong class=\"co4\">\nmetadata<\/strong>:<strong class=\"co3\">\n  name<\/strong><strong class=\"sy2\">: <\/strong>mysql<strong class=\"co4\">\n  labels<\/strong>:<strong class=\"co3\">\n    app<\/strong><strong class=\"sy2\">: <\/strong>mysql<strong class=\"co4\">\nspec<\/strong>:<strong class=\"co4\">\n  ports<\/strong>:<strong class=\"co3\">\n    - name<\/strong><strong class=\"sy2\">: <\/strong>mysql<strong class=\"co3\">\n      port<\/strong><strong class=\"sy2\">: <\/strong>3306<strong class=\"co3\">\n  clusterIP<\/strong><strong class=\"sy2\">: <\/strong><strong class=\"kw1\">None<\/strong><strong class=\"co4\">\n  selector<\/strong>:<strong class=\"co3\">\n    app<\/strong><strong class=\"sy2\">: <\/strong>mysql\n<strong class=\"sy1\">---<\/strong><strong class=\"co3\">\napiVersion<\/strong><strong class=\"sy2\">: <\/strong>apps\/v1<strong class=\"co3\">\nkind<\/strong><strong class=\"sy2\">: <\/strong>StatefulSet<strong class=\"co4\">\nmetadata<\/strong>:<strong class=\"co3\">\n  name<\/strong><strong class=\"sy2\">: <\/strong>mysql<strong class=\"co4\">\nspec<\/strong>:<strong class=\"co4\">\n  selector<\/strong>:<strong class=\"co4\">\n    matchLabels<\/strong>:<strong class=\"co3\">\n      app<\/strong><strong class=\"sy2\">: <\/strong>mysql<strong class=\"co3\">\n  serviceName<\/strong><strong class=\"sy2\">: <\/strong>mysql<strong class=\"co3\">\n  replicas<\/strong><strong class=\"sy2\">: <\/strong>3<strong class=\"co4\">\n  template<\/strong>:<strong class=\"co4\">\n    metadata<\/strong>:<strong class=\"co4\">\n      labels<\/strong>:<strong class=\"co3\">\n        app<\/strong><strong class=\"sy2\">: <\/strong>mysql<strong class=\"co4\">\n    spec<\/strong>:<strong class=\"co4\">\n      initContainers<\/strong>:<strong class=\"co3\">\n      - name<\/strong><strong class=\"sy2\">: <\/strong>mysql-init<strong class=\"co3\">\n        image<\/strong><strong class=\"sy2\">: <\/strong>mysql:8.0<strong class=\"co4\">\n        command<\/strong><strong class=\"sy2\">:\n<\/strong>        - bash\n        - <strong class=\"st0\">\"-c\"<\/strong>\n        - <strong class=\"sy2\">|\n<\/strong><strong class=\"co0\">          set -ex\n          [[ `hostname` =~ -([0-9]+)$ ]] || exit 1\n          ordinal=${BASH_REMATCH[1]}\n          echo [mysqld] &gt; \/mnt\/conf\/server-id.cnf\n          # MySQL doesn't allow \"0\" as a `server-id` so we have to add 1 to the Pod's index\n          echo server-id=$((1 + $ordinal)) &gt;&gt; \/mnt\/conf\/server-id.cnf\n          if [[ $ordinal -eq 0 ]]; then\n            printf \"[mysqld]\\nlog-bin\" &gt; \/mnt\/conf\/primary.cnf\n          else\n            printf \"[mysqld]\\nsuper-read-only\" \/mnt\/conf\/replica.cnf\n          fi          <\/strong><strong class=\"co4\">\n        volumeMounts<\/strong>:<strong class=\"co3\">\n        - name<\/strong><strong class=\"sy2\">: <\/strong>config<strong class=\"co3\">\n          mountPath<\/strong><strong class=\"sy2\">: <\/strong>\/mnt\/conf<strong class=\"co4\">\n      containers<\/strong>:<strong class=\"co3\">\n      - name<\/strong><strong class=\"sy2\">: <\/strong>mysql<strong class=\"co3\">\n        image<\/strong><strong class=\"sy2\">: <\/strong>mysql:8.0<strong class=\"co4\">\n        env<\/strong>:<strong class=\"co3\">\n        - name<\/strong><strong class=\"sy2\">: <\/strong>MYSQL_ALLOW_EMPTY_PASSWORD<strong class=\"co3\">\n          value<\/strong><strong class=\"sy2\">: <\/strong><strong class=\"st0\">\"1\"<\/strong><strong class=\"co4\">\n        ports<\/strong>:<strong class=\"co3\">\n        - name<\/strong><strong class=\"sy2\">: <\/strong>mysql<strong class=\"co3\">\n          containerPort<\/strong><strong class=\"sy2\">: <\/strong>3306<strong class=\"co4\">\n        volumeMounts<\/strong>:<strong class=\"co3\">\n        - name<\/strong><strong class=\"sy2\">: <\/strong>config<strong class=\"co3\">\n          mountPath<\/strong><strong class=\"sy2\">: <\/strong>\/etc\/mysql\/conf.d<strong class=\"co3\">\n        - name<\/strong><strong class=\"sy2\">: <\/strong>data<strong class=\"co3\">\n          mountPath<\/strong><strong class=\"sy2\">: <\/strong>\/var\/lib\/mysql<strong class=\"co3\">\n          subPath<\/strong><strong class=\"sy2\">: <\/strong>mysql<strong class=\"co4\">\n        livenessProbe<\/strong>:<strong class=\"co4\">\n          exec<\/strong>:<strong class=\"co3\">\n            command<\/strong><strong class=\"sy2\">: <\/strong><strong class=\"br0\">[<\/strong><strong class=\"st0\">\"mysqladmin\"<\/strong>, <strong class=\"st0\">\"ping\"<\/strong><strong class=\"br0\">]<\/strong><strong class=\"co3\">\n          initialDelaySeconds<\/strong><strong class=\"sy2\">: <\/strong>30<strong class=\"co3\">\n          periodSeconds<\/strong><strong class=\"sy2\">: <\/strong>5<strong class=\"co3\">\n          timeoutSeconds<\/strong><strong class=\"sy2\">: <\/strong>5<strong class=\"co4\">\n        readinessProbe<\/strong>:<strong class=\"co4\">\n          exec<\/strong>:<strong class=\"co3\">\n            command<\/strong><strong class=\"sy2\">: <\/strong><strong class=\"br0\">[<\/strong><strong class=\"st0\">\"mysql\"<\/strong>, <strong class=\"st0\">\"-h\"<\/strong>, <strong class=\"st0\">\"127.0.0.1\"<\/strong>, <strong class=\"st0\">\"-e\"<\/strong>, <strong class=\"st0\">\"SELECT 1\"<\/strong><strong class=\"br0\">]<\/strong><strong class=\"co3\">\n          initialDelaySeconds<\/strong><strong class=\"sy2\">: <\/strong>5<strong class=\"co3\">\n          periodSeconds<\/strong><strong class=\"sy2\">: <\/strong>5<strong class=\"co3\">\n          timeoutSeconds<\/strong><strong class=\"sy2\">: <\/strong>1<strong class=\"co4\">\n      volumes<\/strong>:<strong class=\"co3\">\n      - name<\/strong><strong class=\"sy2\">: <\/strong>config<strong class=\"co3\">\n        emptyDir<\/strong><strong class=\"sy2\">: <\/strong><strong class=\"br0\">{<\/strong><strong class=\"br0\">}<\/strong><strong class=\"co4\">\n  volumeClaimTemplates<\/strong>:<strong class=\"co4\">\n  - metadata<\/strong>:<strong class=\"co3\">\n      name<\/strong><strong class=\"sy2\">: <\/strong>data<strong class=\"co4\">\n    spec<\/strong>:<strong class=\"co3\">\n      accessModes<\/strong><strong class=\"sy2\">: <\/strong><strong class=\"br0\">[<\/strong><strong class=\"st0\">\"ReadWriteOnce\"<\/strong><strong class=\"br0\">]<\/strong><strong class=\"co4\">\n      resources<\/strong>:<strong class=\"co4\">\n        requests<\/strong>:<strong class=\"co3\">\n          storage<\/strong><strong class=\"sy2\">: <\/strong>1Gi<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This is quite a long manifest so lets unpack what happens.<\/p>\n<ul>\n<li>A <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/services-networking\/service\/#headless-services\">headless service<\/a> is created by setting its <code>clusterIP<\/code> to <code>None<\/code>. This is tied to the StatefulSet and provides the network identities for its Pods.<\/li>\n<li>A StatefulSet is created to hold the MySQL Pods. The <code>replicas<\/code> field specifies that three Pods will run. The headless service is referenced by the <code>serviceName<\/code> field.<\/li>\n<li>Within the StatefulSet, an init container is created that pre-populates a file inside a config directory mounted using a persistent volume. The container runs a Bash script that establishes the ordinal index of the running Pod. When the index is 0, the Pod is the first to be created within the StatefulSet so it becomes the MySQL primary node. The other Pods are configured as replicas. The appropriate config file gets written into the volume where it\u2019ll be accessible to the MySQL container later on.<\/li>\n<li>The MySQL container is created with the config volume mounted to the correct MySQL directory. This ensures the MySQL instance gets configured as either the primary or a replica, depending on whether it\u2019s the first Pod to start in the StatefulSet.<\/li>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/tasks\/configure-pod-container\/configure-liveness-readiness-startup-probes\">Liveness and readiness probes<\/a> are used to detect when the MySQL instance is ready. This prevents successive Pods in the StatefulSet from starting until the previous one is Running, ensuring MySQL replicas don\u2019t exist before the primary node is up.<\/li>\n<\/ul>\n<p>An ordinary Deployment or ReplicaSet could not implement this workflow. Once your Pods have started, you can scale the StatefulSet up or down without risking the destruction of the MySQL primary node. Kubernetes provides a guarantee that the established Pod order will be respected.<\/p>\n<pre># Create the MySQL StatefulSet&#13;\n$ kubectl apply -f mysql-statefulset.yaml&#13;\n&#13;\n# Scale up to 5 Pods - a MySQL primary and 4 MySQL replicas&#13;\n$ kubectl scale statefulset mysql --replicas=5<\/pre>\n<h2 id=\"rolling-updates\"><span class=\"ez-toc-section\" id=\"Rolling_Updates\"><\/span>Rolling Updates<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>StatefulSets implement rolling updates when you change their specification. The StatefulSet controller will replace each Pod in sequential reverse order, using the persistently assigned ordinal indexes. <code>mysql-3<\/code> will be deleted and replaced first, followed by <code>mysql-2<\/code> and <code>mysql-1<\/code>. <code>mysql-2<\/code> won\u2019t get updated until the new <code>mysql-3<\/code> Pod transitions to the <code>Running<\/code> state.<\/p>\n<p>The rolling update mechanism includes support for staged deployments too. Setting the <code>.spec.updateStrategy.rollingUpdate.partition<\/code> field in your StatefulSet\u2019s manifest instructs Kubernetes to only update the Pods with an ordinal index greater than or equal to the given partition.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yaml\">\n<pre class=\"de1\"><strong class=\"co3\">apiVersion<\/strong><strong class=\"sy2\">: <\/strong>apps\/v1<strong class=\"co3\">\nkind<\/strong><strong class=\"sy2\">: <\/strong>StatefulSet<strong class=\"co4\">\nmetadata<\/strong>:<strong class=\"co3\">\n  name<\/strong><strong class=\"sy2\">: <\/strong>mysql<strong class=\"co4\">\nspec<\/strong>:<strong class=\"co4\">\n  selector<\/strong>:<strong class=\"co4\">\n    matchLabels<\/strong>:<strong class=\"co3\">\n      app<\/strong><strong class=\"sy2\">: <\/strong>mysql<strong class=\"co3\">\n  serviceName<\/strong><strong class=\"sy2\">: <\/strong>mysql<strong class=\"co3\">\n  replicas<\/strong><strong class=\"sy2\">: <\/strong>3<strong class=\"co4\">\n  updateStrategy<\/strong>:<strong class=\"co4\">\n    rollingUpdate<\/strong>:<strong class=\"co3\">\n      partition<\/strong><strong class=\"sy2\">: <\/strong>1<strong class=\"co4\">\n  template<\/strong><strong class=\"sy2\">:\n<\/strong>    <strong class=\"sy1\">...<\/strong><strong class=\"co4\">\n  volumeClaimTemplates<\/strong><strong class=\"sy2\">:\n<\/strong>    <strong class=\"sy1\">...<\/strong><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>In this example only Pods indexed <code>1<\/code> or higher will be targeted by update operations. The first Pod in the StatefulSet won\u2019t receive a new specification until the partition is lowered or removed.<\/p>\n<h2 id=\"limitations\"><span class=\"ez-toc-section\" id=\"Limitations\"><\/span>Limitations<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>StatefulSets have <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/controllers\/statefulset\/#limitations\">some limitations<\/a> you should be aware of before you adopt them. These common gotchas can <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/trip-and-travel\/\" data-internallinksmanager029f6b8e52c=\"10\" title=\"Trip &amp; Travel\" target=\"_blank\" rel=\"noopener\">trip<\/a> you up when you start deploying stateful applications.<\/p>\n<ul>\n<li>Deleting a StatefulSet does not guarantee the Pods will be terminated in the order indicated by their identities.<\/li>\n<li>Deleting a StatefulSet or scaling down its replica count will not delete any associated volumes. This guards against accidental data loss.<\/li>\n<li>Using rolling updates <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/controllers\/statefulset\/#forced-rollback\">can create a situation<\/a> where an invalid broken state occurs. This happens when you supply a configuration that never transitions to the Running or Ready state because of a problem with your application. Reverting to a good configuration won\u2019t fix the problem because Kubernetes waits indefinitely for the bad Pod to become Ready. You have to manually resolve the situation by deleting the pending or failed Pods.<\/li>\n<\/ul>\n<p>StatefulSets also <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.plural.sh\/blog\/kubernetes-statefulsets-are-broken\">omit a mechanism<\/a> for resizing the volumes linked to each Pod. You have to manually edit each persistent volume and its corresponding persistent volume claim, then delete the StatefulSet and orphan its Pods. Creating a new StatefulSet with the revised specification will allow Kubernetes to reclaim the orphaned Pods and resize the volumes.<\/p>\n<h2 id=\"when-not-to-use-a-statefulset\"><span class=\"ez-toc-section\" id=\"When_Not_To_Use_a_StatefulSet\"><\/span>When Not To Use a StatefulSet<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You should only use a StatefulSet when individual replicas have their own state. A StatefulSet isn\u2019t necessary when all the replicas share the same state, even if it\u2019s persistent.<\/p>\n<p>In these situations you can use a regular ReplicaSet or Deployment to launch your Pods. Any mounted volumes will be shared across all of the Pods which is the expected behavior for stateless systems.<\/p>\n<p>A StatefulSet doesn\u2019t add value unless you need individual persistent storage or sticky replica identifiers. Using a StatefulSet incorrectly can cause confusion by suggesting Pods are stateful when they\u2019re actually running a stateless workload.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>StatefulSets provide persistent identities for replicated Kubernetes Pods. Each Pod is named with an ordinal index that\u2019s allocated sequentially. When the Pod gets rescheduled, its replacement inherits its identity. The StatefulSet also ensures that Pods get terminated in the reverse order they were created in.<\/p>\n<p>StatefulSets allow Kubernetes to accommodate applications that require graceful rolling deployments, stable network identifiers, and reliable access to persistent storage. They\u2019re suitable for any situation where the replicas in a set of Pods have their own state that needs to be preserved.<\/p>\n<p>A StatefulSet doesn\u2019t need to be used if your replicas are stateless, even if they\u2019re storing some persistent data. Deployments and ReplicaSets are more suitable when individual replicas don\u2019t need to be identified or scaled in a consistent order.<\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/devops\/what-are-kubernetes-statefulsets-when-should-you-use-them\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;What Are Kubernetes StatefulSets? When Should You Use Them?&#8221; StatefulSets are Kubernetes objects used to consistently deploy stateful application components. Pods created as part of a StatefulSet are given persistent identifiers that they retain even when they\u2019re rescheduled. A StatefulSet can deploy applications that need to reliably identify specific replicas, rollout updates in a pre-defined&#8230;<\/p>\n","protected":false},"author":1,"featured_media":498319,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/Kubernetes-New.jpg?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-498318","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/498318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=498318"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/498318\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/498319"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=498318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=498318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=498318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}