如何使用 kubectl cp 覆盖目录

Kis*_*ore 1 overwrite cp kubernetes kubectl

我正在使用kubectl cp命令将本地目录复制到 Kubernetes pod

kubectl cp test $POD:/tmp
Run Code Online (Sandbox Code Playgroud)

它将test目录复制到 Kubernetes pod/tmp目录中。

现在我想覆盖 pod 中的 test 目录。使用kubectl cp命令进行复制时,我没有找到任何覆盖目录的选项。

目前,我正在从 pod 中删除 test 目录,然后复制该目录。

kubectl exec $POD -- sh -c 'rm -rf /tmp/test'
kubectl cp test $POD:/tmp
Run Code Online (Sandbox Code Playgroud)

这工作正常,但如果复制时出现任何错误,pod 中的现有目录也将被删除。

如何在不先删除 pod 目录的情况下用本地目录覆盖 pod 目录?

提前致谢。

mar*_*rio 5

不幸的是,目前无法通过kubectl cp命令实现您想要的状态。

如果有一些未记录的功能,请随时编辑此答案并提供解决方案,但目前文档中没有一个地方可以提出相反的建议。

无论是在这里还是在kubectl命令的上下文帮助中,都可以通过运行获得kubectl cp --help,没有提到会修改kubectl cp命令的默认操作的选项,这基本上是将现有目录的内容合并并复制的内容。

$ 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).
Run Code Online (Sandbox Code Playgroud)

基本上kubectl cp命令的默认行为是合并源目录和目标目录的内容。假设我们有/tmp/test包含以下内容的本地目录:

/tmp/test$ ls
different_file.txt
Run Code Online (Sandbox Code Playgroud)

单行文字"some content"。如果我们的本地复制/tmp/test directory/tmp我们的目录Pod,里面已经包含test了不同的文件夹,让我们说testfile.txt,这两个目录的内容将被合并,所以我们的目标/tmp/test将包含最终:

/tmp/test# ls
different_file.txt  testfile.txt
Run Code Online (Sandbox Code Playgroud)

如果我们将本地的内容更改different_file.txt"yet another content"并再次运行命令:

kubectl cp /tmp/test pod-name:/tmp
Run Code Online (Sandbox Code Playgroud)

它只会覆盖different_file.txt目标/tmp/test目录中已经存在的目标。

目前无法覆盖此默认行为。