{"id":500158,"date":"2022-10-12T03:48:36","date_gmt":"2022-10-12T00:48:36","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/should-you-run-stateful-applications-in-kubernetes\/"},"modified":"2022-10-12T03:48:36","modified_gmt":"2022-10-12T00:48:36","slug":"should-you-run-stateful-applications-in-kubernetes","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/should-you-run-stateful-applications-in-kubernetes\/","title":{"rendered":"#Should You Run Stateful Applications In Kubernetes?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 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-6a2d1e3799991\" 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-6a2d1e3799991\" 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\/should-you-run-stateful-applications-in-kubernetes\/#%E2%80%9CShould_You_Run_Stateful_Applications_In_Kubernetes%E2%80%9D\" >&#8220;Should You Run Stateful Applications In Kubernetes?&#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\/should-you-run-stateful-applications-in-kubernetes\/#The_Problems_With_State\" >The Problems With State<\/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\/should-you-run-stateful-applications-in-kubernetes\/#Running_Stateful_Services_In_Kubernetes\" >Running Stateful Services In Kubernetes<\/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\/should-you-run-stateful-applications-in-kubernetes\/#Managing_State_Outside_of_Kubernetes\" >Managing State Outside of Kubernetes<\/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\/should-you-run-stateful-applications-in-kubernetes\/#Avoiding_Kubernetes_for_Stateful_Services\" >Avoiding Kubernetes for Stateful Services<\/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\/should-you-run-stateful-applications-in-kubernetes\/#Should_You_Run_Stateful_Apps_In_Kubernetes\" >Should You Run Stateful Apps In Kubernetes?<\/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\/should-you-run-stateful-applications-in-kubernetes\/#Summary\" >Summary<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CShould_You_Run_Stateful_Applications_In_Kubernetes%E2%80%9D\"><\/span>&#8220;Should You Run Stateful Applications In Kubernetes?&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<figure style=\"width: 1200px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage size-full wp-image-821780\" data-pagespeed-no-defer=\"\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/07\/shutterstock_1858584427.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Graphic showing the Kubernetes logo overlaid on colorful hexagonal shapes\" width=\"1200\" height=\"675\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\">Shutterstock.com\/o_m<\/span><\/figcaption><\/figure>\n<p>Kubernetes is often <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>roached from the perspective of stateless systems. A stateless application is easy to containerize, distribute, and scale because it doesn\u2019t need to store any data outside its environment. It doesn\u2019t matter if the container\u2019s stopped or moved to a different host \u2013 new instances can replace older ones without any repercussions.<\/p>\n<p>Most real applications aren\u2019t like this though. All but the simplest systems possess state that\u2019s usually stored in a database or a persistent filesystem. Data that configures your service or is created by users must be retained and made accessible to all your containers, irrespective of where they\u2019re located.<\/p>\n<p>The challenge of maintaining state across transient environments is encountered by most organizations using containers, orchestration, and cloud native working practices. Stateful workloads can be accommodated by Kubernetes but external alternatives exist too. In this article, you\u2019ll learn some of the approaches that make Kubernetes work with stateful apps.<\/p>\n<h2 id=\"the-problems-with-state\"><span class=\"ez-toc-section\" id=\"The_Problems_With_State\"><\/span>The Problems With State<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The term \u201cstate\u201d describes the data associated with an application at a particular point in time. It is long-lived information such as database content and user accounts that will need to be retrieved throughout the system\u2019s lifetime. The state continually changes as data is created and modified while your service is in use.<\/p>\n<p>Correct application functioning is dependent on each instance being able to access the persistent state. If you distribute four replicas of a component across two physical hosts, both of those machines will need access to your data store. This means the application instances have interlinked dependencies that can\u2019t be automatically replaced.<\/p>\n<p>The constraints around stateful services conflict with the Kubernetes model of ephemeral containers that can be replaced at any time. When you\u2019re working with a stateful application, you need to make special provision so containers can reliably access the state they need. This requires additional configuration to provide reliable data persistence that remains stable as your application scales.<\/p>\n<h2 id=\"running-stateful-services-in-kubernetes\"><span class=\"ez-toc-section\" id=\"Running_Stateful_Services_In_Kubernetes\"><\/span>Running Stateful Services In Kubernetes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Kubernetes support for stateful systems <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/blog\/2016\/07\/stateful-applications-in-containers-kubernetes\">has grown over<\/a> the past few years, supported by an increase in community interest. Stateful applications can be assembled from officially supported resources such as <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/tutorials\/stateful-application\/basic-stateful-set\">stateful sets<\/a> and persistent volumes. These offer integrated methods for storing and managing your data.<\/p>\n<p>Persistent volumes provide data storage to your Pods. Files written to a persistent volume are stored independently of the Pod that creates them. The volume\u2019s content persists in your cluster after the Pods are destroyed, allowing their replacements to access the stored state.<\/p>\n<p>StatefulSets are API objects that represent stateful applications. They function similarly to Deployments but assign a unique identifier to each Pod they encapsulate. Pods retain their identifiers even if they\u2019re restarted or scheduled onto another Node. This allows you to implement procedures where Pod ordering and identity is important. The reliable identifiers let you rematch volumes to Pods after a scheduling event and gracefully rollout application updates in sequence.<\/p>\n<p>These features mean it\u2019s now possible to run stateful applications inside your Kubernetes cluster. You can write data to persistent volumes and use StatefulSets instead of Deployments when Pods need to remember their identities.<\/p>\n<h2 id=\"managing-state-outside-of-kubernetes\"><span class=\"ez-toc-section\" id=\"Managing_State_Outside_of_Kubernetes\"><\/span>Managing State Outside of Kubernetes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A popular route for running stateful services in Kubernetes is to locate the state <em>outside<\/em> your cluster. You architect your system so that its components are decoupled from the storage they require. They can access persistent data in separate services over the network.<\/p>\n<p>You can maintain your own database server, connect to existing network file shares, or use a fully managed service from your cloud provider. The applications in your Kubernetes cluster should be configured to interact with your storage systems using their APIs or direct access protocols.<\/p>\n<p>This is a good way of promoting decoupling in your services. Removing persistent filesystem access from your containerized applications makes them more portable across environments. Containers can be launched using stateless deployment models as their storage dependencies are reduced to basic network calls. You can benefit from the flexibility of Kubernetes without incurring the complexity cost of using persistent volumes and stateful sets to store state in your cluster.<\/p>\n<h2 id=\"avoiding-kubernetes-for-stateful-services\"><span class=\"ez-toc-section\" id=\"Avoiding_Kubernetes_for_Stateful_Services\"><\/span>Avoiding Kubernetes for Stateful Services<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A third school of thought is to avoid Kubernetes altogether for stateful services. This is usually an over-reaction \u2013 if you\u2019re not comfortable maintaining state in your cluster, you can still use the method outlined above to deploy your applications using an adjacent storage provider.<\/p>\n<p>Nonetheless there are still some systems which might not make sense in Kubernetes. Extremely filesystem-dependent architectures which work with large numbers of files could be challenging to implement and scale using persistent volumes. An external storage system managed alongside Kubernetes might add unacceptable latency when file interactions are the core function of your service.<\/p>\n<p>In these circumstances you may have to reach for alternative deployment approaches that give you more control of data storage and I\/O operations. However work is ongoing in the ecosystem to enhance the storage options for containerized systems. <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/thenewstack.io\/the-most-popular-cloud-native-storage-solutions\">Cloud native storage solutions<\/a> are emerging as higher-level abstractions of concepts like persistent volumes and stateful sets, implementing distributed filesystems that remain performant when used across multiple nodes. <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.redhat.com\/en\/technologies\/storage\/ceph\">Ceph<\/a>, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/min.io\">Minio<\/a>, and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/portworx.com\">Portworx<\/a> are some of the contenders in this space.<\/p>\n<h2 id=\"should-you-run-stateful-apps-in-kubernetes\"><span class=\"ez-toc-section\" id=\"Should_You_Run_Stateful_Apps_In_Kubernetes\"><\/span>Should You Run Stateful Apps In Kubernetes?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Most stateful applications can be deployed without issues using Kubernetes. The principal decision is whether you keep your persistent data inside your cluster, by using persistent volumes and stateful sets, or interface with an externally managed data store.<\/p>\n<p>Persistent volumes work for most use cases but they do come with some limitations. Not all volume <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/storage\/persistent-volumes\/#access-modes\">access modes<\/a> are supported by every implementation so it\u2019s important to check which features your Kubernetes distribution supports.<\/p>\n<p>Relatively few drivers offer the <code>ReadWriteMany<\/code> mode which permits the volume to be bound to several Nodes simultaneously, with each of them able to read and write data. The <code>ReadWriteOnce<\/code> mode is the most broadly supported, allowing each Node to read data but only one of them to write. These constraints can affect your application\u2019s scheduling \u2013 a system with several Pods that need to write to a shared database instance will need to run them all on a single Node, unless <code>ReadWriteMany<\/code> is available. This limits your ability to scale your services.<\/p>\n<p>Utilizing an externally managed database or object storage system is an effective way to mitigate these lingering issues while still benefiting from the flexibility of Kubernetes. This does require your application to be fully decoupled from its storage so it might not be an option if you\u2019re migrating a legacy service.<\/p>\n<p>Working with older applications presents the strongest case for <em>not<\/em> running a stateful app in Kubernetes. You can run into roadblocks if you\u2019re unable to be intentional about where state is stored and how it\u2019s managed. In these situations it\u2019s usually best to refactor your system before you try to distribute it across deployment nodes.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Although stateful applications and Kubernetes aren\u2019t quite a natural match, it\u2019s possible to accommodate persistent data in your cluster by combining <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/tutorials\/stateful-application\/basic-stateful-set\">stateful sets<\/a> and persistent volumes. These provide officially supported methods for orchestrating stateful systems using Kubernetes but you need to remain mindful of the scheduling constraints they impose.<\/p>\n<p>Because in-cluster state management adds complexity, keeping persistent data in an external service is a popular way to streamline your deployments. Managed databases, object storage platforms, and private networks allow you to provision storage outside your cluster then securely consume it from within. You\u2019ll need to adapt your application to support external storage interfaces but can then benefit from increased deployment flexibility.<\/p>\n<p>Applications where the state consists of simple config files can utilize ConfigMaps to run in Kubernetes without having to adopt persistent file storage. ConfigMaps are first-class objects which are automatically provided to your Pods when they\u2019re needed, either as environment variables or mounted files. They remove the need for persistent volumes when you\u2019re only storing a handful of long-lived settings.<\/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\/should-you-run-stateful-applications-in-kubernetes\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;Should You Run Stateful Applications In Kubernetes?&#8221; Shutterstock.com\/o_m Kubernetes is often approached from the perspective of stateless systems. A stateless application is easy to containerize, distribute, and scale because it doesn\u2019t need to store any data outside its environment. It doesn\u2019t matter if the container\u2019s stopped or moved to a different host \u2013 new instances&#8230;<\/p>\n","protected":false},"author":1,"featured_media":500159,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/07\/shutterstock_1858584427.jpg?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-500158","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\/500158","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=500158"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/500158\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/500159"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=500158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=500158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=500158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}