I\'m trying to copy files from Kubernetes Pods to my local system. I am getting the below error while running following command:
kubectl cp aks-ssh2-6cd4948
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.
On my side the issue was with having multiple containers inside the pod:
kubectl cp -c grafana \
metrics/grafana-5c4f76b49b-p88lc:/etc/grafana \
./grafana/etc
so set the container name with -c grafana
and properly prefix the namespace of the pod in my case metrics/
kubectl cp will not work if your container does not have tar command in the PATH. From your error it sees like tar command is not available on your container. https://github.com/kubernetes/kubernetes/issues/58512 Please explore other options
The command in the question posted is absolutely right. As answered before this particular issue seems to be missing tar
in the container. I actually did not know it was needed, but confirmed that the pod has it:
# find / -name tar
/bin/tar
/usr/lib/mime/packages/tar
/usr/share/doc/tar
My error was using .
to copy to the current directory (works with cp
and scp
) because it needs the full path, as shown in the original question:
kubectl cp pod-name-shown-in-get-pods:/path/to/filename /local/dir/filename
But not:
kubectl cp pod-name-shown-in-get-pods:/path/to/filename .
Which gives:
error: open .: is a directory
tar: Removing leading `/' from member names
Now the tar
in the error message makes sense!
I resolve this problem by set the source folder to be relative path. If the file location is /home/azureuser/test.cap, and working dir is /home/azureuser/, the cmd is
kubectl cp aks-ssh2-6cd4948f6f-fp9tl:test.cap ./test.cap