English
Exporting Files from Kubernetes Containers: A Complete kubectl cp Tutorial and Practical Tips

In actual operations and maintenance work, we often need to export files from a Kubernetes container's file system, such as configuration files, scripts, log files, or data generated by applications. This article will introduce in detail several methods for exporting files from containers, as well as solutions to common problems.
Preparatory work
Before you begin, please ensure:
Installed and configured
kubectlcommand-line toolHas permission to access the target Pod
Know the full path of the target file
Method 1: Use kubectl cp Command (Recommended)
kubectl cp is the most direct file export method, similar to docker cp.
Basic usage
bash
# Syntax: kubectl cp <namespace>/<pod-name>:<container-path> <local-path>
kubectl cp default/my-pod:app/config.yaml ./config.yaml
# Specify the container (when the Pod has multiple containers)
kubectl cp default/my-pod:app/config.yaml ./config.yaml -c container-nameExport the entire directory
bash
# Export directory
kubectl cp default/my-pod:app/config/ ./config-backup/
# Export all files in the current directory
kubectl cp default/my-pod:app/ ./app-backup/💡 Note
The container path does not need to include /, otherwise it will cause a tar: Removing leading / from member names Error!
Method 2: Use kubectl exec + cat
When the container lacks tar For commands, or when the file is small, you can use cat Export files in conjunction with redirection.
Basic usage
bash
# Redirect the contents of a file in the container to a local file.
kubectl exec my-pod -n default -- cat /app/config.yaml > ./config.yaml
# Specify container (multi-container Pod)
kubectl exec my-pod -n default -c container-name -- cat /app/config.yaml > ./config.yamlExport binary file
For binary files, you can use base64 Encoding ensures transmission integrity:
bash
# Export from container and decode
kubectl exec my-pod -n default -- base64 /app/binary-file > ./binary-file.b64
base64 -d ./binary-file.b64 > ./binary-fileor use dd Command:
bash
kubectl exec my-pod -n default -- dd if=/app/binary-file 2>/dev/null > ./binary-fileMethod 3: Use kubectl exec+ Interactive Shell
Applicable to scenarios where you need to browse the file system and confirm file locations.
bash
# Enter the container interactive shell
kubectl exec -it my-pod -n default -c container-name -- /bin/sh
# Or use bash
kubectl exec -it my-pod -n default -c container-name -- /bin/bashAfter entering the container:
bash
# View file
ls -la /app/
cat /app/config.yaml
# After viewing the file contents, copy it locally (run it in a separate terminal).Method 4: Export multiple files from a container
Export using a loop
bash
# Export multiple specified files
for file in config.yaml settings.json secret.key; do
kubectl exec my-pod -n default -- cat /app/$file > ./$file
doneExport files matching the pattern
bash
# First get the file list
files=$(kubectl exec my-pod -n default -- ls /app/*.yaml 2>/dev/null)
# Export one by one
for file in $files; do
filename=$(basename $file)
kubectl exec my-pod -n default -- cat $file > ./$filename
doneVerify file integrity
After exporting the file, it is recommended to verify the integrity of the file:
bash
# Method 1: Compare MD5 values
# Calculate MD5 in the container
kubectl exec my-pod -n default -- md5sum /app/config.yaml
# Compute MD5 locally
md5sum ./config.yaml
# Method 2: Compare file sizes
kubectl exec my-pod -n default -- wc -c /app/config.yaml
ls -la ./config.yaml
# Method 3: Compare the number of lines in files (text files)
kubectl exec my-pod -n default -- wc -l /app/config.yaml
wc -l ./config.yamlCommon Questions and Solutions
Issue 1: The file does not exist
Error message:
cat: /path/to/file: No such file or directoryReason:
File path error
The file is in the Pod's Other containers middle
Filename case or spelling error
Solution:
bash
# 1. View all containers in the Pod
kubectl get pod my-pod -n default -o jsonpath='{.spec.containers[*].name}'
# 2. Search for files in the container
kubectl exec my-pod -n default -c container-name -- find / -name "filename" 2>/dev/null
# 3. View the directory structure in the container
kubectl exec my-pod -n default -c container-name -- ls -la /Problem 2: The Pod has multiple containers, and the wrong container is selected by default.
Reason: kubectl By default, select the first container in the Pod.
Solution: use -c Explicitly specify the container name in the parameter.
bash
# View all containers
kubectl get pod my-pod -n default -o jsonpath='{.spec.containers[*].name}'
# Specify the correct container
kubectl cp default/my-pod:/app/file.txt ./file.txt -c correct-containerProblem 4: Insufficient permissions
Error message:
cat: /path/to/file: Permission deniedSolution:
Ensure the user being used has file read permission.
Consider using
sudo(if the container supports it)Check file owner and permissions:
kubectl exec my-pod -n default -- ls -la /path/to/file
Issue 5: The file is too large, causing memory problems.
Solution: Export in chunks or use compressed transmission.
bash
# Compress it in the container before exporting.
kubectl exec my-pod -n default -- gzip -c /app/large-file.log > ./large-file.log.gz
# decompress
gunzip ./large-file.log.gzImport files into the container
As a supplement, this section also explains how to import local files into a container.
Use kubectl cp Import
bash
# Import files into the container
kubectl cp ./local-file.yaml default/my-pod:/app/remote-file.yaml
# Specify container
kubectl cp ./local-file.yaml default/my-pod:/app/remote-file.yaml -c container-nameUse kubectl exec+ Redirect import
bash
# Write local file contents to the container
cat ./local-file.yaml | kubectl exec -i my-pod -n default -c container-name \
-- tee /app/remote-file.yaml > /dev/nullBest Practices Summary
First choice
kubectl cpWhen the container containstarThis is the most convenient way when using commands.Alternative
kubectl execYou are an expert technical translator. Translate the following text into en. IMPORTANT: Translate ALL Chinese characters to English. If the text contains Chinese, you MUST translate it. If the text is already in en, output it EXACTLY as it is. Output ONLY the translated text, no quotes, no explanations.cat: When the container is missingtarWhen doing so, use this method.Multi-container Pods must specify
-c: Avoid choosing the wrong container, which may cause files to not be foundVerify integrity after export Confirm that the export is correct using MD5, file size, etc.
Consider compressing large files for transmission.: Avoid performance issues during transmission.
Make good use of an interactive Shell: When unsure of the file location, first enter the container and browse.