Introduction
The kubectl cp command is very helpful in debugging issues and applying a temporary fix through file change.
For example, if we need to add debug loggers, we might want to consider using it instead of modifying configmaps.
Or we can use it to get specific log files out when using support bundle or kubectl logs command is not possible.
However, it may fail to work sometimes (usually because the tar binary is missing from the container)
Alternative
This only works if you have the kubectl exec permission.
If kubectl exec is not permitted, you cannot get an interactive shell or run commands directly inside the container. It is a common security measure in production environments, but it also means that you won’t be able to workaround the copy command. Consider using a sidecar container and shared volumes to exchange files instead or exchange files with local file sever through curl.
Copying Files FROM the Pod to Your Machine
This command creates a tar archive inside the pod, streams it out, and extracts it on your local machine.
# Syntax
kubectl exec <pod-name> -c <container-name> -- tar cf - /path/in/pod | tar xf - -C /local/path
# Example: Get all files from /app/logs in the pod and save them to your current directory
kubectl exec my-pod -- tar cf - /var/opt/jfrog/artifactory/etc/artifactory/logback.xml | tar xf -
Copying Files TO the Pod from Your Machine
cat logback.xml | kubectl exec -i my-pod -- sh -c 'cat > /var/opt/jfrog/artifactory/etc/artifactory/logback.xml'