美文网首页
PVC操作-kubectl cp 容器与主机之间互相拷贝

PVC操作-kubectl cp 容器与主机之间互相拷贝

作者: 催化剂 | 来源:发表于2022-08-29 09:52 被阅读0次

第一步:配置deployment,指定PVC

第二步,执行kubectl cp命令,拷贝文件到容器指定的目录/data

文件夹拷贝到容器的根目录下

root@master:~/fpi/park-gis-web# kubectl cp ./data ipes-nanjing/park-gis-web-d5f74d88f-mqtj6:/

//文件夹,从本地拷贝到容器

kubectl cp ./data ipes-nanjing/park-gis-web-757df84795-8lrw6:/

//文件夹,从容器拷贝到本地

kubectl cp ipes-nanjing/gateway-server-69cf9485b5-p8kwt:/config/  ./

//从pod复制文件到宿主机,从容器拷贝到本地

【错误】kubectl cp ipes-nanjing/gateway-server-69cf9485b5-p8kwt:/config/application.yml ./application.yml

报错:root@master:~/fpi/tempyml# kubectl cp ipes-nanjing/gateway-server-69cf9485b5-p8kwt:/config/application.yml ./application.yml

  tar: Removing leading `/' from member names

  warning: file "application.yml" is a symlink, skipping (consider using "kubectl exec -n "ipes-nanjing" "gateway-server-69cf9485b5-p8kwt" -- tar cf - "/config/application.yml" | tar xf -")

//从pod复制文件到宿主机

第一步,在pod内部,把文件cp到exec默认进去的目录/app

root@gateway-server-69cf9485b5-p8kwt:/app# cp  /config/application.yml ./

第二步,调用kubectl cp命令,目标参数时,必须为文件不能是一个目录(eg:application.yml)

kubectl cp ipes-nanjing/gateway-server-69cf9485b5-p8kwt:application.yml ./application.yml

[root@qd01-test-yinhe177004066 daicong_test]# 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.

  #

  # For advanced use cases, such as symlinks, wildcard expansion or

  # file mode preservation consider using 'kubectl exec'.

  # Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace <some-namespace>

  tar cf - /tmp/foo | kubectl exec -i -n <some-namespace> <some-pod> -- tar xf - -C /tmp/bar

  # Copy /tmp/foo from a remote pod to /tmp/bar locally

  kubectl exec -n <some-namespace> <some-pod> -- tar cf - /tmp/foo | tar xf - -C /tmp/bar

  # 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,in the default namespace

  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

      --no-preserve=false: The copied file/directory's ownership and permissions will not be preserved in the container

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).

``

* 举例将本地的localtime文件复制到容器中实现时间的同步

```bash

kubectl cp /etc/localtime  <dest-pod>:/etc/localtime -c <container-name> -n <namespace-name>

相关文章

网友评论

      本文标题:PVC操作-kubectl cp 容器与主机之间互相拷贝

      本文链接:https://www.haomeiwen.com/subject/whphnrtx.html