{"id":373806,"date":"2021-11-29T15:00:20","date_gmt":"2021-11-29T12:00:20","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-copy-files-between-kubernetes-pods-and-your-machine-cloudsavvy-it\/"},"modified":"2021-11-29T15:00:20","modified_gmt":"2021-11-29T12:00:20","slug":"how-to-copy-files-between-kubernetes-pods-and-your-machine-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-copy-files-between-kubernetes-pods-and-your-machine-cloudsavvy-it\/","title":{"rendered":"#How to Copy Files Between Kubernetes Pods and Your Machine \u2013 CloudSavvy IT"},"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-6a3d8b0dac612\" 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-6a3d8b0dac612\" 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-copy-files-between-kubernetes-pods-and-your-machine-cloudsavvy-it\/#Copying_To_or_From_Your_Machine\" >Copying To or From Your Machine<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-copy-files-between-kubernetes-pods-and-your-machine-cloudsavvy-it\/#Command_Arguments\" >Command Arguments<\/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\/how-to-copy-files-between-kubernetes-pods-and-your-machine-cloudsavvy-it\/#When_Kubectl_Cp_Wont_Work\" >When Kubectl Cp Won\u2019t Work<\/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\/how-to-copy-files-between-kubernetes-pods-and-your-machine-cloudsavvy-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Copy Files Between Kubernetes Pods and Your Machine \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-9632\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/02\/748108a6.jpg?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/02\/748108a6.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/02\/748108a6.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Graphic showing the Kubernetes logo\" width=\"1602\" height=\"902\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Containers running in Kubernetes Pods are meant to be self-contained compute units that you don\u2019t need to manually interact with. Sometimes you might have to copy files to or from a Pod\u2019s filesystem though, perhaps because you\u2019re debugging a problem and want to archive logs, config files, or caches stored in a container.<\/p>\n<p>Here\u2019s how to move files between your machine and containers in a Pod, using Kubectl\u2019s built-in transfer feature or a manual alternative.<\/p>\n<h2 id=\"copying-to-or-from-your-machine\"><span class=\"ez-toc-section\" id=\"Copying_To_or_From_Your_Machine\"><\/span>Copying To or From Your Machine<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/v1-17.docs.kubernetes.io\/docs\/reference\/generated\/kubectl\/kubectl-commands#cp\"><code>kubectl cp<\/code> command<\/a> is the simplest way to copy to or from a container filesystem. It works similarly to the <code>docker cp<\/code> you may already be familiar with from local container development.<\/p>\n<p><code>kubectl cp<\/code> requires source and destination arguments. One of the source or destination must be a Pod reference. This is formatted as the Pod name, followed by a colon and the in-container filesystem path.<\/p>\n<pre>kubectl cp example-pod:\/tmp\/example-dir .\/example-dir<\/pre>\n<p>This example copies the contents of <code>\/tmp\/example-dir<\/code> from <code>example-pod<\/code> to <code>example-dir<\/code> on your local filesystem. Reversing the arguments would copy your machine\u2019s <code>example-dir<\/code> back to the Pod.<\/p>\n<h2 id=\"command-arguments\"><span class=\"ez-toc-section\" id=\"Command_Arguments\"><\/span>Command Arguments<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>As a single Pod can hold multiple containers, it\u2019s important to bear this in mind when using <code>cp<\/code>. Kubernetes will default to copying to or from the first container instance within the Pod. You can target a specific container instead by adding the <code>-c<\/code> flag:<\/p>\n<pre>kubectl cp example-pod:\/tmp\/example-dir .\/example-dir -c second-container<\/pre>\n<p>Like all other <code>kubectl<\/code> commands, <code>cp<\/code> runs against the cluster connection defined by your <code>KUBECONFIG<\/code> environment variable. This defaults to <code>~\/.kube\/conf<\/code>.<\/p>\n<p>There are two options when you\u2019re referencing namespaced Pods. You can either add the standard <code>--namespace example-namespace<\/code> flag or use a full Pod path in your filesystem reference:<\/p>\n<pre>kubectl cp example-namespace\/example-pod:\/tmp\/example-dir .\/example-dir<\/pre>\n<p>File attributes are preserved by default. Add the <code>--no-preserve<\/code> flag to your <code>cp<\/code> command to s<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/trip-and-travel\/\" data-internallinksmanager029f6b8e52c=\"10\" title=\"Trip &amp; Travel\" target=\"_blank\" rel=\"noopener\">trip<\/a> ownership and permissions data from the copied files left at the destination. Unlike regular Unix <code>cp<\/code>, Kubectl\u2019s implementation is always recursive so referencing a directory will include its contents too.<\/p>\n<h2 id=\"when-kubectl-cp-wont-work\"><span class=\"ez-toc-section\" id=\"When_Kubectl_Cp_Wont_Work\"><\/span>When Kubectl Cp Won\u2019t Work<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>kubectl cp<\/code> comes with one big caveat: internally, it\u2019s a simple wr<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>er around <code>tar<\/code>. The command will compress the source path, copy the archive to the destination, and then extract its content. Your container image must include the <code>tar<\/code> binary in its path for this to work.<\/p>\n<p>This means <code>kubectl cp<\/code> may not always be an option when you\u2019re following Docker best practices to keep images small. Although popular minimal bases such as <code>alpine<\/code> do include <code>tar<\/code>, it may not feature in every image and won\u2019t be present if you\u2019re assembling them from scratch.<\/p>\n<p>The use of <code>tar<\/code> also limits what <code>kubectl cp<\/code> can copy. You can\u2019t resolve symlinks or use wildcards like <code>\/tmp\/*.log<\/code>. You\u2019ll have to use a different approach when this functionality is needed.<\/p>\n<p>One option is to combine <code>kubectl exec<\/code>, the <code>cat<\/code> command, and shell redirection to stream a file\u2019s content out of the cluster and into a local file:<\/p>\n<pre>kubectl exec &#13;\n    --namespace example-namespace &#13;\n    example-pod -c second-container &#13;\n    -- cat \/tmp\/example-file &gt; local-file<\/pre>\n<p>The <code>kubectl exec<\/code> command starts a shell session in the Pod. Everything after the <code>--<\/code> is run inside the container. The output of calling <code>cat<\/code> on the file will be emitted to your terminal where it can be redirected into a local file.<\/p>\n<p>This technique is suitable when you\u2019re copying a handful of relatively small files. It would quickly become tedious if you needed to retrieve a large directory. Unfortunately there\u2019s no better alternative without adding additional software to your container.<\/p>\n<p>If you\u2019re feeling daring, you could install SSH in your container and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/66776\/how-to-remotely-copy-files-over-ssh-without-entering-your-password\">use <code>scp<\/code> to<\/a> access its filesystem from your host. This would give you a complete copying experience at the expense of running an extra daemon and creating a potential security issue. It should only be considered on properly protected clusters.<\/p>\n<p>Adding <code>tar<\/code> to your container images is a much safer option. This lets you use <code>kubectl cp<\/code> and unlocks more advanced manual copying via <code>kubectl exec<\/code> when you need extra functionality. If you run into limitations with the <code>cp<\/code> syntax, invoke <code>tar<\/code> directly via <code>kubectl exec<\/code> to add extra flags to the command:<\/p>\n<pre>kubectl exec &#13;\n    --namespace example-namespace &#13;\n    example-pod -c second-container &#13;\n    -- tar -cf \/tmp\/example-dir | tar xf - -C .\/extracted-dir<\/pre>\n<p>This runs <code>tar -cF<\/code> inside your container and pipes the result to the <code>tar xf<\/code> extraction command on your local machine. The <code>-<\/code> instructs <code>tar<\/code> to extract the data fed to it via the standard input stream. <code>-C<\/code> is used to specify a destination directory.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>kubectl cp<\/code> command lets you copy files between Kubernetes Pods and your machine. It works in either direction but can\u2019t be used to move files from Pod to Pod. If you need to do that, it\u2019s best to use a two-stage procedure, copying first from Pod A to your machine, then onward to Pod B.<\/p>\n<p>As <code>cp<\/code> relies on <code>tar<\/code> under-the-hood, you need to make sure your container image includes it. You\u2019ll see a <code>No such file or directory tar<\/code> error if you try to run <code>cp<\/code> against a Pod without <code>tar<\/code>. In this situation, either add <code>tar<\/code> to the container or use a combination of <code>kubectl exec<\/code> and <code>cat<\/code> if you only need to retrieve a few files.\n<\/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\/14811\/how-to-copy-files-between-kubernetes-pods-and-your-machine\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Copy Files Between Kubernetes Pods and Your Machine \u2013 CloudSavvy IT&#8221; Containers running in Kubernetes Pods are meant to be self-contained compute units that you don\u2019t need to manually interact with. Sometimes you might have to copy files to or from a Pod\u2019s filesystem though, perhaps because you\u2019re debugging a problem and want&#8230;<\/p>\n","protected":false},"author":1,"featured_media":373807,"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-373806","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\/373806","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=373806"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/373806\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/373807"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=373806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=373806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=373806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}