debug
kubectl cp
Copies files and directories between your local filesystem and a container in a pod. Uses tar under the hood, so the target container must have a working tar binary on its PATH.
kubectl cp <src> <dst> [-c CONTAINER] where src/dst may be POD:PATH Common flags
| Flag | Purpose |
|---|---|
| -c, --container | Container inside a multi-container pod. |
| -n, --namespace | Namespace of the pod (or use ns/pod:path form). |
| --retries | Number of retry attempts on transient failure (default 0). |
| --no-preserve | Do not preserve file mode/ownership on the destination. |
Examples
kubectl cp ./local.conf web-abc:/etc/app/local.conf Push a file from your machine into the pod.
kubectl cp web-abc:/var/log/app.log ./app.log Pull a log file out of the pod.
kubectl cp ./dir web-abc:/tmp/dir -c sidecar Copy a directory into a specific sidecar container.
Gotcha
Fails silently on distroless/scratch images that lack tar. Absolute paths on Windows may need quoting to avoid drive-letter confusion.
Related commands
kubectl exec
Executes a command inside a running container. Use -it for an interactive TTY shell; omit them for a one-shot command whose stdout you want to capture for scripts.
kubectl logs
Prints container logs from a pod, deployment, or other workload. Supports streaming, previous-instance recovery after crashes, and selecting a specific container in multi-container pods.
kubectl port-forward
Forwards one or more local ports to a pod, service, or deployment inside the cluster via the API server. Ideal for reaching internal services (databases, admin UIs) from your laptop without exposing them publicly.
kubectl proxy
Runs a local HTTP proxy that authenticates as your kubeconfig user and forwards requests to the Kubernetes API server. Useful for exploring the raw API with curl or serving the dashboard behind localhost auth.