Copying Files to and from Kubernetes Pods

A guide on how to copy files and folders between your local machine and Kubernetes pods

Copying Files to and from Kubernetes Pods

In this article, we discuss the kubectl cp command, which allows for easy file transfers between local machines and Kubernetes pods. We provide examples for copying files from local to remote pods and vice versa, as well as a reference to the official kubectl cheat sheet.

kubectl provides a handy command, kubectl cp, for transporting files between pods, similar to the scp linux command.

copying from your local machine to a specified pod

kubectl cp <local-path> <k8s-namespace>/<k8s-pod>:<remote-path>

For example, copying the local file ~/baz.txt to the remote folder /foo/bar.

kubectl cp ~/baz.txt my-namespace/my-pod:/foo/bar

copying from a remote pod to your local machine

kubectl cp <k8s-namespace>/<k8s-pod>:<remote-path> <local-path>

For example, copying the remote file /foo/bar.txt to the local folder ~/baz/my-folder.

kubectl cp my-namespace/my-pod:/foo/bar.txt ~/baz/my-folder

reference from the official cheatsheet

kubectl cheat sheet

kubectl cp /tmp/foo_dir my-pod:/tmp/bar_dir            # Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the current namespace
kubectl cp /tmp/foo my-pod:/tmp/bar -c my-container    # Copy /tmp/foo local file to /tmp/bar in a remote pod in a specific container
kubectl cp /tmp/foo my-namespace/my-pod:/tmp/bar       # Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace my-namespace
kubectl cp my-namespace/my-pod:/tmp/foo /tmp/bar       # Copy /tmp/foo from a remote pod to /tmp/bar locally