0
0
KubernetesHow-ToBeginner · 3 min read

How to Copy File from Pod in Kubernetes Using kubectl cp

Use the kubectl cp command to copy files from a Kubernetes pod to your local machine. The syntax is kubectl cp <pod-name>:<path-in-pod> <local-path>, which copies the file or directory from the pod to your computer.
📐

Syntax

The kubectl cp command copies files between your local machine and a Kubernetes pod.

  • kubectl cp <pod-name>:<path-in-pod> <local-path> copies from pod to local.
  • pod-name is the name of the pod.
  • path-in-pod is the full path of the file or directory inside the pod.
  • local-path is the destination path on your local machine.
bash
kubectl cp <pod-name>:<path-in-pod> <local-path>
💻

Example

This example copies the file /var/log/app.log from the pod named myapp-pod to the current directory on your local machine.

bash
kubectl cp myapp-pod:/var/log/app.log ./app.log
Output
File copied successfully to ./app.log
⚠️

Common Pitfalls

  • Using the wrong pod name causes an error: error: pod not found.
  • Incorrect file path inside the pod results in no such file or directory.
  • Not having proper permissions to access the pod or file can cause permission denied errors.
  • Copying directories requires the path to be a directory and destination to be a directory or new directory name.
bash
kubectl cp wrong-pod:/path/file.txt ./file.txt
# Error: pod not found

kubectl cp myapp-pod:/wrong/path/file.txt ./file.txt
# Error: no such file or directory

# Correct usage example:
kubectl cp myapp-pod:/var/log/app.log ./app.log
📊

Quick Reference

CommandDescription
kubectl cp : Copy file from pod to local machine
kubectl cp :Copy file from local machine to pod
kubectl get podsList pods to find pod name
kubectl exec -it -- ls Check files inside pod

Key Takeaways

Use kubectl cp with pod name and file path to copy files from pod to local machine.
Ensure the pod name and file path inside the pod are correct to avoid errors.
You need proper permissions to access the pod and files.
You can also copy files from local to pod by reversing the source and destination.
Use kubectl get pods to find pod names before copying files.