12

Is this possible inside the kubernetes cluster?

All examples I've found are copying from a local disk to a pod or vice versa, or is the only option to copy from node to node, for example over SSH, SCP or with other utilities?

slm
  • 15,396
  • 12
  • 109
  • 124
JDev
  • 2,157
  • 3
  • 31
  • 57

4 Answers4

14

It's not possible to do cluster to cluster copying. You'd need to use kubectl cp to copy it locally, then copy the file back:

kubectl cp <pod>:/tmp/test /tmp/test
kubectl cp /tmp/test <pod>:/tmp/test

If you are trying to share files between pods, and only one pods needs write access, you probably want to mount an ro volume on multiple pods, or use an object store like S3. Copying files to and from pods really shouldn't be something you're doing often, that's an anti-pattern

jaxxstorm
  • 12,422
  • 5
  • 57
  • 67
  • Yes, I understand all this. But for now I want to make an intermediate version, so that it works. Initially, to adjust everything as needed, actually is no time and resources to configure everything correctly. The whole idea is that I want to set up the jenkins on one node and applications an other. I certainly understand how to do everything right. The correct way is still a little time consuming and resource intensive. – JDev Jul 19 '18 at 17:00
  • But wait, this is the solution! I have jenkins with kubectl with access to the cluster. I can directly copy from jenkins to the pod shared folder. Then if the pod suddenly it is recreated. Then in the general folder there will be a file which i copied from jenkins. – JDev Jul 19 '18 at 17:04
4

Check out this article.

In short, this is the command:

kubectl exec pod-01 -- tar cf - /dir1 /dir2 | kubectl exec -i pod-02 -- tar xvf - -C /
  • NICE! `kubectl cp` no longer supports symlinks, and doesn't want to support pod-to-pod. It makes wonder why even have it? This command is much better! – TigerBear Apr 03 '23 at 11:36
0

You can build an image with an openssh server and then scp into it.

Dockerfile:

...
RUN mkdir /root/.ssh
RUN apt install -y openssh-server
ADD id_rsa* authorized_keys /root/.ssh/
RUN chown root.root /root/.ssh/ -R
...

You will have to curl for the IP of that pod so you can ssh into it. Make sure that sshd is running inside that pod.

Michael
  • 445
  • 4
  • 16
-1

Here's a way to copy from inside your container to your local machine:

kubectl cp <file-spec-src> <file-spec-dest>

example

kubectl cp your-pod:/path/to/file /local/path
kubectl cp /local/path your-pod:/path/to/file