How to overrwite directory using kubectl cp

Related searches

I am copying a local directory into Kubernetes pod using kubectl cp command

kubectl cp test $POD:/tmp

It copies the test directory into Kubernetes pod /tmp directory.

Now I want to overwrite the test directory in the pod. I did not find any option to overwrite directory while copying using kubectl cp command.

Currently, I am deleting the test directory from the pod and then copy the directory.

kubectl exec $POD -- sh -c 'rm -rf /tmp/test'
kubectl cp test $POD:/tmp

This is working fine, but in case any error comes while copying, existing directory from pod will also be deleted.

How can I overwrite the pod directory with a local directory without deleting the pod directory first?

Thanks in advance.


Currently there is unfortunatelly no way to achieve your desired state with kubectl cp command.

If there are some undocumented features, please feel free to edit this answer and provide the solution, but currently there is no single place in documentation that could suggest the opposite.

Neither here nor in the context help of the kubectl command, available by running kubectl cp --help, there is no option mentioned that would modify the default operation of thekubectl cp command, which is basically a merge of the content of the already existing directory and copied one.

$ kubectl cp --help
Copy files and directories to and from containers.

Examples:
  # !!!Important Note!!!
  # Requires that the 'tar' binary is present in your container
  # image.  If 'tar' is not present, 'kubectl cp' will fail.

  # Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the default namespace
  kubectl cp /tmp/foo_dir <some-pod>:/tmp/bar_dir

  # 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

  # Copy /tmp/foo from a remote pod to /tmp/bar locally
  kubectl cp <some-namespace>/<some-pod>:/tmp/foo /tmp/bar

Options:
  -c, --container='': Container name. If omitted, the first container in the pod will be chosen

Usage:
  kubectl cp <file-spec-src> <file-spec-dest> [options]

Use "kubectl options" for a list of global command-line options (applies to all commands).

Basically the default behavior of kubectl cp command is a merge of the content of source and destination directory. Let's say we have local directory /tmp/test containing:

/tmp/test$ ls
different_file.txt

with single line of text "some content". If we copy our local /tmp/test directory to /tmp directory on our Pod, which already contains test folder with a different file, let's say testfile.txt, the content of both directories will be merged, so our destination /tmp/test will contain eventually:

/tmp/test# ls
different_file.txt  testfile.txt

If we change the content of our local different_file.txt to "yet another content" and run again the command:

kubectl cp /tmp/test pod-name:/tmp

it will only override the destination different_file.txt which is already present in the destination /tmp/test directory.

Currently there is no way to override this default behavior.

How to copy files recursively to a Kubernetes pod?, user@localhost ~ $ kubectl cp --help Copy files and directories to and from containers. Examples: # !!!Important Note!!! # Requires that the 'tar'� 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.


It is very possible. I did it. Try this in PowerShell:

PS> $items = ls <dir1>

PS> foreach ($item in $items) { kubectl cp <dir1>/$item podname:var/opt/mount/<dir2>/$item }

PS> if ($? -eq $false) { kubectl cp <dir1>podname:var/opt/mount/<dir2>}

This is for persistent volume so it will exist until volume is destroyed.

The logic to this is:

  1. Get all items for the desired directory
  2. Try writing all items of that desired directory, if destination directory does not exist, then go to second statement in.
  3. Second part, is write desired directory as destination directory name
  4. Instead of running the second part again if you rerun command (which will place the desired directory inside destination directory), you instead write all items to destination directory

Use "kubectl cp" to Copy Files to and from Kubernetes Pods , 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'� Similar to that we have ‘KUBECTL CP’ to Copy the files and directories from a Kubernetes Container [POD] to the local host and vice versa. Syntax: kubectl cp <file-spec-src> <file-spec-dest>


Instead of doing "kubectl cp" everytime for your directory, mount your local directory to your pod using "volume mounts".

kubectl-cp command man page, Copy files and directories to and from containers. Options. -c, --container="". Container name. If omitted, the first container in the pod� Directory Layout Branches Layout Repository Layout Shared Base Layout App Deployment kubectl cp <some-pod>:/tmp/foo /tmp/bar


Copy Files & Folders to and from Kubernetes Pod(s) — All Cases , In the first part(A), lets take cases for copying files & folders from local to kubectl cp [folder-path] [pod-name]:/[path]$ kubectl cp testfile.txt� kubectl cp Description. Copy files and directories to and from containers. Options-c, --container="" Container name. If omitted, the first container in the pod will be chosen--no-preserve=false. The copied file/directory's ownership and permissions will not be preserved in the container Options Inherited from Parent Commands--add-dir-header=false


Copying Container Files � The Kubectl Book, Copying files from Containers in a cluster to a local filesystem; Copying files from a local filesystem to kubectl cp /tmp/foo_dir <some-pod>:/tmp/bar_dir. Copy� The amazing hint on this is, this works as well with docker. Just change kubectl to docker and it will work as well. Copy Files from a docker container to your machine


All kubectl clients running a vulnerable version and using the cp operation. Vulnerability impact: A malicious user can potentially create or overwrite files outside of the destination directory of the kubectl cp operation. Mitigations prior to upgrading: Avoid using kubectl cp with any untrusted workloads. Fixed versions: Fixed in v1.13.9 by