How to copy files from kubernetes Pods to local system
Asked Answered
S

20

256

I'm trying to copy files from Kubernetes Pods to my local system. I am getting the below error while running following command:

kubectl cp aks-ssh2-6cd4948f6f-fp9tl:/home/azureuser/test.cap ./test.cap

Output:

tar: home/azureuser/test: Cannot stat: No such file or directory tar: Exiting with failure status due to previous errors error: home/azureuser/test no such file or directory

I could see the file under above given path. I am really confused.

Could you please help me out?

Studer answered 19/9, 2018 at 13:40 Comment(0)
Y
267

As stated inkubectl help:

kubectl cp --help
Copy files and directories to and from containers.
Examples:
# !!!Important Note!!!
# Requires that the 'tar' binary is present in your container
# image.  If 'tar' is not present, 'kubectl cp' will fail.

# 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 <some-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

Options:
-c, --container='': Container name. If omitted, the first container in the pod will be chosen

Usage:
kubectl cp <file-spec-src> <file-spec-dest> [options]

Use "kubectl options" for a list of global command-line options (applies to all commands).

You can also login to your Containter and check if file is there:

kubectl exec -it aks-ssh2-6cd4948f6f-fp9tl /bin/bash
ls -la /home/azureuser/test.cap

If this still doesn't work, try:

You may try to copy your files to workdir and then retry to copy them using just their names. It's weird, but it works for now.

Consider advice of kchugalinskiy here #58692.

Yip answered 19/9, 2018 at 14:47 Comment(2)
Lesson: don't forget to put "namespace or project-name(if openshift)" also it could not cp linked file (getting tar: Removing leading / from member names error)Jefe
Also note if you're getting is outside target destination when trying to copy from pod to local, then a solution that seemed to work is making it so the local destination is relative. e.g. kubectl -c my-service cp namesapce/pod:/tmp/remoteFile1 localFilename Dialytic
S
70

Let's say you are copying file from bin folder to local system. The command is

kubectl cp default/POD_NAME:bin/FILE_NAME /Users/username/FILE_NAME

You can connect to POD to verify if you are specifying correct file name

kubectl exec -ti POD_NAME bash
Solvable answered 16/10, 2018 at 1:8 Comment(2)
Is there a way to copy files from Kubernetes driver to all the pods? This is the question - #71823125Locoism
E.g. pattern: kubectl cp -namespace MyNamespace MyPod:MyFilename MyFilenameFante
Z
44

According to https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands

kubectl cp <file-spec-src> <file-spec-dest> is equivalent to using

kubectl exec -n <some-namespace> <some-pod> -- tar cf - <src-file> | tar xf - -C <dest-file>

So technically if you do not have tar installed on the pod, you can do kubectl exec -n <some-namespace> <some-pod> -- cat <src-file> > <dest-file>

Assuming the file is small or already compressed, the effect should be the same, except you cannot use cat on a directory or a set of files.

Zinciferous answered 3/3, 2020 at 6:57 Comment(3)
Million thanks! Saved my day. But how does it work? How do pod understand where to put a file as we just specify a folder which pod is not aware of?Measure
It works because you are running command(s) in your local terminal and piping the output of one to the other (or into a file, in the case of the cat). In the tar example, you are running the local command kubectl and piping its output into the local command tar. The kubectl command just happens to be running commands in the pod and transparently bringing the output of that command to your local terminal.Deflower
Beware that this cat trick may break binary files. I did that with a Java heap dump and the file was modified during the transfer. The md5sum of the file on the pod didn't match the md5sum of the file I got locally. The file was completely broken.Thiele
D
30

The command in the question posted is absolutely right. As answered before, this particular issue seems to be a missing tar binary in the container. I actually did not know it was needed, but confirmed that the pod has it:

# find / -name tar
/bin/tar
/usr/lib/mime/packages/tar
/usr/share/doc/tar

