Transfer files between Kubernetes Pods and local system

1
15109
Transfer files between Kubernetes Pods and local system

Yet another DevOps tip for developers. In this article, I will show you how to transfer files between Kubernetes Pods and local system, which is often be your computer.

Transfer files between Kubernetes Pods

Let say you have a running pod named my-service, which you can verified by running following command:

$ kubectl get pods
NAME         READY   STATUS    RESTARTS   AGE
my-service   1/1     Running   0          6d23h

Transfer files syntax

The Kubernetes you need to use is kubectl cp. To syntax of the command as following:

$ kubectl help cp
Copy files and directories to and from containers.

Examples:
  # !!!Important Note!!!
  # Requires that the 'tar' binary is present in your container
  # image.  If 'tar' is not present, 'kubectl cp' will fail.
  #
  # For advanced use cases, such as symlinks, wildcard expansion or
  # file mode preservation consider using 'kubectl exec'.

  # Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace <some-namespace>
  tar cf - /tmp/foo | kubectl exec -i -n <some-namespace> <some-pod> -- tar xf - -C /tmp/bar

  # Copy /tmp/foo from a remote pod to /tmp/bar locally
  kubectl exec -n <some-namespace> <some-pod> -- tar cf - /tmp/foo | tar xf - -C /tmp/bar

  # Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the default namespace
  kubectl cp /tmp/foo_dir <some-pod>:/tmp/bar_dir

  # Copy /tmp/foo local file to /tmp/bar in a remote pod in a specific container
  kubectl cp /tmp/foo <some-pod>:/tmp/bar -c <specific-container>

  # Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace <some-namespace>
  kubectl cp /tmp/foo <some-namespace>/<some-pod>:/tmp/bar

  # Copy /tmp/foo from a remote pod to /tmp/bar locally
  kubectl cp <some-namespace>/<some-pod>:/tmp/foo /tmp/bar

Options:
  -c, --container='': Container name. If omitted, the first container in the pod will be chosen
      --no-preserve=false: The copied file/directory's ownership and permissions will not be preserved in the container

Usage:
  kubectl cp <file-spec-src> <file-spec-dest> [options]

Following are some usage examples of the command.

Copy file from Pod to local

Assuming you have only one container inside the my-service pod, and you want to get this file /home/data.csv to your computer, you need to execute following command to get the file from the pod.

$ kubectl cp my-service:/home/data.csv .

The command will get the file from container inside my-service pod to current directory where you execute the command.

Copy file from local to Pod

Similarly, in order to transfer a local file to a pod in Kubernetes, you will issue this command:

$ kubectl cp ./data.txt my-service:/home/

After that, the data.txt file at current directory will be transfered into my-service pod and put inside /home directory of the first container.

Specify namespace while copying

By default, the command kubectl cp will locate the my-service pod in current namespace. However, if you want to specify copying in and out to a pod in different namespace, then you need to specify the namespace before pod name. For examples:

$ kubectl cp my-namespace/my-service:/home/data.csv .

$ kubectl cp ./data.txt my-namespace/my-service:/home/

Specify a container to copy

In some rare cases, you will have several containers running inside one pod, and for that, in order to transfer files to that pod in Kubernetes, what you need to do is to provide that option -c, --container , which is the container name. If this option is omitted, the first container defined in the spec will be the target destination.

$ kubectl cp my-namespace/my-service:/home/data.csv . -c service

$ kubectl cp ./data.txt my-namespace/my-service:/home/ -c service

Conclusion

The command kubectl cp is very useful and you will need it to transfer files between Kubernetes pods and local system. Some use-cases of this might be when you want to debug some logs or content files output locally inside the container, or you want to get the database dumped of the container…etc.

For any moment that you forget how to use it, issue kubectl help cp, it will show everything that you need.