Use “kubectl cp” to Copy Files to and from Kubernetes Pods

Tyler Tyler (285)
Total time: 10 minutes 
Updated: July 22nd, 2020

If you’re using Kubernetes, you may find the need to move files to and from containers running on pods. Before the days of containerization, we would use a tool like SCP (secure copy protocol) to move files to and from remote machines. Fortunately, Kubernetes provides a similar tool.

In this guide, you’ll learn how to use kubectl cp to move files to and from Kubernetes pods.

Posted in these interests:

kubernetes
PRIMARY
7 guides

From the docs, here’s the basic usage:

kubectl cp  

The kubectl cp command takes two parameters. The first is the source; the second is the destination. As with scp, either parameter (source or destination files) can refer to a local or remote file.

Before we begin

We need to make sure our kubernetes client is authenticated with a cluster. There are many ways to authenticate, but authentication is outside the scope of this guide.

Second, we’ll need to identify the name of a pod (or pods) that we want to work with. We’ll need to use kubectl get pod to find the name of the pod(s), and we’ll use these names in the following sections.

Copy file from local machine to pod

Suppose we want to move a file from our local machine to a pod.

kubectl cp /path/to/file my-pod:/path/to/file

In the above example, we copied a local file /path/to/file to a pod named, my-pod. We’ve specified an identical path on the pod to copy the file. Notice that we used an absolute path in both cases. We can also use relative paths:

kubectl cp my-file my-pod:my-file

In this example, we’ve specified a relative path to a local file, and a relative path on the pod. One key difference between kubectl cp and a tool like scp is that with kubernetes, the file is copied relative to the working directory, not the home directory.

Copy file from a pod to a pod

Similarly, we can copy a file from one pod to another. The same rules for absolute and relative paths apply.

kubectl cp pod-1:my-file pod-2:my-file

Copy file from pod to your local machine

As you might have guessed, you simply swap the parameters from the first example.

kubectl cp my-pod:my-file my-file

This will copy my-file from the working directory of your pod to your current directory.

Copying directories

When using scp to copy directories, we’re accustomed to adding the -r (recursive) flag. With kubectl cp this is implied. You use the exact same syntax to copy directories is you would files.

kubectl cp my-dir my-pod:my-dir

Specifying a container

In some cases, you may be running multiple containers on a pod. In which case, you’ll need to specify the container. You can do so with -c, which is consistent with most other kubectl commands.

kubectl cp my-file my-pod:my-file -c my-container-name

Use “kubectl cp” to Copy Files to and from Kubernetes Pods

Tyler Tyler (285)
Total time: 10 minutes 
Updated: July 22nd, 2020

If you’re using Kubernetes, you may find the need to move files to and from containers running on pods. Before the days of containerization, we would use a tool like SCP (secure copy protocol) to move files to and from remote machines. Fortunately, Kubernetes provides a similar tool.

In this guide, you’ll learn how to use kubectl cp to move files to and from Kubernetes pods.

Posted in these interests:

kubernetes
PRIMARY
7 guides

From the docs, here’s the basic usage:

kubectl cp  

The kubectl cp command takes two parameters. The first is the source; the second is the destination. As with scp, either parameter (source or destination files) can refer to a local or remote file.

Before we begin

We need to make sure our kubernetes client is authenticated with a cluster. There are many ways to authenticate, but authentication is outside the scope of this guide.

Second, we’ll need to identify the name of a pod (or pods) that we want to work with. We’ll need to use kubectl get pod to find the name of the pod(s), and we’ll use these names in the following sections.

Copy file from local machine to pod

Suppose we want to move a file from our local machine to a pod.

kubectl cp /path/to/file my-pod:/path/to/file

In the above example, we copied a local file /path/to/file to a pod named, my-pod. We’ve specified an identical path on the pod to copy the file. Notice that we used an absolute path in both cases. We can also use relative paths:

kubectl cp my-file my-pod:my-file

In this example, we’ve specified a relative path to a local file, and a relative path on the pod. One key difference between kubectl cp and a tool like scp is that with kubernetes, the file is copied relative to the working directory, not the home directory.

Copy file from a pod to a pod

Similarly, we can copy a file from one pod to another. The same rules for absolute and relative paths apply.

kubectl cp pod-1:my-file pod-2:my-file

Copy file from pod to your local machine

As you might have guessed, you simply swap the parameters from the first example.

kubectl cp my-pod:my-file my-file

This will copy my-file from the working directory of your pod to your current directory.

