Sign In

Curriculum 12: Pod Management

kubectl cp

10 min · 25 XP

Copying Files with kubectl cp

The kubectl cp command copies files and directories between your local machine and containers running in pods. It works similarly to scp but operates through the Kubernetes API.

Copying Files to a Pod

# Copy a local file into a pod
kubectl cp ./config.json my-pod:/app/config.json

# Copy an entire directory
kubectl cp ./data/ my-pod:/tmp/data/

Copying Files from a Pod

# Copy a file from a pod to your local machine
kubectl cp my-pod:/var/log/app.log ./app.log

# Copy a directory from a pod
kubectl cp my-pod:/app/exports/ ./local-exports/

Working with Namespaces and Multi-Container Pods

Specify the namespace and target container when needed:

# With namespace
kubectl cp my-pod:/data/dump.sql ./dump.sql -n production

# With a specific container in a multi-container pod
kubectl cp my-pod:/logs/error.log ./error.log -c sidecar

Tar Requirement

The kubectl cp command requires tar to be installed inside the container. It works by running tar in the container to stream files over the API connection. If the container image does not include tar (common with minimal or distroless images), the command will fail:

error: tar: not found

In that case, use kubectl exec with shell redirection as a workaround:

# Alternative when tar is not available
kubectl exec my-pod -- cat /app/output.log > ./output.log

Limitations

  • Symbolic links may not be preserved correctly.
  • File ownership and permissions can change during the copy.
  • Large file transfers can be slow because they route through the API server.
  • The command does not support glob patterns or wildcards.

For frequent or large file transfers, consider mounting a shared volume instead.