kubectl cp Copy Files to and from Pods
Copy files between local machine and Kubernetes pods with kubectl cp. Supports containers, namespaces, tar-based transfer, and common troubleshooting.
π‘ Quick Answer:
kubectl cp <pod>:/path /local/pathcopies files from pod to local.kubectl cp /local/path <pod>:/pathcopies local to pod. Requirestarbinary in the container.
The Problem
You need to:
- Extract log files or heap dumps from a running pod
- Upload configuration or debug tools into a container
- Copy database exports or test fixtures
- Collect crash artifacts before pod restart
The Solution
Copy from Pod to Local
# Basic syntax
kubectl cp <namespace>/<pod>:<path> <local-path>
# Copy a file
kubectl cp production/myapp-6f7b9c4d5-abc12:/var/log/app.log ./app.log
# Copy a directory
kubectl cp default/myapp-pod:/app/data ./local-data/
# From specific container (multi-container pod)
kubectl cp myapp-pod:/tmp/dump.hprof ./dump.hprof -c sidecarCopy from Local to Pod
# Upload a file
kubectl cp ./config.yaml default/myapp-pod:/etc/app/config.yaml
# Upload a directory
kubectl cp ./fixtures/ default/myapp-pod:/app/test-data/
# To specific namespace and container
kubectl cp ./debug-tools.sh production/myapp-pod:/tmp/ -c mainSpecify Namespace
# Using -n flag
kubectl cp -n production myapp-pod:/data/export.sql ./export.sql
# Using namespace/pod syntax
kubectl cp production/myapp-pod:/data/export.sql ./export.sqlArchitecture
graph LR
A[Local Machine] -->|kubectl cp local pod:/path| B[kubectl]
B -->|tar stream via exec| C[Pod Container]
C -->|tar extracts| D[Container Filesystem]
D -->|tar stream via exec| E[kubectl]
E -->|extracts locally| ACommon Patterns
# Copy logs before pod is evicted
kubectl cp myapp-pod:/var/log/ ./pod-logs/ --retries=3
# Extract heap dump for analysis
kubectl cp myapp-pod:/tmp/heap-dump-$(date +%s).hprof ./dumps/
# Upload hotfix jar (emergency only!)
kubectl cp ./hotfix.jar myapp-pod:/app/lib/hotfix.jar
# Copy from init container output
kubectl cp myapp-pod:/shared-data/init-output.json ./init-output.json -c init-container
# Compress before copy (faster for large files)
kubectl exec myapp-pod -- tar czf /tmp/logs.tar.gz /var/log/app/
kubectl cp myapp-pod:/tmp/logs.tar.gz ./logs.tar.gzAlternative: Use exec + cat for Small Files
# Faster for single small files (no tar overhead)
kubectl exec myapp-pod -- cat /etc/app/config.yaml > config.yaml
# Binary files
kubectl exec myapp-pod -- cat /tmp/dump.bin | base64 -d > dump.binCommon Issues
| Issue | Cause | Fix |
|---|---|---|
| βtar: not foundβ | Container has no tar binary | Use exec + cat or add tar to image |
| βerror: unexpected EOFβ | Pod restarted during copy | Retry; use compression for large files |
| Permission denied | Running as non-root, target path restricted | Copy to /tmp first |
| Symlinks not followed | tar default behavior | Use exec + tar -h to follow links |
| Slow transfer | Large files uncompressed | Compress first with exec tar czf |
| Wrong container | Multi-container pod | Specify -c container-name |
Best Practices
- Prefer ephemeral debug containers for investigation β donβt add tools to production images
- Compress large transfers β
exec tar czfthencpthe archive - Never cp secrets to local disk β use
kubectl get secretwith proper RBAC - Use
/tmpas staging β guaranteed writable in most containers - Consider rsync alternatives β
kubectl exec rsyncfor large directory syncs
Key Takeaways
kubectl cpusestarinternally viakubectl execβ container needstarbinary- Direction:
kubectl cp <source> <destination>β pod paths usepod:/pathsyntax - Use
-cflag for multi-container pods to target the right container - For large files, compress first then copy β much faster over the network
- Not a replacement for proper logging/monitoring β use for ad-hoc debugging only

Recommended
Kubernetes Recipes β The Complete Book100+ production-ready patterns with detailed explanations, best practices, and copy-paste YAML. Everything in one place.
Get the Book βLearn by Doing
CopyPasteLearn β Hands-on Cloud & DevOps CoursesMaster Kubernetes, Ansible, Terraform, and MLOps with interactive, copy-paste-run lessons. Start free.
Browse Courses β