15

I have several files named out1, out2, ... in my Kubernetes container. I want to copy them to my local computer. I am using:

$ kubectl cp pod:/path/out* .

But I am getting an error:

tar: Removing leading `/' from member names
tar: /path/out*: Cannot stat: No such file or directory tar: Exiting with failure status due to previous errors
error: path/out* no such file or directory

How can I use kubectl cp with wildcards?

11

Wildcard support was added to kubectl cp in the #72641 pull request.

It is merged but only available in Kubernetes v1.14.0. Take a look at the ChangeLog:

Notable Features:
[...]
• kubectl supports copying files with wild card (#72641, @dixudx)

However, as stated on the issue #78854 the PR was made without proper tests and the feature is actually broken. The PR to fix the problem (#78928) is open since June, and it's not merged yet.

Although the PR to fix the feature exists, they are also considering removing the support to wildcard copy.

2
8

You can do that using this command:

kubectl cp -n [NAMESPACE] [POD_NAME]:/[POD_DIRECTORY]/. .
3
  • This is not a solution that uses wildcards to copy files. kubectl cp with wildcards was introduced on Kubernetes v1.14. Mar 28 '19 at 22:01
  • I see, I misunderstood the question, your answer is correct. @EduardoBaitello Mar 28 '19 at 22:02
  • And yet, this solved my problem. Upvoted (note that the other answer with more votes is still the one that should be accepted). Ideally this tidbit would have been included in that answer, but that's unreasonable to ask. Apr 14 at 17:47
1

As there is no wildcard support, a workaround is to do a 2-step process to achieve the same:

  • kubectl exec ... to do make a new tmp directory and wildcard copy/move your desired transfer files into that dir on the container
  • use the cmd from @cookiedough above to copy all files from that dir on the container to your local

Example:

export TARGET_POD="myapp-mypod1234" && \
kubectl exec "$TARGET_POD" -n MYNAMESPACE "$TARGET_POD" \
-- sh -c "mkdir -p /tmp/outfiles && cp /path/out* /tmp/outfiles/" && \
kubectl cp "sites/$TARGET_POD:/tmp/outfiles/." .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.