Recently I have been experimenting with different ways to handle storage in Kubernetes; this usually requires me to rsync data from one storage backend to another. My goto solution was to mount both storage backend PVs in the same pod and use rsync, but that is cumbersome and got annoying to do each time.

There is a kubectl cp command which lets you copy files and directories either from your local filesystem to a kubernetes pod, or from the kubernetes pod to your local filesystem. It is not currently possible to copy directly from one pod to another pod.

An alternative is to pipe the stdout of the tar command to the stdin of tar in a separate pod, similar to using tar to copy files between two servers without rsync/scp. This does require both pods to have tar installed, but assuming it is then you can migrate/copy data from one pod to another pod like this:

kubectl exec pod-01 -- tar cf - /dir1 /dir2 | kubectl exec -i pod-02 -- tar xvf - -C /

The above copies the contents of /dir1 and /dir2 from pod-01 to the same location in pod-02. Since these are two separate kubectl commands, you could also specify separate namespaces for each.

Note: This passes all of the traffic through your local machine which may limit the effective throughput for the transfer when working with remote kubernetes clusters. Add -z to enable compression if needed.


If you do not use Kubernetes, this same method can be used on a single machine with docker using docker exec instead of kubectl.