Copying Files from a Kubernetes Pod to the Local Machine
Copying files from a Kubernetes pod to the local machine is a common task when working with Kubernetes. This can be useful for retrieving log files, configuration files, or any other relevant data stored within the pod.
To copy a file from a Kubernetes pod to the local machine, you can use the kubectl cp
command with the following syntax:
kubectl cp <namespace>/<pod>:<source_file> <dest_file>
Here, <namespace>
is the name of the Kubernetes namespace where the pod is running, <pod>
is the name of the pod, <source_file>
is the path to the file within the pod, and <dest_file>
is the destination file path on the local machine.
For example, let's say you have a pod named my-app
running in the default
namespace, and you want to copy a log file named app.log
from the pod to the current directory on your local machine. You would run the following command:
kubectl cp default/my-app:app.log ./app.log
This will copy the app.log
file from the my-app
pod in the default
namespace to the current directory on your local machine, naming the file app.log
.
If the file you want to copy is located in a subdirectory within the pod, you can specify the full path to the file in the <source_file>
parameter. For instance, to copy a file named config.yaml
from the /app/config
directory within the pod, you would run:
kubectl cp default/my-app:/app/config/config.yaml ./config.yaml
Remember that the kubectl cp
command can only copy files from a running pod. If the pod is not running or the specified file does not exist, the command will fail.
graph TD
A[Local Machine] -- "kubectl cp" --> B[Kubernetes Pod]
Command |
Description |
kubectl cp <namespace>/<pod>:<source_file> <dest_file> |
Copy a file from a pod to the local filesystem. |