how to copy file, directory from PODs to local host machine.

how to copy file, directory from K8s POD to local machine ?

     Generally we need some data from K8s PODs to our local machine for debugging and for future references. In this blog we will see that how we can copy the data from K8s POD to the local host directory or we can say to our local host machine and vise versa.

Copy data from PODs to the Local machine. 

1- If we are in the POD.

       if you are in the K8s POD already, then you can use SCP commands and their options to send the data file/directory to the local machine. 

From below commands we can login into the PODs.

kubectl exec -it <POD-Name > /bin/bash

After that Go to that path where the file present. use below command to scp the file to the local machine.

scp -r <filename /directory name > root@<local-machine IP >:<local machine path>

for Ex:

scp -r dummy.pcap root@xx.xx.xx.xx:/home/localdirectory/


2- using kubectl cp command:

# Copy /tmp/foo from a remote pod to /tmp/bar locally

kubectl cp <some-namespace>/<some-pod>:/tmp/foo /tmp/bar

we can also use in below format to copy the folder from POD to local host.

kubectl cp server-cudu-f66785456-agbcp:apps/config  /home/name/files/config


Copy local directory to remote POD directory:

# 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

we can also use in below format to copy a folder from the localhost directory to a POD.

kubectl cp /home/name/files/config server-cudu-f56776c55646-abcde:apps/config


No comments:

Post a Comment