This Kubernetes tutorial will teach you how to copy files from a Kubernetes pod to your local machine. Kubernetes is an open-source system for automating deployment, scaling and management of containerized applications. Kubernetes does this by distributing containers as services across a cluster of nodes in the Kubernetes environment.
In order to accomplish this goal, we need to use kubectl command-line utility with the cp option for copy files and folders to and from pods and localhost.
Below are a few examples that will cover most of the options that are available.
Copy a Folder (Pod to Localhost)
The below command will copy a folder from a pod /apps directory to the local machine.
kubectl cp server-deployment-f66b6c774-bwxqp:/apps /home/name/files
Copy specific file (Pod to Localhost)
The below command will copy a file located inside the a pod /apps directory to the local machine.
kubectl cp server-deployment-f66b6c774-bwxqp:apps/config /home/name/files/config
Copy a Folder (Localhost to Pod)
The below command will copy a folder from the localhost directory to a pod.
kubectl cp /home/name/files/config server-deployment-f66b6c774-bwxqp:apps/config