0

I'm trying to copy a file from a container to my mac and I also look into this question How to copy files from kubernetes Pods to local system but didn’t help

at first, I tried

kubectl cp -n company -c company-web-falcon company/company-web-falcon-bb86d79cf-6jcqq:/etc/identity/ca/security-ca.pem /etc/identity/ca/security-ca.pem

which resulted in this error

tar: Removing leading `/' from member names

So I tried this

kubectl cp -n company -c company-web-falcon company/company-web-falcon-bb86d79cf-6jcqq:/etc/identity/ca/security-ca.pem /etc/identity/ca/security-ca.pem  .

which resulted in this error

    error: source and destination are required

how should I fix it?

Jonas
  • 121,568
  • 97
  • 310
  • 388
Hard Worker
  • 995
  • 11
  • 33
  • remove the `slash` from the pod path i.e `/etc` it should be `etc/..` and it should work `kubectl cp -n company -c company-web-falcon company/company-web-falcon-bb86d79cf-6jcqq:etc/identity/ca/security-ca.pem /etc/identity/ca/security-ca.pem` – Adiii Aug 08 '22 at 06:04
  • @Adiii its failing on the file name tar: etc/identity/ca/security-ca.pem: Cannot stat: No such file or directory tar: Exiting with failure status due to previous errors – Hard Worker Aug 08 '22 at 07:19
  • then seems like `Workingdir` is set either in the docker image or deployment, so other way would be `kubectl exec company-web-falcon -- bash "cp -r /etc/identity/ca/security-ca.pem .` then run the `cp` command – Adiii Aug 08 '22 at 07:34

2 Answers2

3

You can copy the file with the following command kubectl cp

  • Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the default namespace
kubectl cp /tmp/foo_dir <some-pod>:/tmp/bar_dir
  • Copy /tmp/foo local file to /tmp/bar in a remote pod in a specific container
kubectl cp /tmp/foo <some-pod>:/tmp/bar -c <specific-container>
  • Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace
kubectl cp /tmp/foo <some-namespace>/<some-pod>:/tmp/bar
  • Copy /tmp/foo from a remote pod to /tmp/bar locally
kubectl cp <some-namespace>/<some-pod>:/tmp/foo /tmp/bar

As for the warning that you receive, you can get more details in the github issues There are multiple accepted explanations. The best one is the following

You move the wanted file to the working dir in the pod (the directory which is automatically opened, when you open bash on it) -

user@podname:/usr/src# ls data.txt
data.txt

In this case it is - /usr/src folder.

Then in the local bash terminal -

user@local:~$ kubectl cp podname:data.txt data.txt
user@local:~$ ls data.txt
data.txt
codeaprendiz
  • 2,703
  • 1
  • 25
  • 49
2

So based on the comment seems like WORKINGDIR is set either in the docker file or in the deployment, in that case, it will fail. so if you want to achieve this you should move it to the working directory.

kubectl exec company-web-falcon-bb86d79cf-6jcqq -c company-web-falcon -- bash -c "cp /etc/identity/ca/security-ca.pem ." && kubectl cp -n company -c company-web-falcon company/company-web-falcon-bb86d79cf-6jcqq:security-ca.pem /etc/identity/ca/security-ca.pem
Adiii
  • 54,482
  • 7
  • 145
  • 148
  • my workdir is empty and read only - if i have a workdir i can only copy files from work dir ? – Hard Worker Aug 08 '22 at 07:50
  • i do not think its empty, having same issue on mac in the past, you can check the above, `cp file .` will copy it to working dir and then you will see that the copy command work. but the cp command looking into `working` directory – Adiii Aug 08 '22 at 07:52