Copy a file into kubernetes pod without using kubectl cp

4/29/2019

I have a use case where my pod is run as non-rootuser and its running a python app. Now I want to copy file from master node to running pod. But when I try to run

kubectl cp app.py 103000-pras-dev/simplehttp-777fd86759-w79pn:/tmp

This command hungs up but when i run pod as root user and then run the same command it executes successfully. I was going through the code of kubectl cp where it internally uses tar command.

Tar command has got multiple flags like --overwrite --no-same-owner, --no-preserve and few others. Now from kubectl cp we can't pass all those flag to tar. Is there any way by which I can copy file using kubectl exec command or any other way.

kubectl exec simplehttp-777fd86759-w79pn -- cp app.py /tmp/ **flags**
-- prashant
kubernetes

2 Answers

5/16/2019

If it is only a text file it can be also "copied" via netcat.

1) You have to be logged on both nodes

$ kubectl exec -ti <pod_name> bash

2) Make sure to have netcat, if not install them

$ apt-get update
$ apt-get install netcat-openbsd

3) Go to the folder with permissions i.e.

/tmp

4) Inside the container where you have python file write

$ cat app.py | nc -l <random_port>

Example

$ cat app.py | nc -l 1234

It will start listening on provided port.

5) Inside the container where you want have the file

$ nc <PodIP_where_you_have_py_file> > app.py 

Example

$ nc 10.36.18.9 1234 > app.py

It must be POD IP, it will not recognize pod name. To get ip use kubectl get pods -o wide

It will copy content of app.py file to the other container file. Unfortunately, you will need to add permissions manual or you can use script like (sleep is required due to speed of "copying"):

#!/bin/sh
nc 10.36.18.9 1234 > app.py | sleep 2 |chmod 770 app.py;
-- PjoterS
Source: StackOverflow

4/29/2019

Meanwhile I found a hack, disclaimer this is not the exact kubectl cp just a workaround.

I have written a go program where I have created a goroutine to read file and attached that to stdin and ran kubectl exec tar command with proper flags. Here is what I did

reader, writer := io.Pipe()
copy := exec.CommandContext(ctx, "kubectl", "exec", pod.Name, "--namespace", pod.Namespace, "-c", container.Name, "-i",
    "--", "tar", "xmf", "-", "-C", "/", "--no-same-owner") // pass all the flags you want to
copy.Stdin = reader
go func() {
    defer writer.Close()

    if err := util.CreateMappedTar(writer, "/", files); err != nil {
        logrus.Errorln("Error creating tar archive:", err)
    }
}()

Helper function definition

func CreateMappedTar(w io.Writer, root string, pathMap map[string]string) error {
    tw := tar.NewWriter(w)
    defer tw.Close()

    for src, dst := range pathMap {
        if err := addFileToTar(root, src, dst, tw); err != nil {
            return err
        }

    }

    return nil
}

Obviously, this thing doesn't work because of permission issue but *I was able to pass tar flags

-- prashant
Source: StackOverflow