rsync files to a kubernetes pod

Solution 1:

To rsync to a pod I use the following helper:

pod=$1;shift;kubectl exec -i $pod -- "[email protected]"

I put this in a file called "rsync-helper.sh" and then run the rsync like this:

rsync -av --progress --stats -e './rsync-helper.sh' source-dir/ thePodName:/tmp/dest-dir

If you'd like a simple script that wraps this all up save this as krsync:

#!/bin/bash

if [ -z "$KRSYNC_STARTED" ]; then
    export KRSYNC_STARTED=true
    exec rsync --blocking-io --rsh "$0" [email protected]
fi

# Running as --rsh
namespace=''
pod=$1
shift

# If use uses [email protected] rsync passes as: {us} -l pod namespace ...
if [ "X$pod" = "X-l" ]; then
    pod=$1
    shift
    namespace="-n $1"
    shift
fi

exec kubectl $namespace exec -i $pod -- "[email protected]"

Then you can use krsync where you would normally rsync:

krsync -av --progress --stats src-dir/ pod:/dest-dir

Or you can set the namespace:

krsync -av --progress --stats src-dir/ [email protected]:/dest-dir

NOTE: You must have rsync executable in the pod image for this to work.

Solution 2:

In the end, I wrote a Python script to act as a receiver of tar files. You can do thus:

tar cf - . | kubectl exec shinken -i catcher -v /etc/shinken/custom_configs

Note that this only works if you cluster nodes are kubernetes 1.1 or later.


Solution 3:

If the tar binary is available on the container, you can transfer files using the new cp command.

Though possibly not as efficient as rsync.


Solution 4:

A one-liner, just edit to you names and paths:

p_user=$USER;p_name=$POD_NAME; rsync -avurP --blocking-io --rsync-path= --rsh="$(which kubectl) exec $p_name -i -- " /home/$USER/target_dir rsync:/home/$p_user/