How to use tar command options when calling kubectl cp command

1/16/2020

It seems the kubectl cp command uses tar command in the background. How can I use (pass them via the kubectl cp command) some of tar command options such as --exclude? For example, this could be helpful to exclude specific files/directories that are not needed to be copied out of the container.

Thanks

-- imriss
copy
kubectl
kubernetes
options
tar

1 Answer

1/16/2020

You can do it as -

kubectl exec -n <some-namespace> <some-pod> -- tar cf - --exclude='pattern' /tmp/foo | tar xf - -C /tmp/bar

As example -

I got a pod running in the namespace "pankaj"; and inside my container i got 2 files at path - /tmp/foo

/tmp/foo # ls
file1.txt  file2.txt

My use is I want to copy only file file1.txt so I will be doing it like -

kubectl exec -n pankaj <pod name> -- tar cf - --exclude='file2.txt' tmp/foo | tar xf - -C /Users/pa357856/test

the result will be I will get only file1.txt

$ pwd
/Users/pa357856/test
$ ls tmp/foo/
file1.txt
-- Pankaj
Source: StackOverflow