Table of Contents
Kubectl is a powerful command-line tool that enables developers to interact with Kubernetes clusters. One of the most useful features of Kubectl is the kubectl cp command, which allows you to copy files and directories to and from Kubernetes containers. In this article, we will explore the kubectl cp command and provide examples of how it can be used to copy files to and from Kubernetes containers.
General syntax of kubectl cp is given below
kubectl cp <file-spec-src> <file-spec-dest> [options]
How to copy from local to pod on a particular namespace
A file from local machine, or client machine can be copied to the pod. By default, if we don't specify any namespace, it will be copied to the pod ( if present ) in the default namespace
We can use the below syntax for copying a file from local to a pod on a particular namespace
kubectl cp <file name> <namespace>/<pod>:<destination file-path on pod>/<filename>
kubectl cp crednt.tools regression/regression2300:/tmp/crednt.tools
The above command will copy a file crednt.tools to the pod regression2300 in namespace regression to path /tmp with file name as crednt.tools
How to copy from local to a container (pod) on a particular namespace
In practical use, we will having multi container pods. In multi-container pod, all the operation unless a container name is specified will happen to the main container. Same will be case while copying the file.
We can use the below syntax for copying a file from local to a container in a multi-container pod
kubectl cp <file name > <namespace>/<podname>:/<destination file-path on pod>/<filename> -c <container>
kubectl cp crednt.tools regression-230-0/regression2300:/tmp/crednt.tools -c security
The above command will copy a file crednt.tools to a container security on a pod regression2300 belonging to the namespace regression-230-0 to the path /tmp with file name crednt.tools
How to copy from pod to local on a particular namespace
We can use the below syntax to copy a file from a pod to the local machine
kubectl cp <namespace>/<pod>:<file name> <destination path on local>/<file name>
kubectl cp regression/regression2300:/tmp/pcfstate /root/kubectlcp/pcf
The above command will copy a file pcfstate from path /tmp of the pod regression2300 to the local machine on the path /root/kubectlcp/ with file name as pcf. The pod belongs to namespace regression
How to copy from container (pod) to local on a particular namespace
We can use the below syntax to copy a file from a container on a multi container pod using the below syntax
kubectl cp <namespace>/<pod>:<filename> <destination path on container>/< file name> -c <container>
kubectl cp regression-230-0/regression2300:/tmp/supervisor.pid /root/kubectlcp/pid -c security
The above command copy a file supervisor.pid from path /tmp from the container security belonging to a multi-container pod regression2300 which is part of namespace 230-0 to the local machine path /root/kubectlcp with file name pid
How to copy directories to and from containers/pods on kubernetes
We can copy directories recursively to and from containers, using the above explained syntax and commands. Only difference being, instead of giving file name, we will have to specify directory name.
In the earlier version of kubectl, we had to give -r option for copying recursively. But in the latest version ( tested from kubernetes 1.21 and above) , we don't need to specify the -r flag.
Below syntax can be used to copy a directory from a pod (container) to local
kubectl cp <namespace>/<pod-name>:<directory-name > <destination local path>/<directory-name> -c <container-name>
Below syntax can be used to copy a directory from a local to a pod ( container)
kubectl cp <local path>/<directory-name> <namespace>/<pod-name>:<directory-name> -c <container-name>