Copy Files From Pod in Kubernetes

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Updated: July 12, 2024Published: July 12, 2024

The short answer

To copy a file or a directory from a Pod to a specified location on your local machine, you can use the kubectl cp command as follows:

Bash
$ kubectl cp [<namespace>/]<pod>:<source> <destination>

Where:

  • namespace is an optional namespace name.
  • pod is the name of the Pod you want to copy files from.
  • source is the absolute path to the file or directory you want to copy within the specified Pod.
  • destination is the relative or absolute path to the directory you want to copy the files into.

For example, this command will copy the nginx.conf file from the nginx Pod into the local config directory and renaming it nginx\_tmp.conf:

Bash
$ kubectl cp nginx:/etc/nginx/nginx.conf /home/johndoe/config/nginx_tmp.conf

And this command will copy the logs directory from the node Pod within the website namespace into the local /tmp/logs directory:

Bash
$ kubectl cp website/node:/app/logs /tmp/logs

Easily retrieve these commands using Warp's AI Command Suggestions feature

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering kubernetes copy from pod in the AI Command Suggestions will prompt a kubectl command that can then quickly be inserted into your shell by doing CMD+ENTER.

Written by
Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


Execute Commands in Pods With kubectl

Learn how to execute standalone commands and start interactive shell sessions in Pods using the `kubectl exec` command.

Get Kubernetes Secrets With kubectl

Learn how to list, describe, customize, sort and filter secrets in a Kubernetes cluster by name, type, namespace, label and more using the kubectl command.

Scale Deployments in Kubernetes

Learn how to manually and automatically scale a Deployment based on CPU usage in Kubernetes using the kubectl-scale and kubectl-autoscale commands.

Tail Logs In Kubernetes

Learn how to tail and monitor Kubernetes logs efficiently to debug, trace, and troubleshoot errors more easily using the kubectl command.

Forward Ports In Kubernetes

Learn how to forward the ports of Kubernetes resources such as Pods and Services using the kubectl port-forward command.

Get Context In Kubernetes

Learn how to get information about one or more contexts in Kubernetes using the kubectl command.

Delete Kubernetes Namespaces With kubectl

Learn how to delete one or more namespaces and their related resources in a Kubernetes cluster using the kubectl command.

Get Kubernetes Logs With kubectl

Learn how to get the logs of pods, containers, deployments, and services in Kubernetes using the kubectl command. Troubleshoot a cluster stuck in CrashloopBackoff, ImagePullBackoff, or Pending error states.

List Kubernetes Namespaces With kubectl

Learn how to list, describe, customize, sort and filter namespaces in a Kubernetes cluster by name, label, and more using the kubectl command.

Delete Kubernetes Deployments With kubectl

Learn how to delete Kubernetes deployments and its associated Pods using the kubectl command.

Deleting a Pod with `kubectl`

Learn how to delete a pod in Kubernetes using graceful shutdowns, labels, field selectors, and namespaces.

Create Kubernetes Namespace With `kubectl`

Learn how to create one or more Kubernetes namespaces with `kubectl`.