kubectl exec -n -c -- tar cf - /tmp/foo | tar xf - -C /tmp/bar
# -n 指定名称空间
# -c 指定容器
# -C 指定路径
kubectl exec -n -- 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 。
kubectl cp /:/tmp/foo /tmp/bar
# Copy /tmp/foo from a remote pod to /tmp/bar locally
kubectl cp /:/tmp/foo /tmp/bar -c
# 从远程的K8S集群的pod内的特定容器将文件转存到本地
tar cf - <需要转存的一系列文件> | kubectl exec -i -n <名称空间> -c <容器的名字> -- tar xf - -C <容器内存储文件的绝对路径>
tar cf - /tmp/foo | kubectl exec -i -n -- tar xf - -C /tmp/bar
# Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace
# 将本地目录 /tmp/foo 进行压缩为 “-”,
# 并使用 kubectl exec + tar 进行定向转存到
# K8S的容器内的指定目录/tmp/bar进行解压
tar cf - pod-ifself.yaml | kubectl exec -i -n test-2022 pod-initc -c pod-tomcat -- tar xf - -C /tmp
# 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 /:/tmp/bar_dir -c
# 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 /:/tmp/bar -c
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
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.
压缩文件解压
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
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
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
-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 --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
tar cf - /tmp/foo | kubectl exec -i -n -- tar xf -
-C /tmp/bar
# Copy /tmp/foo from a remote pod to /tmp/bar locally
kubectl exec -n -- 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 :/tmp/bar_dir
# Copy /tmp/foo local file to /tmp/bar in a remote pod in a specific container
kubectl cp /tmp/foo :/tmp/bar -c
# Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace
kubectl cp /tmp/foo /:/tmp/bar
# Copy /tmp/foo from a remote pod to /tmp/bar locally
kubectl cp /:/tmp/foo /tmp/bar
---
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