How to use kubectl cp to copy files automatically from a local system to kubernetes Pods with list filter

10/6/2018

I have many pods in a kubernetes system with randomly name wordpress.xxx.xx.

Here the list of pods

I want to use one command with kubectl cp in other to copy files to all pods from one deployment.

In my case I don't want to use volumes because they mount into a path that already the existing content of that folder will be hidden.

How to do that, please?

Thank you for your answer.

-- Ngo Quyet
kubernetes
tomcat

1 Answer

10/6/2018

The kubectl cp command copies files to a single container in a pod. To copy files in multiple containers easily, the below shell script can be used.

for pod in `kubectl get pods -o=name | grep wordpress | sed "s/^.\{4\}//"`; do echo "copying to $pod"; kubectl cp file.txt $pod:/; done

or

for pod in `kubectl get pods -o=name | grep wordpress | sed "s/^.\{4\}//"`
do
    echo "copying file to $pod"
    kubectl cp file.txt $pod:/
done

Both the scripts are same, single vs multi-line.

-- Praveen Sripati
Source: StackOverflow