如何将文件从kubernetes吊舱复制到本地系统

如何将文件从kubernetes吊舱复制到本地系统,kubernetes,azure-aks,Kubernetes,Azure Aks,我正在尝试将文件从Kubernetes吊舱复制到本地系统。运行以下命令时出现以下错误: kubectl cp aks-ssh2-6cd4948f6f-fp9tl:/home/azureuser/test.cap ./test.cap 输出: tar:home/azureuser/test:Cannot stat:没有这样的文件或目录tar: 由于以前的错误,以失败状态退出错误: home/azureuser/test没有这样的文件或目录 我可以看到上面给定路径下的文件。我真的很困惑 您能帮我一

我正在尝试将文件从Kubernetes吊舱复制到本地系统。运行以下命令时出现以下错误:

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

tar:home/azureuser/test:Cannot stat:没有这样的文件或目录tar: 由于以前的错误,以失败状态退出错误: home/azureuser/test没有这样的文件或目录

我可以看到上面给定路径下的文件。我真的很困惑


您能帮我一下吗?

如《kubectl帮助》中所述:

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).
如果仍然不起作用,请尝试:

您可以尝试将文件复制到workdir,然后尝试仅使用其名称复制文件。这很奇怪,但它现在起作用了`


请考虑此处的建议。

您可以将本地目录装入pod

更新aks ssh yaml文件:

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

现在,您可以访问本地目录中的文件。

假设您正在将文件从bin文件夹复制到本地系统。命令是

kubectl cp default/POD_NAME:bin/FILE_NAME /Users/username/FILE_NAME
您可以连接到POD以验证是否指定了正确的文件名

kubectl exec -ti POD_NAME bash

我通过将源文件夹设置为相对路径来解决此问题。 如果文件位置为/home/azureuser/test.cap,工作目录为/home/azureuser/,则cmd为


如果您的容器路径中没有tar命令,kubectl cp aks-ssh2-6cd4948f6f-fp9tl:test.cap./test.cap

kubectl cp将不工作。从您的错误中可以看出,tar命令在您的容器上不可用。 请根据以下内容浏览其他选项

kubectl cp
相当于使用

kubectl exec-n--tar cf-| tar xf--C

因此,从技术上讲,如果pod上没有安装tar,您可以执行
kubectl exec-n--cat>


假设文件很小或已经压缩,效果应该是相同的,只是不能在目录或一组文件上使用cat。

Kubernetes在用户没有pod权限时给出一个“未找到文件”错误。这就是我的问题。

如果有人使用windows播客,可能很难使用kubectl cp命令的linux路径从本地计算机将文件复制到播客:

将文件从本地计算机复制到kubernetes吊舱的过程:(尤其是windows容器)

  • 我想将node.aspx从本地计算机复制到 podname:\c:\inetpub\wwwroot
  • 首先将Node.aspx上载到云驱动器,路径为 /home/{your_username}在我的情况下/home/pranesh
  • 然后找出吊舱的名称,在我的例子中是 aspx-deployment-84597d88f5-pk5nh,遵循以下命令
  • 这会将文件复制到容器的c驱动器
  • 然后使用powershell将文件从c驱动器移动到所需路径
  • 使用相反的过程从容器复制到云驱动器并下载

  • 就我而言,问题在于吊舱内有多个容器:

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

    因此,将容器名称设置为
    -c grafana
    ,并在我的示例中正确地为pod的名称空间添加前缀
    metrics/

    所发布问题中的命令绝对正确。与之前一样,容器中似乎缺少此特定问题
    tar
    。我实际上不知道需要它,但确认pod有:

    # find / -name tar
    /bin/tar
    /usr/lib/mime/packages/tar
    /usr/share/doc/tar
    
    我的错误是使用
    复制到当前目录(与
    cp
    scp
    一起使用),因为它需要完整路径,如原始问题所示:

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

    其中:

    error: open .: is a directory
    tar: Removing leading `/' from member names
    
    现在,错误消息中的
    tar
    就有意义了

    请注意,如果路径中有一个前导的
    /
    ,如以下示例所示:

    kubectl cp pod-name-shown-in-get-pods:/etc/resolv.conf /local/dir/resolv.conf
    
    你还会看到:

    tar: Removing leading `/' from member names
    

    但是,警告可以忽略,因为文件仍将被复制。在上面的示例中,使用
    etc/resolv.conf
    而不是
    /etc/resolv.conf
    ,在没有警告的情况下进行复制。

    我在Azure上尝试了此方法,它成功了:

    kubectl cp 'POD NAME':xyz.json test
    
    kubectl cp is the command for copying
    
    POD NAME =  vote-app
    
    xyz.json
    是需要从pod复制的文件 测试是在AZURE目录的驱动器中创建的文件

    所以最后的命令是:

    kubectl cp vote-app:xyz.json test
    
    测试将在Azure目录中生成,稍后您可以从Azure的下载选项下载
    test
    文件

    kubectl cp:-n
    
    为我工作。

    “kubectl cp”命令用于将文件从POD复制到本地路径,反之亦然

  • 将文件从pod复制到本地
  • kubectl cp:

  • 将文件从pod的特定容器复制到本地
  • kubectl cp:-c特定容器

  • 将文件从本地复制到pod

  • kubectl cp:

    如果您呼叫的机器不是Kubernetes节点之一,则这没有帮助;例如,如果您正在使用托管云Kubernetes安装,并试图将其中的内容发送到本地笔记本电脑上。教训:不要忘了输入“名称空间或项目名称(如果openshift)”,它也无法复制链接文件(获取
    tar:从成员名称中删除前导/错误)另外请注意,如果在尝试从pod复制到本地时,您得到的
    在目标目标目标之外,那么一个似乎有效的解决方案是使其成为相对的本地目标。e、 g.
    kubectl-c我的服务cp名称apce/pod:/tmp/remoteFile1 localFilename
    000000谢谢!救了我一天。但是它是如何工作的呢?pod如何理解将文件放在哪里,因为我们只指定了一个pod不知道的文件夹?
    kubectl cp pod-name-shown-in-get-pods:/etc/resolv.conf /local/dir/resolv.conf
    
    tar: Removing leading `/' from member names
    
    kubectl cp 'POD NAME':xyz.json test
    
    kubectl cp is the command for copying
    
    POD NAME =  vote-app
    
    kubectl cp vote-app:xyz.json test
    
    kubectl cp <pod-id>:<path> <destination-path> -n <namespace>