{"id":6247,"date":"2019-07-25T19:50:31","date_gmt":"2019-07-25T19:50:31","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=6247"},"modified":"2021-11-16T11:15:43","modified_gmt":"2021-11-16T11:15:43","slug":"kubernetes-volume-emptydir-explained-with-examples","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/kubernetes-volume-emptydir-explained-with-examples\/","title":{"rendered":"Kubernetes volume emptyDir explained with examples"},"content":{"rendered":"\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"632\" height=\"371\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/07\/emptydir-hostpath-kubernetes-volume-1.jpg\" alt=\"\" class=\"wp-image-6257\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/07\/emptydir-hostpath-kubernetes-volume-1.jpg 632w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/07\/emptydir-hostpath-kubernetes-volume-1-300x176.jpg 300w\" sizes=\"auto, (max-width: 632px) 100vw, 632px\" \/><\/figure>\n\n\n\n<p><strong>Here are the following facts for emptyDir storage type in Kubernetes<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>An emptyDir volume is first created when a Pod is assigned to a Node and initially its empty<\/li><li>A Volume of type emptyDir that lasts for the life of the Pod, even if the Container terminates and restarts.<\/li><li>If a container in a Pod crashes the emptyDir content is unaffected.<\/li><li>All containers in a Pod share use of the emptyDir volume .<\/li><li>Each container can independently mount the emptyDir at the same \/ or different path.<\/li><li>Using emptyDir, The Kubelet will create the directory in the container, but not mount any storage. <\/li><li>Containers in the Pod can all read\/write the same files in the emptyDir volume, though that volume can be mounted at the same or different paths in each Container.<\/li><li>When a Pod is removed from a node for any reason, the data in the emptyDir is deleted forever along with the container.<\/li><li>A Container crashing does NOT remove a Pod from a node, so the data in an emptyDir volume is safe across Container crashes.<\/li><li>By default, emptyDir volumes are stored on whatever medium is backing the node &#8211; that might be disk or SSD or network storage.<\/li><li>You can set the emptyDir.medium field to &#8220;Memory&#8221; to tell Kubernetes to mount a tmpfs (RAM-backed filesystem) for you instead.<\/li><li>The location should of emptyDir should be in \/var\/lib\/kubelet\/pods\/{podid}\/volumes\/kubernetes.io~empty-dir\/ on the given node where your pod is running.<\/li><\/ul>\n\n\n\n<p><strong>Some uses for an&nbsp;<\/strong><code><strong>emptyDir<\/strong><\/code><strong>&nbsp;are:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>scratch space, such as for a disk-based merge sort<\/li><li>checkpointing a long computation for recovery from crashes<\/li><li>holding files that a content-manager container fetches while a webserver container serves the data<\/li><\/ul>\n\n\n\n<p><strong>Example of emptyDir &#8211; 1<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">apiVersion: v1\nkind: Pod\nmetadata:\n  name: test-pd\nspec:\n  containers:\n  - image: k8s.gcr.io\/test-webserver\n    name: test-container\n    volumeMounts:\n    - mountPath: \/cache\n      name: cache-volume\n  volumes:\n  - name: cache-volume\n    emptyDir: {}<\/code><\/span><\/pre>\n\n\n<p><strong>Example of emptyDir &#8211; 2<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"359\" height=\"336\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/07\/kubernetes-emptydir-volume.jpg\" alt=\"\" class=\"wp-image-6248\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/07\/kubernetes-emptydir-volume.jpg 359w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/07\/kubernetes-emptydir-volume-300x281.jpg 300w\" sizes=\"auto, (max-width: 359px) 100vw, 359px\" \/><\/figure>\n\n\n\n<p><strong>Example of emptyDir &#8211; 3<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">nano myVolumes-Pod.yaml\n\napiVersion: v1\nkind: Pod\nmetadata:\n  name: myvolumes-pod\nspec:\n  containers:\n  - image: alpine\n    imagePullPolicy: IfNotPresent\n    name: myvolumes-container<span class=\"hljs-number\">-1<\/span>\n    \n    command: &#91;<span class=\"hljs-string\">'sh'<\/span>, <span class=\"hljs-string\">'-c'<\/span>, <span class=\"hljs-string\">'echo The Bench Container 1 is Running ; sleep 3600'<\/span>]\n    \n    volumeMounts:\n    - mountPath: \/demo1\n      name: demo-volume\n\n  - image: alpine\n    imagePullPolicy: IfNotPresent\n    name: myvolumes-container<span class=\"hljs-number\">-2<\/span>\n    \n    command: &#91;<span class=\"hljs-string\">'sh'<\/span>, <span class=\"hljs-string\">'-c'<\/span>, <span class=\"hljs-string\">'echo The Bench Container 2 is Running ; sleep 3600'<\/span>]\n    \n    volumeMounts:\n    - mountPath: \/demo2\n      name: demo-volume\n\n  - image: alpine\n    imagePullPolicy: IfNotPresent\n    name: myvolumes-container<span class=\"hljs-number\">-3<\/span>\n    \n    command: &#91;<span class=\"hljs-string\">'sh'<\/span>, <span class=\"hljs-string\">'-c'<\/span>, <span class=\"hljs-string\">'echo The Bench Container 3 is Running ; sleep 3600'<\/span>]\n    \n    volumeMounts:\n    - mountPath: \/demo3\n      name: demo-volume\n\n  volumes:\n  - name: demo-volume\n    emptyDir: {}\n    <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_14518\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_52842\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_14518\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Here are the following facts for emptyDir storage type in Kubernetes An emptyDir volume is first created when a Pod is assigned to a Node and initially its empty A Volume of type emptyDir that lasts for the life of the Pod, even if the Container terminates and restarts. If a container in a Pod&#8230;<\/p>\n","protected":false},"author":1,"featured_media":7717,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[4859],"tags":[5449,5527,519,4860,5525],"class_list":["post-6247","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kubernetes","tag-devopsschool","tag-emptydir","tag-example","tag-kubernetes","tag-volume"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=6247"}],"version-history":[{"count":5,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6247\/revisions"}],"predecessor-version":[{"id":25490,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6247\/revisions\/25490"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/7717"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=6247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=6247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=6247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}