使用tke或者eks集群的过程中,大家会有从容器下载大文件或者上传大文件到容器,其实eks和tke的控制台的登录容器页面是这次上传下载文件的,但是大小有限制,默认都是10M。
如果我需要上传大文件到容器,或者下载容器内大文件到本地,那应该要如何操作呢?控制台肯定是行不通了,其实我们可以用k8s提供的kubectl cp这个功能来实现我们的需求。
这里我们来看看kubectl cp的用法,其实help说明很清楚了
[niewx@VM-0-4-centos ~]$ kubectl cp -h
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).
下面我们针对不同集群来具体的操作下看看
因为执行kubectl命令需要用到k8s集群的kubeconfig,首先你开启下集群的公网或者内网访问,具体看客户端机器是公网还是内网访问集群,然后在基本信息页面复制或者下载对应的kubeconfig
然后在客户端机器配置下kubeconfig,这里我们将复制内容写到tke.config这个文件
[niewx@VM-0-4-centos ~]$ ll tke.config
-rw-rw-r-- 1 niewx niewx 5476 Apr 4 12:12 tke.config
[niewx@VM-0-4-centos ~]$ ll -lh nps
-rwxr-xr-x 1 niewx niewx 12M Apr 8 2021 nps
[niewx@VM-0-4-centos ~]$ k get pod | grep debug
debug-pod-7f8b7f7f67-k859n 1/1 Running 0 96s
下面我们测试下将nps这个文件上传到debug容器,然后再下载到本地
[niewx@VM-0-4-centos ~]$ kubectl --kubeconfig=tke.config cp /home/weixnie/nps weixnie/debug-pod-7f8b7f7f67-k859n:/tmp/
[niewx@VM-0-4-centos ~]$ kubectl --kubeconfig=tke.config exec -it debug-pod-7f8b7f7f67-k859n sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
/app # ls -alh /tmp
total 12M
drwxrwxrwt 1 root root 4.0K Apr 4 04:22 .
drwxr-xr-x 1 root root 4.0K Apr 4 04:20 ..
-rwxr-xr-x 1 1000 1000 12M Apr 4 04:24 nps
[niewx@VM-0-4-centos ~]$ kubectl --kubeconfig=tke.config cp weixnie/debug-pod-7f8b7f7f67-k859n:/tmp/nps /home/niewx/nps-cp
tar: removing leading '/' from member names
[niewx@VM-0-4-centos ~]$ ll -alh /home/niewx/nps-cp
-rw-rw-r-- 1 niewx niewx 12M Apr 4 12:28 /home/niewx/nps-cp
这里从测试可以看出,我们将本地文件nps上传到了容器的/tmp目录,然后将容器内的/tmp/nps文件下载到本地的nps-cp这个文件里面。
下面可以测试下将本地文件夹上传到容器里面,这里我们将本地的yaml文件夹上传到了容器的/tmp目录下。
[niewx@VM-0-4-centos ~]$ kubectl --kubeconfig=tke.config cp /home/niewx/yaml weixnie/debug-pod-7f8b7f7f67-k859n:/tmp/
[niewx@VM-0-4-centos ~]$ kubectl exec -it debug-pod-7f8b7f7f67-k859n sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
/app # cd /tmp/
/tmp # ls -al
total 12032
drwxrwxrwt 1 root root 4096 Apr 4 05:06 .
drwxr-xr-x 1 root root 4096 Apr 4 04:20 ..
-rwxr-xr-x 1 1000 1000 12308480 Apr 4 04:24 nps
drwxr-xr-x 2 root root 4096 Apr 4 05:06 yaml
kubectl cp同样支持从容器内拷贝文件夹到本地,我们将容器的/tmp/yaml目录文件拷贝到了/home/niewx/yaml/test这个目录
[niewx@VM-0-4-centos ~]$ kubectl --kubeconfig=tke.config cp weixnie/debug-pod-7f8b7f7f67-k859n:/tmp/yaml /home/niewx/yaml/test
tar: removing leading '/' from member names
[niewx@VM-0-4-centos yaml]$ ls -al /home/niewx/yaml | grep test
drwxr-xr-x 2 niewx niewx 4096 Apr 4 13:11 test
注意:上面的pod都是单容器,如果有多个容器,cp命令需要用-c来指定对应的容器。
tar: removing leading '/' from member names
执行kubectl cp命令的这个报错,大家可以忽略,这个只是一个提示而且,不影响文件的上传下载。
eks集群和tke的上传下载文件方式一样,首先下载kubeconfig到本地,开启内网或者公网访问,然后在证书关联,获取对应的kubeconfig。
后面的步骤大家参考第一节即可,这里就不操作了。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。