Kubernetes copying files with rsync and kubectl without ssh access
So, given that kubectl cp
was never reliable ever for me, leading to many notes here, incl. 250115-1052 Rancher much better way to copy data to PVCs with various hacks and issues like 250117-1127 Splitting files, 250117-1104 Unzip in alpine is broken issues etc. etc. etc.
For many/large files, I’d have used rsync
, for which ssh access is theoretically needed. Not quite!
rsync files to a kubernetes pod - Server Fault
ksync.sh
(EDIT Updated by ChatGPT to support files with spaces):
if [ -z "$KRSYNC_STARTED" ]; then
export KRSYNC_STARTED=true
exec rsync --blocking-io --rsh "$0" "$@"
fi
# Running as --rsh
namespace=''
pod=$1
shift
# If user uses pod@namespace, rsync passes args as: {us} -l pod namespace ...
if [ "X$pod" = "X-l" ]; then
pod=$1
shift
namespace="-n $1"
shift
fi
# Execute kubectl with proper quoting
exec kubectl $namespace exec -i "$pod" -- "$@"
Usage is same as rsync basically :
./ksync.sh -av --info=progress2 --stats /local/dir/to/copy/ PODNAME@NAMESPACE:/target/dir/
(Or just --progress
for per-file instead of total progress).
Rsync needs to be installed on server for this to work.
For flaky connections (TODO document better): -hvvrPt --timeout1
and while ! rsync ..; do sleep 5; doen
1