Copying directories

When using scp to copy directories, we’re accustomed to adding the -r (recursive) flag. With kubectl cp this is implied. You use the exact same syntax to copy directories is you would files.

kubectl cp my-dir my-pod:my-dir

Specifying a container

In some cases, you may be running multiple containers on a pod. In which case, you’ll need to specify the container. You can do so with -c, which is consistent with most other kubectl commands.

kubectl cp my-file my-pod:my-file -c my-container-name

Use “kubectl cp” to Copy Files to and from Kubernetes Pods

Tyler Tyler (285)
Total time: 10 minutes 
Updated: July 22nd, 2020

If you’re using Kubernetes, you may find the need to move files to and from containers running on pods. Before the days of containerization, we would use a tool like SCP (secure copy protocol) to move files to and from remote machines. Fortunately, Kubernetes provides a similar tool.

In this guide, you’ll learn how to use kubectl cp to move files to and from Kubernetes pods.

Posted in these interests:

kubernetes
PRIMARY
7 guides

From the docs, here’s the basic usage:

kubectl cp  

The kubectl cp command takes two parameters. The first is the source; the second is the destination. As with scp, either parameter (source or destination files) can refer to a local or remote file.

Before we begin

We need to make sure our kubernetes client is authenticated with a cluster. There are many ways to authenticate, but authentication is outside the scope of this guide.

Second, we’ll need to identify the name of a pod (or pods) that we want to work with. We’ll need to use kubectl get pod to find the name of the pod(s), and we’ll use these names in the following sections.

Copy file from local machine to pod

Suppose we want to move a file from our local machine to a pod.

kubectl cp /path/to/file my-pod:/path/to/file

In the above example, we copied a local file /path/to/file to a pod named, my-pod. We’ve specified an identical path on the pod to copy the file. Notice that we used an absolute path in both cases. We can also use relative paths:

kubectl cp my-file my-pod:my-file

In this example, we’ve specified a relative path to a local file, and a relative path on the pod. One key difference between kubectl cp and a tool like scp is that with kubernetes, the file is copied relative to the working directory, not the home directory.

Copy file from a pod to a pod

Similarly, we can copy a file from one pod to another. The same rules for absolute and relative paths apply.

kubectl cp pod-1:my-file pod-2:my-file

Copy file from pod to your local machine

As you might have guessed, you simply swap the parameters from the first example.

kubectl cp my-pod:my-file my-file

This will copy my-file from the working directory of your pod to your current directory.

Copying directories

When using scp to copy directories, we’re accustomed to adding the -r (recursive) flag. With kubectl cp this is implied. You use the exact same syntax to copy directories is you would files.

kubectl cp my-dir my-pod:my-dir

Specifying a container

In some cases, you may be running multiple containers on a pod. In which case, you’ll need to specify the container. You can do so with -c, which is consistent with most other kubectl commands.

kubectl cp my-file my-pod:my-file -c my-container-name

Use “kubectl cp” to Copy Files to and from Kubernetes Pods

Tyler Tyler (285)
Total time: 10 minutes 
Updated: July 22nd, 2020

If you’re using Kubernetes, you may find the need to move files to and from containers running on pods. Before the days of containerization, we would use a tool like SCP (secure copy protocol) to move files to and from remote machines. Fortunately, Kubernetes provides a similar tool.

In this guide, you’ll learn how to use kubectl cp to move files to and from Kubernetes pods.

Posted in these interests:

kubernetes
PRIMARY
7 guides

Use “kubectl cp” to Copy Files to and from Kubernetes Pods

kubernetes
Tyler Tyler (285)
Total time: 10 minutes 
Updated: July 22nd, 2020
Tyler
 

Posted in these interests:

kubernetes
PRIMARY
7 guides
kubernetes
PRIMARY
7 guides
PRIMARY
Calling all writers!

We’re hiring. Write for Howchoo

 
In these interests
kubernetes
PRIMARY
7 guides
kubernetes
PRIMARY
7 guides
PRIMARY

From the docs, here’s the basic usage:

kubectl cp  

The kubectl cp command takes two parameters. The first is the source; the second is the destination. As with scp, either parameter (source or destination files) can refer to a local or remote file.

Before we begin

We need to make sure our kubernetes client is authenticated with a cluster. There are many ways to authenticate, but authentication is outside the scope of this guide.

Second, we’ll need to identify the name of a pod (or pods) that we want to work with. We’ll need to use kubectl get pod to find the name of the pod(s), and we’ll use these names in the following sections.

