Kubernetes 如何使用kubectl cp覆盖目录
我正在使用Kubernetes 如何使用kubectl cp覆盖目录,kubernetes,kubectl,overwrite,cp,Kubernetes,Kubectl,Overwrite,Cp,我正在使用kubectl cp命令将本地目录复制到Kubernetes吊舱中 kubectl cp test $POD:/tmp 它将test目录复制到Kubernetes pod/tmp目录中 现在我想覆盖pod中的测试目录。使用kubectl cp命令进行复制时,我找不到任何覆盖目录的选项 目前,我正在从pod中删除测试目录,然后复制该目录 kubectl exec $POD -- sh -c 'rm -rf /tmp/test' kubectl cp test $POD:/tmp 这工
kubectl cp
命令将本地目录复制到Kubernetes吊舱中
kubectl cp test $POD:/tmp
它将test
目录复制到Kubernetes pod/tmp
目录中
现在我想覆盖pod中的测试目录。使用kubectl cp
命令进行复制时,我找不到任何覆盖目录的选项
目前,我正在从pod中删除测试目录,然后复制该目录
kubectl exec $POD -- sh -c 'rm -rf /tmp/test'
kubectl cp test $POD:/tmp
这工作正常,但若复制时出现任何错误,pod中的现有目录也将被删除
如何在不先删除pod目录的情况下用本地目录覆盖pod目录
提前感谢。不要每次都对目录执行“kubectl cp”,而是使用“卷装载”将本地目录装载到pod 不要每次对目录执行“kubectl cp”,而是使用“卷装载”将本地目录装载到pod 不幸的是,目前使用
kubectl cp
命令无法达到您想要的状态。
如果有一些未记录的特性,请随意编辑此答案并提供解决方案,但目前文档中没有任何地方可以提出相反的建议
在通过运行kubectl cp--help
获得的kubectl
命令的上下文帮助中,也没有提到任何选项会修改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).
使用单行文本“某些内容”
。如果我们将本地/tmp/test目录
复制到/Pod
上的/tmp
目录,该目录已经包含test
文件夹和其他文件,比如testfile.txt
,两个目录的内容将合并,因此我们的目标/tmp/test
最终将包含:
/tmp/test# ls
different_file.txt testfile.txt
如果我们将本地不同_file.txt
的内容更改为“另一个内容”
,然后再次运行命令:
kubectl cp /tmp/test pod-name:/tmp
它将只覆盖目标distribute_file.txt
,该文件已存在于目标/tmp/test
目录中
目前无法覆盖此默认行为。不幸的是,目前无法使用
kubectl cp
命令实现所需状态。
如果有一些未记录的特性,请随意编辑此答案并提供解决方案,但目前文档中没有任何地方可以提出相反的建议
在通过运行kubectl cp--help
获得的kubectl
命令的上下文帮助中,也没有提到任何选项会修改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).
使用单行文本“某些内容”
。如果我们将本地/tmp/test目录
复制到/Pod
上的/tmp
目录,该目录已经包含test
文件夹和其他文件,比如testfile.txt
,两个目录的内容将合并,因此我们的目标/tmp/test
最终将包含:
/tmp/test# ls
different_file.txt testfile.txt
如果我们将本地不同_file.txt
的内容更改为“另一个内容”
,然后再次运行命令:
kubectl cp /tmp/test pod-name:/tmp
它将只覆盖目标distribute_file.txt
,该文件已存在于目标/tmp/test
目录中
目前无法覆盖此默认行为。很有可能。我做到了。在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>}
PS>$items=ls
PS>foreach($items中的item){kubectl cp/$item podname:var/opt/mount/$item}
PS>if($?-eq$false){kubectl cp podname:var/opt/mount/}
这是针对持久卷的,因此它将一直存在,直到卷被销毁
其逻辑是:
这是很有可能的。我做到了。在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>}
PS>$items=ls
PS>foreach($items中的item){kubectl cp/$item podname:var/opt/mount/$item}
PS>if($?-eq$false){kubectl cp podname:var/opt/mount/}
这是针对持久卷的,因此它将一直存在,直到卷被销毁
其逻辑是:
记住,一旦pod被删除,所有这些工作都将被撤销;如果某个节点出现故障,或者您有一个HorizontalPodAutoscaler,这可能会在您无法控制的情况下发生。最好找到一种方法来实现您的最终目标,而不依赖命令,如
kubectl cp
,无论是更新映像还是将持久性存储装载到pod中。我正在/tmp/test目录中复制数据。EFS安装在此测试目录上。所以pod终止时数据不会被删除。记住,所有这些工作都将被撤消