My error was using . to copy to the current directory (works with cp and scp) because it needs the full path, as shown in the original question:

kubectl cp pod-name-shown-in-get-pods:path/to/filename /local/dir/filename

But not:

kubectl cp pod-name-shown-in-get-pods:path/to/filename .

Which gives:

error: open .: is a directory
tar: Removing leading `/' from member names

Now the tar in the error message makes sense!

Note that if there is a leading / in the source path, as in the following example:

kubectl cp pod-name-shown-in-get-pods:/etc/resolv.conf /local/dir/resolv.conf

You would also see:

tar: Removing leading `/' from member names

However, the warning can be ignored, as the file would still copied. Use etc/resolv.conf instead of /etc/resolv.conf in the above example, to copy without the warning.

Deliberative answered 4/12, 2020 at 16:31 Comment(1)
It is strange why leading / is giving warning.Andryc
C
17

kubectl cp command is already mentioned by some of the users on this thread.

kubectl cp <pod-id>:<path> <local-path> -n <namespace> -c <specific_container>

Note that to run this command tar utility should already be installed on the pod.

However I have come across few errors while running this command on windows PowerShell.

PS P:\Users\nstty\Downloads\k8s-diags> kubectl cp dremio-master-0:/var/log/dremio/server.log P:\Users\nstty\Downloads\k8s-diags\server-logs\
error: one of src or dest must be a local file specification

error: one of src or dest must be a local file specification

When running this command on windows, don't use the full path of the local system. Use relative path instead (. or ..). Now using relative path in the below command but getting a different error.

PS P:\Users\nstty\Downloads\k8s-diags> kubectl cp dremio-master-0:/var/log/dremio/server.log .
tar: Removing leading `/' from member names
error: open .: is a directory

error: open .: is a directory

If you are copying a file, then in the local path use the relative path along with the file name that you want for the copied file. kubectl will first create this file and then copy the contents to this file. Below is the working command.

PS P:\Users\nstty\Downloads\k8s-diags> kubectl cp dremio-master-0:/var/log/dremio/server.log .\server-logs\server.log
tar: Removing leading `/' from member names