Copy file from local machine to pod

Suppose we want to move a file from our local machine to a pod.

kubectl cp /path/to/file my-pod:/path/to/file

In the above example, we copied a local file /path/to/file to a pod named, my-pod. We’ve specified an identical path on the pod to copy the file. Notice that we used an absolute path in both cases. We can also use relative paths:

kubectl cp my-file my-pod:my-file

In this example, we’ve specified a relative path to a local file, and a relative path on the pod. One key difference between kubectl cp and a tool like scp is that with kubernetes, the file is copied relative to the working directory, not the home directory.

Copy file from a pod to a pod

Similarly, we can copy a file from one pod to another. The same rules for absolute and relative paths apply.

kubectl cp pod-1:my-file pod-2:my-file

Copy file from pod to your local machine

As you might have guessed, you simply swap the parameters from the first example.

kubectl cp my-pod:my-file my-file

This will copy my-file from the working directory of your pod to your current directory.

Copying directories

When using scp to copy directories, we’re accustomed to adding the -r (recursive) flag. With kubectl cp this is implied. You use the exact same syntax to copy directories is you would files.

kubectl cp my-dir my-pod:my-dir

Specifying a container

In some cases, you may be running multiple containers on a pod. In which case, you’ll need to specify the container. You can do so with -c, which is consistent with most other kubectl commands.

kubectl cp my-file my-pod:my-file -c my-container-name

From the docs, here’s the basic usage:

kubectl cp  

The kubectl cp command takes two parameters. The first is the source; the second is the destination. As with scp, either parameter (source or destination files) can refer to a local or remote file.

Before we begin

We need to make sure our kubernetes client is authenticated with a cluster. There are many ways to authenticate, but authentication is outside the scope of this guide.

Second, we’ll need to identify the name of a pod (or pods) that we want to work with. We’ll need to use kubectl get pod to find the name of the pod(s), and we’ll use these names in the following sections.

Copy file from local machine to pod

Suppose we want to move a file from our local machine to a pod.

kubectl cp /path/to/file my-pod:/path/to/file

In the above example, we copied a local file /path/to/file to a pod named, my-pod. We’ve specified an identical path on the pod to copy the file. Notice that we used an absolute path in both cases. We can also use relative paths:

kubectl cp my-file my-pod:my-file

In this example, we’ve specified a relative path to a local file, and a relative path on the pod. One key difference between kubectl cp and a tool like scp is that with kubernetes, the file is copied relative to the working directory, not the home directory.

Copy file from a pod to a pod

Similarly, we can copy a file from one pod to another. The same rules for absolute and relative paths apply.

kubectl cp pod-1:my-file pod-2:my-file

Copy file from pod to your local machine

As you might have guessed, you simply swap the parameters from the first example.

kubectl cp my-pod:my-file my-file

This will copy my-file from the working directory of your pod to your current directory.

Copying directories

When using scp to copy directories, we’re accustomed to adding the -r (recursive) flag. With kubectl cp this is implied. You use the exact same syntax to copy directories is you would files.

kubectl cp my-dir my-pod:my-dir

Specifying a container

In some cases, you may be running multiple containers on a pod. In which case, you’ll need to specify the container. You can do so with -c, which is consistent with most other kubectl commands.

kubectl cp my-file my-pod:my-file -c my-container-name

From the docs, here’s the basic usage:

kubectl cp  

The kubectl cp command takes two parameters. The first is the source; the second is the destination. As with scp, either parameter (source or destination files) can refer to a local or remote file.

Before we begin

We need to make sure our kubernetes client is authenticated with a cluster. There are many ways to authenticate, but authentication is outside the scope of this guide.

Second, we’ll need to identify the name of a pod (or pods) that we want to work with. We’ll need to use kubectl get pod to find the name of the pod(s), and we’ll use these names in the following sections.

Copy file from local machine to pod

Suppose we want to move a file from our local machine to a pod.

kubectl cp /path/to/file my-pod:/path/to/file

In the above example, we copied a local file /path/to/file to a pod named, my-pod. We’ve specified an identical path on the pod to copy the file. Notice that we used an absolute path in both cases. We can also use relative paths:

kubectl cp my-file my-pod:my-file

In this example, we’ve specified a relative path to a local file, and a relative path on the pod. One key difference between kubectl cp and a tool like scp is that with kubernetes, the file is copied relative to the working directory, not the home directory.

Copy file from a pod to a pod

