When running kubernetes pod, sometimes we need to copy local files to remote pod, or need to copy from remote pod to local.
You can use kubectl cp
for this task.
- Copy
/tmp/foo
local file to/tmp/bar
in a remote pod in namespace<YOUR-NAMESPACE>
tar cf — /tmp/foo | kubectl exec -i -n <YOUR-NAMESPACE> <YOUR-POD> — tar xf — -C /tmp/bar
2. Copy /tmp/foo
from a remote pod to /tmp/bar
locally
kubectl exec -n <YOUR-NAMESPACE> <YOUR-POD> — tar cf — /tmp/foo | tar xf — -C /tmp/bar
3. Copy /tmp/foo_dir
local directory to /tmp/bar_dir
in a remote pod in the default namespace
kubectl cp /tmp/foo_dir <YOUR-POD>:/tmp/bar_dir
4. Copy /tmp/foo
local file to /tmp/bar
in a remote pod in a specific container
kubectl cp /tmp/foo <YOUR-POD>:/tmp/bar -c <YOUR-CONTAINER>
5. Copy /tmp/foo
local file to /tmp/bar
in a remote pod in namespace <YOUR-NAMESPACE>
kubectl cp /tmp/foo <YOUR-NAMESPACE>/<YOUR-POD>:/tmp/bar
6. Copy /tmp/foo
from a remote pod to /tmp/bar
locally
kubectl cp <YOUR-NAMESPACE>/<YOUR-POD>:/tmp/foo /tmp/bar