Last updated on April 20th, 2021
A quick post on how easy it is to copy files between kubernetes containers. How much you can make your life easier by using simple commands to copy data between containers and your computer.
Copy a file from kubernetes container to local machine
kubectl cp POD_NAME:/mnt/test.txt /tmp/test.txt
If you don’t create everything in the default namespace, you should also give namespace name with the -n parameter:
kubectl cp POD_NAME:/mnt/test.txt /tmp/test.txt -n NAMESPACE_NAME
Copy a file from local machine to kubernetes container
kubectl cp /tmp/test.txt POD_NAME:/mnt/test.txt
kubectl cp /tmp/test.txt POD_NAME:/mnt/test.txt -n NAMESPACE_NAME
This way you don’t need to do anything else. There is no need to add additional ports in the configuration, or install the ssh client /server.
If you have a POD that contains more than one container, you can select a specific container using -c:
kubectl cp /tmp/test.txt POD_NAME:/mnt/test.txt -c CONTAINER_NAME -n NAMESPACE_NAME
Kubernetes documentation can be found at https://kubernetes.io/docs/home/