How to copy files from kubernetes Pods to local system How to copy files from kubernetes Pods to local system kubernetes kubernetes

How to copy files from kubernetes Pods to local system


As stated inkubectl help:

kubectl cp --helpCopy files and directories to and from containers.Examples:# !!!Important Note!!!# Requires that the 'tar' binary is present in your container# image.  If 'tar' is not present, 'kubectl cp' will fail.# Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the default namespacekubectl cp /tmp/foo_dir <some-pod>:/tmp/bar_dir# Copy /tmp/foo local file to /tmp/bar in a remote pod in a specific containerkubectl cp /tmp/foo <some-pod>:/tmp/bar -c <specific-container># Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace <some-namespace>kubectl cp /tmp/foo <some-namespace>/<some-pod>:/tmp/bar# Copy /tmp/foo from a remote pod to /tmp/bar locallykubectl cp <some-namespace>/<some-pod>:/tmp/foo /tmp/barOptions:-c, --container='': Container name. If omitted, the first container in the pod will be chosenUsage:kubectl cp <file-spec-src> <file-spec-dest> [options]Use "kubectl options" for a list of global command-line options (applies to all commands).

You can also login to your Containter and check if file is there:

kubectl exec -it aks-ssh2-6cd4948f6f-fp9tl /bin/bashls -la /home/azureuser/test.cap

If this still doesn't work, try:

You may try to copy your files to workdir and then retry to copy them using just their names. It's weird, but it works for now.

Consider advice of kchugalinskiy here #58692.


Let's say you are copying file from bin folder to local system. The command is

kubectl cp default/POD_NAME:bin/FILE_NAME /Users/username/FILE_NAME

You can connect to POD to verify if you are specifying correct file name

kubectl exec -ti POD_NAME bash


According to https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands

kubectl cp <file-spec-src> <file-spec-dest> is equivalent to using

kubectl exec -n <some-namespace> <some-pod> -- tar cf - <src-file> | tar xf - -C <dest-file>

So technically if you do not have tar installed on the pod, you can do kubectl exec -n <some-namespace> <some-pod> -- cat <src-file> > <dest-file>

Assuming the file is small or already compressed, the effect should be the same, except you cannot use cat on a directory or a set of files.