Similarly, we can copy a file from one pod to another. The same rules for absolute and relative paths apply.

kubectl cp pod-1:my-file pod-2:my-file

Copy file from pod to your local machine

As you might have guessed, you simply swap the parameters from the first example.

kubectl cp my-pod:my-file my-file

This will copy my-file from the working directory of your pod to your current directory.

Copying directories

When using scp to copy directories, we’re accustomed to adding the -r (recursive) flag. With kubectl cp this is implied. You use the exact same syntax to copy directories is you would files.

kubectl cp my-dir my-pod:my-dir

Specifying a container

In some cases, you may be running multiple containers on a pod. In which case, you’ll need to specify the container. You can do so with -c, which is consistent with most other kubectl commands.

kubectl cp my-file my-pod:my-file -c my-container-name

Basic usage

Calling all writers!

We’re hiring. Write for Howchoo

Tyler's profile pictureTyler
Joined in 2015
Software Engineer and creator of howchoo.
Tyler's profile picture
Share this guide!
RedditEmailText
Related to this guide:
Secure Your Sensitive Data with Kubernetes SecretsSecure Your Sensitive Data with Kubernetes Secrets
Learn how to create and use Kubernetes secrets
Tyler's profile picture TylerView
In these interests: codedevopskubernetes
How to Read Kubernetes SecretsHow to Read Kubernetes Secrets
So you’ve started using Kubernetes secrets. At some point, you’ll probably want to see the secret in plain text, either to validate it or use it in another context.
Tyler's profile picture TylerView
In these interests: kubernetes
Install Prometheus and Grafana in your Kubernetes clusterInstall Prometheus and Grafana in your Kubernetes cluster
What is Prometheus? Prometheus is an open source systems monitoring toolkit originally developed by SoundCloud.
Tyler's profile picture TylerView
In these interests: devopskubernetes
Secure Your Sensitive Data with Kubernetes SecretsSecure Your Sensitive Data with Kubernetes Secrets
Learn how to create and use Kubernetes secrets
Tyler's profile picture TylerView
In these interests: codedevopskubernetes
Tyler's profile pictureViewcodedevopskubernetes
How to Read Kubernetes SecretsHow to Read Kubernetes Secrets
So you’ve started using Kubernetes secrets. At some point, you’ll probably want to see the secret in plain text, either to validate it or use it in another context.
Tyler's profile picture TylerView
In these interests: kubernetes
Tyler's profile pictureViewkubernetes
Install Prometheus and Grafana in your Kubernetes clusterInstall Prometheus and Grafana in your Kubernetes cluster
What is Prometheus? Prometheus is an open source systems monitoring toolkit originally developed by SoundCloud.
Tyler's profile picture TylerView
In these interests: devopskubernetes
Tyler's profile pictureViewdevopskubernetes
People also read:
Install Apache Superset in a Kubernetes cluster
Obviously we’d call this “Supernetes”
As a Kubernetes user, I find that I often need to trigger a kubernetes cron job manually, outside of its schedule. Fortunately, since the release of Kubernetes 1.
There are many reasons you might want to copy Kubernetes secrets from one cluster to another. In recent months, I had to migrate to a new GKE cluster in order to get some new functionality.
Install Apache Superset in a Kubernetes cluster
Obviously we’d call this “Supernetes”
As a Kubernetes user, I find that I often need to trigger a kubernetes cron job manually, outside of its schedule. Fortunately, since the release of Kubernetes 1.
There are many reasons you might want to copy Kubernetes secrets from one cluster to another. In recent months, I had to migrate to a new GKE cluster in order to get some new functionality.
Install Apache Superset in a Kubernetes cluster
Install Apache Superset in a Kubernetes clusterHow to install Apache Superset on a GKE Kubernetes Cluster
How to Create a Kubernetes Job From a Cron Job
How to Copy Secrets From One Kubernetes Cluster to Another
Posted in these interests:
kuberneteskubernetes
kubernetes
PRIMARY
It’s pronounced “koob-er-net-ees”.
kuberneteskubernetes
kubernetes
PRIMARY
It’s pronounced “koob-er-net-ees”.
PRIMARY
Explore
Discuss this guide:
We’re hiring!
Are you a passionate writer? We want to hear from you!
We’re hiring!
Are you a passionate writer? We want to hear from you!
View openings

Want to support Howchoo? When you buy a tool or material through one of our Amazon links, we earn a small commission as an Amazon Associate.

Donate

Leave a Reply

Your email address will not be published. Required fields are marked *