kubectl cp - Copy files and directories to and from containers
kubectl cp <file-spec-src> <file-spec-dest> [-c CONTAINER]
file-spec: [[namespace/]pod:]file/path
Options you will use
-c, --container=NAME- the container (default: the first one, or the kubectl.kubernetes.io/default-container annotation)
--no-preserve- do not keep ownership and permissions on the copy in the container
--retries=N- retry a broken transfer from the pod N times
Examples
$ kubectl cp dev/api-7d9f:/var/log/app.log ./app.logpod -> local: give the destination a FILE name
$ kubectl cp ./fix.conf dev/api-7d9f:/tmp/fix.conf -c apilocal -> pod, one container of several
$ kubectl exec api-7d9f -- cat /etc/app/app.yaml > app.yamlthe fallback when cp cannot work (symlinks, no tar)
Gotchas
- cp is tar over exec:
tar cf -in the container (hence "tar: Removing leading `/' from member names" on stderr - harmless) and needs a tar binary in the image. Distroless images have none: exec: "tar": executable file not found in $PATH. - Symlinks are skipped with a warning. ConfigMap and Secret volume files ARE symlinks (to ..data/KEY) - copy them with exec cat instead.
- Copying a single file to an existing local directory fails ("is a directory"): name the target file.
- Needs RBAC create on pods/exec, like kubectl exec.
Try kubectl cp in a real terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.