{"id":217605,"date":"2021-04-02T16:00:50","date_gmt":"2021-04-02T13:00:50","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-add-persistent-storage-to-kubernetes-pods-cloudsavvy-it\/"},"modified":"2021-04-02T16:00:50","modified_gmt":"2021-04-02T13:00:50","slug":"how-to-add-persistent-storage-to-kubernetes-pods-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-add-persistent-storage-to-kubernetes-pods-cloudsavvy-it\/","title":{"rendered":"#How to Add Persistent Storage to Kubernetes Pods \u2013 CloudSavvy IT"},"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-6a2e7506e4251\" 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-6a2e7506e4251\" 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-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-add-persistent-storage-to-kubernetes-pods-cloudsavvy-it\/#A_Basic_Example\" >A Basic Example<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-add-persistent-storage-to-kubernetes-pods-cloudsavvy-it\/#Create_a_Persistent_Volume\" >Create a Persistent Volume<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-add-persistent-storage-to-kubernetes-pods-cloudsavvy-it\/#Create_a_Persistent_Volume_Claim\" >Create a Persistent Volume Claim<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-add-persistent-storage-to-kubernetes-pods-cloudsavvy-it\/#Add_a_Pod\" >Add a Pod<\/a><\/li><\/ul><\/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\/how-to-add-persistent-storage-to-kubernetes-pods-cloudsavvy-it\/#Storage_Classes\" >Storage Classes<\/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\/how-to-add-persistent-storage-to-kubernetes-pods-cloudsavvy-it\/#Access_Modes\" >Access Modes<\/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\/how-to-add-persistent-storage-to-kubernetes-pods-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Add Persistent Storage to Kubernetes Pods \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9632\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/02\/748108a6.jpg?width=1200&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Graphic showing the Kubernetes logo\" width=\"1602\" height=\"902\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Kubernetes Pod filesystems are ephemeral by default. This is in keeping with the stateless nature of containers. Persistent data should be stored outside the container, even when it looks like it\u2019s within the container\u2019s filesystem. Here\u2019s how to provision persistent storage in Kubernetes.<\/p>\n<p>The basic unit of Kubernetes persistent storage is a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/storage\/persistent-volumes\">Persistent Volume<\/a>. This is an abstraction over the more fundamental <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/storage\/volumes\">Volume<\/a>.<\/p>\n<p>Persistent Volumes exist independently of any specific Pod. Similarly to plain Docker volumes, Kubernetes\u2019 Persistent Volumes can remain in your cluster even when there\u2019s no Pods using them.<\/p>\n<p>Pods are given access to Persistent Volumes by making a <em>Persistent Volume Claim<\/em>. This is another resource type which represents a Pod\u2019s request to use persistent storage. The claim handles the provisioning of Persistent Volumes that satisfy the request.<\/p>\n<h2 id=\"a-basic-example\"><span class=\"ez-toc-section\" id=\"A_Basic_Example\"><\/span>A Basic Example<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Let\u2019s look at how to create a persistent storage system by manually setting up a Persistent Volume and Persistent Volume Claim. Each resource will go into its own manifest file. You can <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>ly these files to your cluster with <code>kubectl apply<\/code>.<\/p>\n<h3 id=\"create-a-persistent-volume\"><span class=\"ez-toc-section\" id=\"Create_a_Persistent_Volume\"><\/span>Create a Persistent Volume<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Begin by creating your volume:<\/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\"><span class=\"co3\">apiVersion<\/span><span class=\"sy2\">: <\/span>v1<span class=\"co3\">\nkind<\/span><span class=\"sy2\">: <\/span>PersistentVolume<span class=\"co4\">\nmetadata<\/span>:<span class=\"co3\">\n  name<\/span><span class=\"sy2\">: <\/span>my-volume<span class=\"co3\">\n  namespace<\/span><span class=\"sy2\">: <\/span>pvc-demo<span class=\"co4\">\nspec<\/span>:<span class=\"co3\">\n  storageClassName<\/span><span class=\"sy2\">: <\/span>manual<span class=\"co4\">\n  capacity<\/span>:<span class=\"co3\">\n    storage<\/span><span class=\"sy2\">: <\/span>2Gi<span class=\"co4\">\n  accessModes<\/span><span class=\"sy2\">:\n<\/span>    - ReadWriteOnce<span class=\"co4\">\n  hostPath<\/span>:<span class=\"co3\">\n    path<\/span><span class=\"sy2\">: <\/span>\/mnt\/data<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This definition creates a volume called <code>my-volume<\/code>. It has a capacity of 2Gi and will be stored at <code>\/mnt\/data<\/code> on the host Node. Because we\u2019re creating this volume manually, the <code>storageClassName<\/code> is set to <code>manual<\/code>. Storage classes can be used to mandate that volumes are only bound to volume claims requesting the same class.<\/p>\n<h3 id=\"create-a-persistent-volume-claim\"><span class=\"ez-toc-section\" id=\"Create_a_Persistent_Volume_Claim\"><\/span>Create a Persistent Volume Claim<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>You can now configure a Persistent Volume Claim:<\/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\"><span class=\"co3\">apiVersion<\/span><span class=\"sy2\">: <\/span>v1<span class=\"co3\">\nkind<\/span><span class=\"sy2\">: <\/span>PersistentVolumeClaim<span class=\"co4\">\nmetadata<\/span>:<span class=\"co3\">\n  name<\/span><span class=\"sy2\">: <\/span>my-volume-claim<span class=\"co3\">\n  namespace<\/span><span class=\"sy2\">: <\/span>pvc-demo<span class=\"co4\">\nspec<\/span>:<span class=\"co3\">\n  storageClassName<\/span><span class=\"sy2\">: <\/span>manual<span class=\"co4\">\n  accessModes<\/span><span class=\"sy2\">:\n<\/span>    - ReadWriteOnce<span class=\"co4\">\n  resources<\/span>:<span class=\"co4\">\n    requests<\/span>:<span class=\"co3\">\n      storage<\/span><span class=\"sy2\">: <\/span>1Gi<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The claim requests 1Gi of storage from a volume using the <code>manual<\/code> class. The volume we created earlier can fulfil these conditions. When the claim is created, Kubernetes should realise this and bind the claim to the volume.<\/p>\n<p>If you were to inspect the details of the volume and claim, you\u2019d see they both show a status of <code>Bound<\/code>.<\/p>\n<pre>kubectl get pv my-volume&#13;\nkubectl get pvc my-volume-claim<\/pre>\n<h3 id=\"add-a-pod\"><span class=\"ez-toc-section\" id=\"Add_a_Pod\"><\/span>Add a Pod<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The final stage is to use your volume claim to add persistent storage to a Pod.<\/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\"><span class=\"co3\">apiVersion<\/span><span class=\"sy2\">: <\/span>v1<span class=\"co3\">\nkind<\/span><span class=\"sy2\">: <\/span>Pod<span class=\"co4\">\nmetadata<\/span>:<span class=\"co3\">\n  name<\/span><span class=\"sy2\">: <\/span>my-pod<span class=\"co3\">\n  namespace<\/span><span class=\"sy2\">: <\/span>pvc-demo<span class=\"co4\">\nspec<\/span>:<span class=\"co4\">\n  containers<\/span>:<span class=\"co3\">\n    - name<\/span><span class=\"sy2\">: <\/span>my-container<span class=\"co3\">\n      image<\/span><span class=\"sy2\">: <\/span>my-image:latest<span class=\"co4\">\n      volumeMounts<\/span>:<span class=\"co3\">\n        - mountPath<\/span><span class=\"sy2\">: <\/span>\/path\/in\/container<span class=\"co3\">\n          name<\/span><span class=\"sy2\">: <\/span>my-pod-volume<span class=\"co4\">\n  volumes<\/span>:<span class=\"co3\">\n    - name<\/span><span class=\"sy2\">: <\/span>my-pod-volume<span class=\"co4\">\n      persistentVolumeClaim<\/span>:<span class=\"co3\">\n        claimName<\/span><span class=\"sy2\">: <\/span>my-volume-claim<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Within the <code>volumes<\/code> section, a reference to the Persistent Volume Claim is configured. You don\u2019t need to specify any other information about the volume. The Pod will use the claim, which will provide the volume it\u2019s bound to.<\/p>\n<p>The claim is referenced in <code>volumeMounts<\/code>. Make sure you use the same name in <code>volumes<\/code> and <code>volumeMounts<\/code>. The volume will be mounted into your Pod at the location specified by <code>mountPath<\/code>.<\/p>\n<p>Your Pod now has persistent storage available. Anything written to <code>\/path\/in\/container<\/code> will be stored to the Persistent Volume. The Persistent Volume Claim will be reused by new Pods that reference it, allowing data to outlive any individual Pod.<\/p>\n<h2 id=\"storage-classes\"><span class=\"ez-toc-section\" id=\"Storage_Classes\"><\/span>Storage Classes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>manual<\/code> storage class is used when you\u2019re creating your own volume and volume claim manifests. Different volume plugin drivers provide their own storage classes. Reference the storage class that represents the volume type you want to use.<\/p>\n<p>Managed Kubernetes services usually provide their own storage classes which map to the platform\u2019s block storage implementation. Examples include <code>gcePersistentDisk<\/code> with Google Kubernetes Engine, or <code>do-block-storage<\/code> with DigitalOcean Managed Kubernetes.<\/p>\n<p>In these scenarios, you don\u2019t need to create the <code>PersistentVolume<\/code> manifest manually. Create a <code>PersistentVolumeClaim<\/code> with the correct <code>storageClassName<\/code> and use the <code>resources.requests.storage<\/code> field (shown above) to specify the desired capacity. The storage driver will automatically bind the claim to a compatible volume instance.<\/p>\n<h2 id=\"access-modes\"><span class=\"ez-toc-section\" id=\"Access_Modes\"><\/span>Access Modes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There are three supported values for the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/storage\/persistent-volumes\/#access-modes\"><code>accessModes<\/code> field<\/a>:<\/p>\n<ul>\n<li><strong><code>ReadWriteOnce<\/code><\/strong> \u2013 The volume can only be mounted to a single Kubernetes node. That node will have full read-write access to the volume.<\/li>\n<li><strong><code>ReadOnlyMany<\/code><\/strong> \u2013 The volume can be consumed by multiple nodes simultaneously. Each node has read-only access (nothing can write to the volume).<\/li>\n<li><strong><code>ReadWriteMany<\/code><\/strong> \u2013 The volume can be mounted to multiple nodes simultaneously. Each node can read and write to the volume.<\/li>\n<\/ul>\n<p>Only one access mode can be used by a given volume at any time. That means two volume claims will only bind to the same volume if both claims declare the same access mode.<\/p>\n<p>The access mode of your volumes affects the Kubernetes scheduler\u2019s ability to span replicas of your Pods across multiple nodes. The <code>ReadOnlyMany<\/code>\/<code>ReadWriteMany<\/code> modes must be used if you need Pods to share persistent storage <em>and<\/em> be replicated over multiple nodes.<\/p>\n<p>Be aware that not all storage drivers support all access modes \u2013 you should check with your plugin\u2019s provider. A non-exhaustive list of volume plugins and compatible access modes is provided <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/storage\/persistent-volumes\/#access-modes\">in the Kubernetes documentation<\/a>.<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Persistent storage in Kubernetes isn\u2019t as daunting as it seems at first glance. Make sure Pods which need access to storage have volumes which are bound to a Persistent Volume Claim.<\/p>\n<p>When Persistent Volume Claims are used, Kubernetes will create Persistent Volumes which outlive individual Pods. When your Pods are replaced, the claimed volumes will be automatically mounted into the new Pods. Data will not be destroyed until the claim is deleted.\n<\/p><\/div>\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.cloudsavvyit.com\/10265\/how-to-add-persistent-storage-to-kubernetes-pods\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Add Persistent Storage to Kubernetes Pods \u2013 CloudSavvy IT&#8221; Kubernetes Pod filesystems are ephemeral by default. This is in keeping with the stateless nature of containers. Persistent data should be stored outside the container, even when it looks like it\u2019s within the container\u2019s filesystem. Here\u2019s how to provision persistent storage in Kubernetes. The&#8230;<\/p>\n","protected":false},"author":1,"featured_media":217606,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/02\/748108a6.jpg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-217605","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\/217605","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=217605"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/217605\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/217606"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=217605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=217605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=217605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}