本件在本地与K8S容器间的互存(cp + tar)

工作思路


  • 明确需求。                                         明确要实现什么功能 == 实现文件到container的转存。
  • 浏览博客了解成功方向。                   当不知道使用什么方法时候,或者有什么方法可以选择,使用他人的经验可以快速定位需求的解决方案有多少种。
  • 阅读官方文档或者官方命令帮助。     因为博客网络都是其他人的需求和理解,可以通过官方文档进行精确选择和实验测试,选择满足自己的方案。
  • 根据官方文档进行测试。                    善于使用 man--help 寻求代码的帮助

K8S容器内转存到服务器本地


方式一 (tar + exec):

kubectl exec -n <some-namespace> <some-pod> -c <some- container> -- tar cf - /tmp/foo | tar xf - -C /tmp/bar
#    -n 指定名称空间
#    -c 指定容器
#    -C 指定路径

kubectl exec -n <some-namespace> <some-pod> -- tar cf - /tmp/foo | tar xf - -C /tmp/bar
# Copy /tmp/foo from a remote pod to /tmp/bar locally
# 通过使用kubectl exec + tar命令方式,
# 从K8S的容器内压缩/tmp/foo文件,
# 并定向解压到本地服务器的指定目录 /tmp/bar 。

方式二 (cp):

kubectl cp <some-namespace>/<some-pod>:/tmp/foo /tmp/bar
# Copy /tmp/foo from a remote pod to /tmp/bar locally

kubectl cp <some-namespace>/<some-pod>:/tmp/foo /tmp/bar -c <some-container>
# 从远程的K8S集群的pod内的特定容器将文件转存到本地

服务器本地转存到K8S容器内


方式一 (tar + exec):

tar cf - <需要转存的一系列文件> | kubectl exec -i -n <名称空间> <pod的名字> -c <容器的名字> -- tar xf - -C <容器内存储文件的绝对路径>

tar cf - /tmp/foo | kubectl exec -i -n <some-namespace> <some-pod> -- tar xf - -C /tmp/bar
# Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace <some-namespace>
# 将本地目录 /tmp/foo 进行压缩为 “-”,
# 并使用 kubectl exec + tar 进行定向转存到
# K8S的容器内的指定目录/tmp/bar进行解压

example:

tar cf - pod-ifself.yaml | kubectl exec -i -n test-2022 pod-initc -c pod-tomcat -- tar xf - -C /tmp

方式二 (cp):

# Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the default namespace and in a specific container
kubectl cp /tmp/foo_dir <some-namespace>/<some-pod>:/tmp/bar_dir -c <specific-container>
  
# Copy /tmp/foo local file to /tmp/bar in a remote pod in the default namespace and in a specific container
kubectl cp /tmp/foo <some-namespace>/<some-pod>:/tmp/bar -c <specific-container>

注意

kubectl cp 在一次传输中只能传输相同类型的文件,不能传输不同类型的文件到容器。

例如:
文件和文件夹不能同时传输到容器内。

# 传输文件夹
kubectl cp ./pod test-2022/pod-tomcat:/tmp -c pod-tomcat

# 传输文件
kubectl cp ./123 test-2022/pod-tomcat:/tmp -c pod-tomcat

# 查看结果
kubectl exec -i pod-tomcat -n test-2022 -- ls -l /tmp

# 失败操作
kubectl cp ./pv-pvc ./123 test-2022/pod-tomcat:/tmp -c pod-tomcat
kubectl cp ./{pv-pvc 123} test-2022/pod-tomcat:/tmp -c pod-tomcat

附注


tar 命令

man tar  # tar            命令的使用
-c       # create         创建
-v       # visit          查看,罗列
-x       # extract        解压

Example:     
       tar -cf archive.tar foo bar
              # Create archive.tar from files foo and bar.
                创建压缩文件
       tar -tvf archive.tar
              # List all files in archive.tar verbosely.
                罗列压缩文件内容
       tar -xf archive.tar
              # Extract all files from archive.tar.
                压缩文件解压

option

Common options:
       -C, --directory=DIR
              change to directory DIR

       -f, --file=ARCHIVE
              use archive file or device ARCHIVE

       -j, --bzip2
              filter the archive through bzip2

        -J, --xz
              filter the archive through xz

       -v, --verbose
              verbosely list files processed

       -z, --gzip
              filter the archive through gzip

operate

Main operation mode:
       -A, --catenate, --concatenate
              append tar files to an archive

       -c, --create
              create a new archive

       -d, --diff, --compare
              find differences between archive and file system

       --delete
              delete from the archive (not on mag tapes!)

       -r, --append
              append files to the end of an archive

       -t, --list
              list the contents of an archive

       --test-label
              test the archive volume label and exit

       -u, --update
              only append files newer than copy in archive

       -x, --extract, --get
              extract files from an archive

kubectl exec 命令

Execute a command in a container.        容器内执行一条命令

Examples:
  
  # Switch to raw terminal mode; sends stdin to 'bash' in ruby-container from pod mypod
  # and sends stdout/stderr from 'bash' back to the client

  kubectl exec mypod -c ruby-container -i -t -- bash -il
  
  # List contents of /usr from the first container of pod mypod and sort by modification time
  # If the command you want to execute in the pod has any flags in common (e.g. -i),
  # you must use two dashes (--) to separate your command's flags/arguments
  # Also note, do not surround your command and its flags/arguments with quotes
  # unless that is how you would execute it normally (i.e., do ls -t /usr, not "ls -t /usr")

  kubectl exec mypod -i -t -- ls -t /usr

option

  -c, --container='': 
# Container name. If omitted, use the kubectl.kubernetes.io/default-container 
# annotation for selecting the container to be attached or the first container 
# in the pod will be chosen

  -i, --stdin=false: Pass stdin to the container
  -t, --tty=false: Stdin is a TTY
  -f, --filename=[]: to use to exec into the resource
  -q, --quiet=false: Only print output from the remote session

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.
  #
  # For advanced use cases, such as symlinks, wildcard expansion or
  # file mode preservation, consider using 'kubectl exec'.
  

  # 要求容器映像中存在“tar”二进制文件。如果“tar”不存在,“kubectl cp”将失败。

  # 对于高级用例,如符号链接、通配符扩展或文件模式保存,请考虑使用“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

pod-tomcat.yaml

---
apiVersion: v1
kind: Pod
metadata:
  name: pod-tomcat
  namespace: test-2022
  labels:
    tomcat: pod-tomcat
spec:
  containers:
    - name: pod-tomcat
      ports:
        - containerPort: 8080
      image: harbor:80/library/tomcat:9.0.64-jdk11
      imagePullPolicy: IfNotPresent
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值