LinuxCommandLibrary

kubectl-cp

Copy files between local and Pods

TLDR

Copy a local file or directory to a pod

$ kubectl cp [path/to/local_file] [pod_name]:/[path/to/file_in_pod]
copy

Copy a local file or directory to a pod in a specific namespace
$ kubectl cp [path/to/local_file] [namespace]/[pod_name]:/[path/to/file_in_pod]
copy

Copy a file or directory from a pod to the local machine
$ kubectl cp [namespace]/[pod_name]:/[path/to/file_in_pod] [path/to/local_file]
copy

Copy a file or directory to a specific container in a pod
$ kubectl cp [path/to/local_file] [pod_name]:/[path/to/file_in_pod] [[-c|--container]] [container_name]
copy

Copy a file or directory to a pod without preserving ownership and permissions
$ kubectl cp [path/to/local_file] [namespace]/[pod_name]:/[path/to/file_in_pod] --no-preserve
copy

Copy a local file or directory to a pod with retries on failure
$ kubectl cp [path/to/local_file] [pod_name]:/[path/to/file_in_pod] --retries [3]
copy

SYNOPSIS

kubectl cp <file-spec-src> <file-spec-dest> [-c <container>] [-n <namespace>] [--retries <num>] [-q] [-v=<level>]

PARAMETERS

-c, --container=<name>
    Container name. Defaults to first container in pod

-n, --namespace=<name>
    Namespace for the operation

-q, --quiet
    Suppress error output

--retries=<num>
    Number of retries before failure (default 5)

-v, --v=<level>
    Log verbosity level

-a, --all-containers
    Copy to/from all containers (experimental in older versions)

DESCRIPTION

The kubectl cp command copies files and directories between the local filesystem and a running container in a Kubernetes pod, or between containers in different pods. It streams data using tar archives over the Kubernetes API server, avoiding the need for external tools like SCP or RSYNC.

Source and destination paths use a special format: for pods, it's <POD>[:<CONTAINER>]:<PATH>; for local files, simply <LOCAL-PATH>. Direction is determined by path order: local-to-pod, pod-to-local, or pod-to-pod.

It supports recursive directory copies and preserves file permissions and timestamps where possible. The target pod must be running, and the user needs read/write permissions via RBAC. Large transfers can be slow due to API limits; consider using kubectl exec with tar for alternatives. No built-in progress bar or resume support.

CAVEATS

Pod/container must be running; no symlink following; API server limits throughput; no progress indicator or partial resume; RBAC required for exec-like access.

PATH FORMAT

Pod: <pod-name>[:<container>]:<path>
Local: <path>

EXAMPLES

kubectl cp foo.txt mypod:/tmp/
kubectl cp mypod:/tmp/ ./localdir
kubectl cp pod1:/a pod2:/b -c cont1 -c cont2

HISTORY

Introduced in Kubernetes v1.6 (March 2017) for basic local-pod copies. Enhanced in v1.7+ for pod-to-pod transfers and recursion. Retries added in v1.23+ for reliability.

SEE ALSO

kubectl(1), kubectl exec(1), tar(1), rsync(1)

Copied to clipboard