DEV Community

Cover image for How to Copy Files from Pod to Local with kubectl cp
CiCube for CICube

Posted on • Originally published at cicube.io

How to Copy Files from Pod to Local with kubectl cp


cicube.io

Introduction

And actually, managing files between your local system and Kubernetes pods is something you need to do quite often; during development, debugging, or troubleshooting, you most probably have to. The kubectl cp command enables you to transfer files into or out of the containers with no additional tools.

In this tutorial, we're going to cover the basic use of kubectl cp with some real-world examples.

What does kubectl cp do?

kubectl cp copies files and directories between your local machine and a Kubernetes pod. Aliases the Unix cp command to work with Kubernetes. In other words, the kubectl cp command allows you to copy files from a local system to a container and vice-versa.

Here is basic syntax:

kubectl cp <source> <destination> -n <namespace>
Enter fullscreen mode Exit fullscreen mode
  • <source>: The file or directory you would like to copy.
  • <destination> refers to the place the file is to be copied to.
  • -n <namespace>: Optional. The pod namespace.

Monitoring GitHub Actions Workflows

CICube is a GitHub Actions monitoring tool that provides you with detailed insights into your workflows to further optimize your CI/CD pipeline. With CICube, you will be able to track your workflow runs, understand where the bottlenecks are, and tease out the best from your build times. Go to cicube.io now and create a free account to better optimize your GitHub Actions workflows!

CICube GitHub Actions Workflow Duration Monitoring


Example 1: Copy a Configuration File to a Pod

If you are setting up a container and need to copy a configuration file from your local machine to the pod. Use the following command:

kubectl cp /path/on/local/config.yaml -n default my-app:/etc/config/config.yaml
Enter fullscreen mode Exit fullscreen mode

This copies the config.yaml file from your local system into the container running in the my-app pod under the /etc/config/ directory.

Example 2: Exporting Logs from a Pod to Local System

Suppose you are debugging an issue and want to download the log files from the pod. You can copy the log files from the pod to your local system as follows:

kubectl cp my-app:/var/log/app.log /path/on/local/system/app.log
Enter fullscreen mode Exit fullscreen mode

This command will copy the log file from a container to your local system for further analysis.

Example 3: Transferring Files Across Namespaces

If your pod is running in a different namespace, you can specify it using the -n flag. For example, to copy a file from a pod in the production namespace:

kubectl cp -n production web-server:/usr/share/nginx/html/index.html /backup/index.html
Enter fullscreen mode Exit fullscreen mode

This command fetches the index.html file from a web server running in the "production" namespace and saves it to the local machine.

Example 4: Copy Directory

You can also copy directories with kubectl cp. Copying a folder from your local machine to the container can be done by the following :

kubectl cp /local/directory -n staging backend-pod:/data/storage
Enter fullscreen mode Exit fullscreen mode

In the preceding example, a directory from the local system is being copied into the /data/storage directory inside the backend-pod.

Example 5: Backup Database Files from Pod to Local System

This is used to save database backup files that are inside a pod onto your local system.

kubectl cp db-pod:/var/lib/postgresql/data/backups/backup.sql /local/backup/backup.sql
Enter fullscreen mode Exit fullscreen mode

This will copy the backup.sql file from the PostgreSQL database container into your pod, to your local machine.

Common Errors and How to Fix Them

  • EOF Errors: EOF Errors Occasionally you may get an "unexpected EOF" error. In general, this could happen with network instability or incomplete file transfer. To avoid this, ensure your network is stable and your target container is running properly.

  • Permission Denied Errors: If you get a "Permission Denied" error, this most likely indicates that the pod is incorrectly set in terms of file permissions. Ensure you have read and/or write access to what you are trying to copy into a file or directory.

Key Considerations

Before trying to use kubectl cp, be sure the pod has installed basic tools like tar because kubectl cp has depended on it until recently to transfer the files. Also, remember to make sure your container's file paths are correctly set up at the outset to avoid unnecessary errors.

Conclusion

kubectl cp is quite a versatile command when it comes to transferring files between your local system and Kubernetes pods. Be it configuration files movement, logs pulls, or database backups, the command simply simplifies the process. The above examples have equipped you with handling a variety of use cases that generally involve file transfer in Kubernetes.

Top comments (2)

Collapse
 
subham_bose_30b profile image
Subham Bose

This is an insightful doc.

Collapse
 
cicubeteam profile image
CiCube

Thanks for your feedback Subham!