Home > Net >  KUBECTL Error while trying to copy a file from a container to my local mac
KUBECTL Error while trying to copy a file from a container to my local mac

Time:08-08

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?

CodePudding user response:

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

CodePudding user response:

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
  • Related