kubernetes复制命令

kubectl cp

  • 通过kubectl帮助文档可以看到复制命令的详情
[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
  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>

如果不知道container的名字执行一下这个指令通过pod的配置文件就可以看到容器的名字了

kubectl describe pod <pod-name> -n <namespace-name>

docker复制命令

  • 帮助文档
[root@qd01-test-yinhe177004066 daicong_test]# docker cp
"docker cp" requires exactly 2 arguments.
See 'docker cp --help'.

Usage:  docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
	docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Copy files/folders between a container and the local filesystem
  • 方法1:使用cp
docker cp /etc/localtime 87986863838b:/etc/ #方法1

注意是容器id,不是镜像的id,需要用docker ps -a|grep 镜像名字 来查看id

docker cp指令是一次性的,如果想实现持续的更新同步,要通过-v卷(volumn)映射去实现

  • 方法2:docker通过卷的映射进行数据的复制
#要启动一个<container-name>容器,宿主机的/etc/localtime目录挂载到容器的/etc/localtime目录
docker run -it -v 宿主机目录:容器目录 <container-name>  bash #方法2

案例:

[root@ks-allinone daicong]# docker run -it centos:v1 bash
[root@4c60cbc7a487 /]# date 
Tue Aug 25 06:43:50 UTC 2020
[root@4c60cbc7a487 /]# exit
exit
[root@ks-allinone daicong]# docker run -it -v /etc/localtime:/etc/localtime centos:v1 bash
[root@392e1f1ef04e /]# date
Tue Aug 25 14:44:36 CST 2020 #可以看到通过卷的映射实现了时间的同步共能

1、可以看到通过卷的映射实现了时间的同步共能
2、格式为docker -v 宿主机目录:容器目录

  • 在创建dockerfile时实现时间时区同步
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

猜你喜欢

转载自blog.csdn.net/qq_26884501/article/details/108220122