How to copy files from kubernetes Pods to local system

烂漫一生 提交于 2020-01-31 03:09:09

问题


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?


回答1:


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.




回答2:


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



回答3:


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.




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/52407277/how-to-copy-files-from-kubernetes-pods-to-local-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!