tar: Removing leading `/' from member names

This message is just a warning from tar utility in your pod. The file should be copied to your local system.

Alternate option: If you want to avoid kubectl cp, here is another approach which we use.

Chantalchantalle answered 20/6, 2022 at 11:12 Comment(0)
H
13

"kubectl cp" command is used to copy files from pods to local path and vice versa

  1. Copying file from pod to local

    kubectl cp <pod_name>:<file_path> <destination_path>

  2. Copying file from specific container of pod to local

    kubectl cp <pod_name>:<file_path> <destination_path> -c specific_container

  3. Copying file from local to pod

    kubectl cp <local_source_path> <pod_name>:<destination_path>

Hydrolyze answered 25/5, 2021 at 5:39 Comment(0)
R
8
kubectl cp <pod-id>:<path> <destination-path> -n <namespace>

Worked for me.

Roentgenoscope answered 25/5, 2021 at 3:57 Comment(1)
The <path> should start from the / but not with /. ex: kubectl cp <pod-id>:path/from/root/dir <destination-path> -n <namespace>Khoisan
Z
4

You can mount a local directory into the pod.

Update your aks-ssh yaml file:

spec:
  ...
  containers:
    ...
    volumeMounts:
    - name: test-dir
      mountPath: /home/azureuser
    ...
  volumes:
  - name: test-dir
    hostPath:
      path: /path/to/your/local/dir

Now you can access your files in the local directory.

Zellner answered 19/9, 2018 at 15:33 Comment(1)
That doesn’t help if the machine you’re calling from isn’t one of the Kubernetes nodes; for instance, if you’re using a managed cloud Kubernetes installation and trying to get content out of it on to your local laptop.Oversubtlety
F
4

For people working on a Windows machine there is an additional gotcha: As at October 2021 you cannot include a drive letter in your local path.

So if you were to try a command like:

kubectl cp aks-ssh2-6cd4948f6f-fp9tl:/home/azureuser/test.cap C:/Temp/Test

you would get this error because kubectl cp sees the colon in the Windows path as the separator between a pod name and the path within the pod. So it would see C:/Temp/Test as a pod named "C" with a path "/Temp/Test"

The way to get around this is to use a relative Windows path instead of an absolute path. It will need to be relative to your current working directory.

So if my current working directory is C:\Users\JoeBloggs and I wanted to copy down to C:\Temp\Test I'd need to use the command:

kubectl cp aks-ssh2-6cd4948f6f-fp9tl:/home/azureuser/test.cap ../../Temp/Test

Note that this issue looks like it may be fixed soon. See https://github.com/kubernetes/kubernetes/pull/94165

Fadeout answered 4/11, 2021 at 21:34 Comment(0)
E
4

Maybe someone could met this error

tar: removing leading '/' from member names
error: open .: is a directory

Which induced by the following command

 kc cp -n monitoring <pod name>:/usr/share/grafana/conf/defaults.ini ./

 kc cp -n monitoring <pod name>:/usr/share/grafana/conf/defaults.ini ./default.ini

To solve it, we should add a destination folder per doc

kc cp -n monitoring <pod name>:/usr/share/grafana/conf/defaults.ini ./tmp/default.ini

Ebert answered 15/12, 2021 at 3:24 Comment(0)
P
3

This works for me:

kubectl cp "namespace"/"pod_name":"path_in_pod" "local_path"

Example:

kubectl cp mynamespace/mypod:var/www/html/index.html \Users\myuser\Desktop\index.html
Pulp answered 31/1, 2022 at 10:59 Comment(0)
S
2

If anyone uses windows pods, it may be hard to get files copied to the pods from local machine with those linux paths for kubectl cp command:

Procedure to copy files from local machine to kubernetes pod: (especially windows container)

  1. I want to copy node.aspx from my local machine to podname:\c:\inetpub\wwwroot
  2. First upload Node.aspx to your cloud drive, path will be /home/{your_username} in my case /home/pranesh
  3. Then find out the pod name, in my case its aspx-deployment-84597d88f5-pk5nh, follow below command
PS /home/pranesh> kubectl cp /home/pranesh/Node.aspx aspx-deployment-84597d88f5-pk5nh:/Node.aspx
  1. This copies the file to c drive of container,
  2. then move file from c drive to required path with powershell
PS /home/pranesh> kubectl exec aspx-deployment-84597d88f5-pk5nh powershell "Copy-Item "C:\Node.aspx" -Destination "C:\inetpub\wwwroot""
  1. Use the reverse procedure for copying from container to cloud drive and download.
Sponson answered 7/9, 2020 at 13:59 Comment(1)
After copying my.log in the windows container to root of c, command to copy locally was kubectl cp my-namespace/my-pod-name:my.log ./my.logSaundra
F
1

I resolve this problem by set the source folder to be relative path. If the file location is /home/azureuser/test.cap, and working dir is /home/azureuser/, the cmd is

kubectl cp aks-ssh2-6cd4948f6f-fp9tl:test.cap ./test.cap

Favourite answered 20/12, 2018 at 9:55 Comment(0)
A
0

kubectl cp will not work if your container does not have tar command in the PATH. From your error it sees like tar command is not available on your container. https://github.com/kubernetes/kubernetes/issues/58512 Please explore other options

Aguirre answered 8/8, 2019 at 9:13 Comment(0)
A
0

Kubernetes gives a file not found error when user does not have permissions to a pod. That was my problem.

Atwitter answered 17/7, 2020 at 3:39 Comment(0)
S
0

On my side the issue was with having multiple containers inside the pod:

kubectl cp -c grafana \
    metrics/grafana-5c4f76b49b-p88lc:/etc/grafana \
    ./grafana/etc

so set the container name with -c grafana and properly prefix the namespace of the pod in my case metrics/

Scorbutic answered 18/9, 2020 at 0:11 Comment(0)
F
0

I tried this method on Azure and it worked:

kubectl cp 'POD NAME':xyz.json test

kubectl cp is the command for copying

POD NAME =  vote-app

xyz.json is the file that needs to be copied from pod the test is the file created in the drive of the AZURE directory...

So final command would be:

kubectl cp vote-app:xyz.json test

test will get generated in Azure Directory and later u can download test file from the download option of Azure enter image description here

Fleece answered 24/12, 2020 at 8:42 Comment(0)
J
0

Steps to Copy File from Kubernetes Pod to Local

  1. Go to the local path you want to copy..... Then Enter the below command kubectl cp pod_name -c <container_name_if_present>:<path_of_the file> <name_of_the_file> <desired_name_of_the_file>

Note: <Path_of_the_file> can be obtained by Executing "pwd" at the location of the file inside the pod.

  1. To copy files from local to pod kubectl cp <path_of_the file_in_local>/<file_name> <pod_name>:
Jodhpur answered 13/11, 2023 at 10:46 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Intensity
R
0

I stumbled upon this thread while searching for similar errors I’ve encountered when attempting to copy a file from my Kubernetes pod to my local host. In my specific case, I realized that I need to specify the path to the pod’s file system, which resides under /host:

1 SSH into my pod hosted on an AKS cluster:

ssh -o 'ProxyCommand ssh -p 2022 -W %h:%p [email protected]' [email protected]

1.1. Check the user ID:

azureuser@aks-nodepool1-39416372-vmss000009:~$ whoami
azureuser
azureuser@aks-nodepool1-39416372-vmss000009:~$ id
uid=1000(azureuser) gid=1000(azureuser) groups=1000(azureuser),4(adm),20(dialout),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),119(netdev),120(lxd)

1.2. Create a test file:

azureuser@aks-nodepool1-39416372-vmss000009:~$ echo "test" > test.txt
azureuser@aks-nodepool1-39416372-vmss000009:~$ pwd
/home/azureuser
azureuser@aks-nodepool1-39416372-vmss000009:~$ ls
test.txt

2.1. From my local host, I attempted to search for the file under the default / directory as ‘root’:

PS D:\> kubectl exec -i -t node-debugger-aks-nodepool1-39416372-vmss000009-sbncw -c debugger -- whoami
 root     

PS D:\> kubectl exec -i -t node-debugger-aks-nodepool1-39416372-vmss000009-sbncw -c debugger -- id
 uid=0(root) gid=0(root) groups=0(root)     

PS D:\> kubectl exec -i -t node-debugger-aks-nodepool1-39416372-vmss000009-sbncw -c debugger -- ls /home/azureuser
 ls: /home/azureuser: No such file or directory
 command terminated with exit code 1

2.2. However, the file is actually located in /host, which corresponds to the pod’s file system (I’m still wrapping my head around this):

PS D:\> kubectl exec -i -t node-debugger-aks-nodepool1-39416372-vmss000009-sbncw -c debugger -- ls /host/home/azureuser
 test.txt

2.3. Now that I’ve corrected the path, the cp command should work:

PS D:\Repos> kubectl cp node-debugger-aks-nodepool1-39416372-vmss000009-sbncw:host/home/azureuser/test.txt \test.txt  

PS D:\> dir \test.txt

    Directory: D:\

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          21/02/2024    13:44              5 test.txt
Roccoroch answered 21/2 at 13:21 Comment(0)
W
-1

I couldn't get kubectl to work for this. Was getting error:

The connection to the server localhost:8080 was refused - did you specify the right host or port?

This worked for me instead:

docker cp CONTAINERID:FILEWITHPATH DESTFILENAME

where "CONTAINERID" was retrieved by calling docker ps.

Wikiup answered 5/11, 2021 at 20:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.