In this article, we will look at theĀ kubectl cp
Ā command, its definition, and syntax, before showing how to use it to perform practical everyday tasks, such as copying files from a pod to your local system. We will also take a look at some common errors associated withĀ kubectl cp
, explaining the common causes and how to go about troubleshooting them.
We will cover:
TheĀ kubectl cp
Ā is a command-line utility in Kubernetes used to copy files and directories to and from a container in a Kubernetes pod. It allows you to transfer files between your local system and a running container within a pod without needing to set up additional tools or services.
kubectl cp
is only available for pods running a compatible version of the K8S API server (v1.14+). It can be a convenient way to transfer files in and out of containers when needed for troubleshooting, debugging, or data exchange.
Command syntax
The example usage of the kubectl cp
Ā command is shown below:
kubectl cp <source> <destination> -n <namespace> <pod-name>:<path>
<source>
: The source file or directory on your local system or in the container (useĀ.
Ā to represent the current directory in the container).<destination>
: The destination on your local system or in the container.-n <namespace>
: Specifies the namespace in which the pod is running (optional).<pod-name>
: The name of the pod where you want to copy files.<path>
: The path within the container where you want to copy files or directories.
Let’s look at some use case examples for the kubectl cp
command.
Example 1 ā Copying files from pod to local system
To copy files from a pod to the local system use:
kubectl cp -n <namespace> <pod-name>:<path> <destination-on-local-system>
For example, if you have a pod named āmy-podā in the ādefaultā namespace and you want to copy a file named āfile.txtā from the pod to your local system, you can use the following command:
kubectl cp -n default my-pod:/path/to/file.txt .
Note theĀ .
represents the current directory.
Example 2 ā Copying files from local system to pod
Now, to copy files from the local system to a pod:
kubectl cp -n <namespace> <source> <pod-name>:<path>
For example, if you have a pod named āmy-podā in the ādefaultā namespace and you want to copy a file named āfile.txtā from your local system to the pod, you can use the following command:
kubectl cp -n default . my-pod:/path/to/file.txt
Example 3 ā Copying files from a pod to a pod
To copy files from one pod to another pod in K8S using theĀ kubectl cp
command, you will need to copy the files to your local system as an intermediate step and then from your local system to the destination pod.
You can use the two examples above to achieve this.
Example 4 ā Copying files from a pod in a specific namespace to local system
The -n
option in theĀ kubectl cp
command is used to point out a specific namespace.
kubectl cp -n <namespace> <pod-name>:<path> <destination-on-local-system>
For example, if you have a pod named āmy-podā in the ātestā namespace and you want to copy a file named āfile.txtā from the pod to your local system, you can use the following command:
kubectl cp -n test my-pod:/path/to/file.txt .
Example 4 ā Copying directories
Instead of specifying files to copy, you can also specify directories. For example, to copy a directory from your local system to a pod:
kubectl cp /path/to/local/directory <namespace>/<pod-name>:/path/in/pod
Example 5 ā Copying from a specific container
You can also useĀ kubectl cp
Ā to copy files between containers within the same pod.
kubectl cp -n <namespace> <pod-name>:<source-container>:<path> <destination-container>:<destination-path>
kubectl cp
Ā command errors can occur for various reasons, and troubleshooting them depends on the specific error message you encounter. Some common errors are described below.
Kubectl cp unexpected EOF error
The āunexpected EOFā error when usingĀ kubectl cp
Ā typically indicates that theĀ kubectl cp
Ā command received an incomplete or unexpected response from the Kubernetes cluster. This can occur for various reasons but is commonly caused by network issues or unexpected interruptions in the transfer process.
How to fix it?
To resolve the issue, check the networking is configured correctly, that the cluster is healthy and responding correctly, and that the container and pod you are targeting are responding as expected. Also, check you have enough disk space available for the file transfer on the source and target.
Kubectl cp terminated with exit code 126
The āterminated with exit code 126ā error when usingĀ kubectl cp
Ā usually indicates that the command failed because it was unable to find or execute a specific executable or script in the target container. Exit code 126 typically means “Permission denied”.
How to fix it?
To resolve the issue, check you have permissions on the file youāre trying to copy, that it is readable, or that the script youāre trying to run is executable. If the file or command youāre trying to access is not found in the specified path, youāll encounter this error.
- Double-check the paths and arguments in your
kubectl cp
Ā command. - Verify that the file or command exists in the container.
- Check the container is running and is responding correctly.
- Also, check that the file youāre trying to copy or execute is in a compatible format with the containerās operating system. For example, a binary compiled for Linux wonāt work in a Windows container.
TheĀ kubectl cp
Ā command can be used to transfer files and directories between your local machine and pods, or between pods running on your K8S cluster.
We encourage you to also check out howĀ SpaceliftĀ helps you manage the complexities and compliance challenges of using Kubernetes. Anything that can be run via kubectl can be run within a Spacelift stack. Find out more aboutĀ how Spacelift works with Kubernetes, and get started on your journey byĀ creating a free trial account.
Manage Kubernetes Easier and Faster
Spacelift allows you to automate, audit, secure, and continuously deliver your infrastructure. It helps overcome common state management issues and adds several must-have features for infrastructure management.