Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubectl cp "error: one of src or dest must be a remote file specification"

When I try to copy some files in an existing directory with a wildcard, I receive the error:

kubectl cp localdir/* my-namespace/my-pod:/remote-dir/
error: one of src or dest must be a remote file specification

It looks like wildcards support have been removed but I have many files to copy and my remote dir is not empty so I can't use the recursive.

How can I run a similar operation?

like image 543
Vincent J Avatar asked May 06 '20 10:05

Vincent J


2 Answers

As a workaround you can use:

find localdir/* | xargs -I{} kubectl cp {} my-namespace/my-pod:/remote-dir/

In find you can use a wildcard to specify files you are looking for and it will copy it to the pod.

like image 91
kool Avatar answered Nov 15 '22 02:11

kool


Here is what I have come up with:

kubectl exec -n <namespace> <pod_name> -- mkdir -p <dest_dir> \
&& tar cf - -C <src_dir> . | kubectl exec -i -n <namespace> <pod_name> -- tar xf - -C <dest_dir>

Notice there are two parts, first is making sure the destination directory exists. Second is using tar to archive the files, send it and unpack it in a container.

Remember, that in order for this to work, it is required that the tar and mkdir binaries are present in your container.

The advantage od this solution over the one proposed earlier (the one with xargs) is that it is faster because it sends all files at once, and not one by one.

like image 40
Matt Avatar answered Nov 15 '22 00:11

Matt