Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy a file into kubernetes pod without using kubectl cp

Tags:

kubernetes

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**
like image 324
prashant Avatar asked Oct 16 '22 15:10

prashant


1 Answers

If the source file is a simple text file, here's my trick:

#!/usr/bin/env bash

function copy_text_to_pod() {
  namespace=$1
  pod_name=$2
  src_filename=$3
  dest_filename=$4

  base64_text=`cat $src_filename | base64`
  kubectl exec -n $namespace $pod_name -- bash -c "echo \"$base64_text\" | base64 -d > $dest_filename"
}

copy_text_to_pod my-namespace my-pod-name /path/of/source/file /path/of/target/file

Maybe base64 is not necessary. I put it here in case there is some special character in the source file.

like image 72
Trent Zhou Avatar answered Oct 21 '22 04:10

Trent Zhou