The kubectl cp
command is used to copy files and directories between your local filesystem and a container in a Kubernetes cluster. This functionality is particularly useful for transferring logs, downloading configuration files, or uploading data to a running application within a pod.
Here are several examples demonstrating how to use kubectl cp
:
Example 1: Copy a file from a Kubernetes pod to your local machine:
kubectl cp
This command copies a file named file
from the pod named pod-name
to your local filesystem. You can verify its success by checking if the local file exists after running the command.
Example 2: Copy a directory from your local machine to a Kubernetes pod:
kubectl cp /path/to/local/directory
This copies the entire directory and its contents from your local machine to the specified directory within the pod. You can verify by logging into the pod and checking if the directory and its contents are present.
Example 3: Copy a file from a Kubernetes pod to another location within the same pod:
kubectl cp
This command is useful for moving files within the pod itself. Verification involves checking the destination path within the pod to ensure the file has been moved successfully.
Example 4: Copy files from multiple pods matching a label selector to your local machine:
kubectl cp -l
Here, files from all pods matching the specified label selector are copied to a local directory. Verification includes ensuring all expected files from the matching pods are present locally.
Example 5: Copy all files from a Kubernetes pod to your local machine:
kubectl cp
This command copies all files and directories from the root of the pod to a specified local directory. Verification involves checking if the local directory contains all expected files and directories from the pod.
Example 6: Copy files from your local machine to a specific directory within a pod:
kubectl cp /path/to/local/file
Copies a file from your local machine to a specific directory within a pod. Verify by logging into the pod and checking if the file is present in the target directory.
Example 7: Copy a file from a specific container within a multi-container pod:
kubectl cp
Copies a file from a specific container within a multi-container pod to your local filesystem. Verification involves checking if the local file exists after running the command.
Example 8: Copy a directory from a specific container within a pod to your local machine:
kubectl cp
Copies an entire directory from a specific container within a pod to your local filesystem. Verification includes checking if the local directory contains all expected files and subdirectories.
Example 9: Copy a file and preserve its permissions and timestamps:
kubectl cp --preserve=true
This command preserves the permissions and timestamps of the file being copied. Verification involves checking if the local file maintains the same permissions and timestamps as the original file within the pod.
Example 10: Copy a file in verbose mode for detailed output:
kubectl cp --verbose
Copies a file with verbose mode enabled, providing detailed output about the copy process. Verification involves reviewing the verbose output to ensure the file was copied correctly.
Also check similar articles.
Running a Kubernetes API Proxy with kubectl proxy
Port Forwarding in Kubernetes with kubectl port-forward
Execute Commands in Kubernetes Pods with kubectl exec
Attach to Running Containers with kubectl attach
Retrieve Container Logs Using kubectl logs
Discussion about this post