34

I want to copy files recursively to a Kubernetes pod

I tried kubectl cp -r

I got: error: unknown shorthand flag: 'r' in -r

What are the best ways to transfer whole directories recursively into a pod.

030
  • 13,383
  • 17
  • 76
  • 178
David West
  • 1,533
  • 3
  • 18
  • 25

4 Answers4

39

kubectl cp by default does recursive copies when given a directory, although it seems to be picky about trailing slashes. If foo/bar is the directory you'd like to copy, simply run

kubectl cp /path/to/foo/bar <pod-id>:/path/in/container/foo/
Xiong Chiamiov
  • 2,841
  • 1
  • 10
  • 30
1

For Windows to Linux, the following commands worked for me (this will not accept backslash, only forward-slash worked):

kubectl cp C:/myfiles/azureCLI/WantToCopy my-pod:/mnt/data/something/
Eduardo Baitello
  • 981
  • 8
  • 26
yuvraj
  • 11
  • 2
0

-r is not needed to copy the folder. The below command will work. I tested this on kubectl version 1.21.

kubectl cp <pod_id>:/home/test_folder test_folder_localhost
Eduardo Baitello
  • 981
  • 8
  • 26
Nikhil
  • 101
0

According to the help menu, the recursive option does not seem to exist.

user@localhost ~ $ kubectl cp --help
Copy files and directories to and from containers.

Examples:
  # !!!Important Note!!!
  # Requires that the 'tar' binary is present in your container
  # image.  If 'tar' is not present, 'kubectl cp' will fail.

  # Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the default namespace
  kubectl cp /tmp/foo_dir <some-pod>:/tmp/bar_dir

  # Copy /tmp/foo local file to /tmp/bar in a remote pod in a specific container
  kubectl cp /tmp/foo <some-pod>:/tmp/bar -c <specific-container>

  # Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace <some-namespace>
  kubectl cp /tmp/foo <some-namespace>/<some-pod>:/tmp/bar

  # Copy /tmp/foo from a remote pod to /tmp/bar locally
  kubectl cp <some-namespace>/<some-pod>:/tmp/foo /tmp/bar

Options:
  -c, --container='': Container name. If omitted, the first container in the pod will be chosen

Usage:
  kubectl cp <file-spec-src> <file-spec-dest> [options]

Use "kubectl options" for a list of global command-line options (applies to all commands).

In order to copy files recursively, all files could be put in a directory and when this folder is copied to the pod, all files were copied:

030
  • 13,383
  • 17
  • 76
  • 178