{"id":6251,"date":"2019-07-26T02:47:32","date_gmt":"2019-07-26T02:47:32","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=6251"},"modified":"2021-11-16T11:15:23","modified_gmt":"2021-11-16T11:15:23","slug":"kubernetes-volume-hostpath-explained-with-examples","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/kubernetes-volume-hostpath-explained-with-examples\/","title":{"rendered":"Kubernetes volume hostPath 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.jpg\" alt=\"\" class=\"wp-image-6255\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/07\/emptydir-hostpath-kubernetes-volume.jpg 632w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/07\/emptydir-hostpath-kubernetes-volume-300x176.jpg 300w\" sizes=\"auto, (max-width: 632px) 100vw, 632px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>The hostPath volume mounts a resource from the host node filesystem. the resources could be directory, file socket, character, or block device. These resources mu<\/li><li>A hostPath volume mounts a file or directory from the host node&#8217;s filesystem into your pod. <\/li><li>A hostPath PersistentVolume must be used only in a single-node cluster. Kubernetes does not support hostPath on a multi-node cluster currently.<\/li><li>The directories created on the underlying hosts are only writable by root. You either need to run your process as root in a privileged container or modify the file permissions on the host to be able to write to a  hostPath volume<\/li><\/ul>\n\n\n\n<p><strong>Uses for a hostPath are:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>This is not something that most Pods will need, but it offers a powerful escape hatch for some applications.<\/li><li>Deploying some Node Specific files through pod<\/li><li>Running a container that needs access to Docker internals; use a hostPath of \/var\/lib\/docker<\/li><li>Allowing a Pod to specify whether a given hostPath should exist prior to the Pod running, whether it should be created, and what it should exist as<\/li><\/ul>\n\n\n\n<p><strong>Example<\/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\">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: \/test-pd\n      name: test-volume\n  volumes:\n  - name: test-volume\n    hostPath:\n      <span class=\"hljs-comment\"># directory location on host<\/span>\n      path: \/data\n      <span class=\"hljs-comment\"># this field is optional<\/span>\n      type: DirectoryOrCreate\n\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\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/07\/kubernetes-hostPath-volume.jpg\" alt=\"\" class=\"wp-image-6254\" width=\"403\" height=\"465\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/07\/kubernetes-hostPath-volume.jpg 261w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/07\/kubernetes-hostPath-volume-260x300.jpg 260w\" sizes=\"auto, (max-width: 403px) 100vw, 403px\" \/><\/figure>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_38696\"><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_19879\"  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_38696\"  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>The hostPath volume mounts a resource from the host node filesystem. the resources could be directory, file socket, character, or block device. These resources mu A hostPath volume mounts a file or directory from the host node&#8217;s filesystem into your pod. A hostPath PersistentVolume must be used only in a single-node cluster. Kubernetes does not&#8230;<\/p>\n","protected":false},"author":1,"featured_media":7700,"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,519,5524,4860,5525],"class_list":["post-6251","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kubernetes","tag-devopsschool","tag-example","tag-hostpath","tag-kubernetes","tag-volume"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6251","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=6251"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6251\/revisions"}],"predecessor-version":[{"id":25489,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6251\/revisions\/25489"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/7700"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=6251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=6251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=6251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}