How to Use the Kubectl Cp Command

The kubectl cp command is used to copy files and directories between a local machine and a container in a pod running in a Kubernetes cluster. It allows you to transfer files to and from the container.

To use the kubectl cp command, follow this syntax:

kubectl cp {{source_path}} {{namespace}}/{{pod_name}}:{{destination_path}}

Replace {{source_path}} with the path to the file or directory on your local machine that you want to copy. {{namespace}} represents the namespace of the pod, {{pod_name}} represents the name of the pod, and {{destination_path}} represents the path inside the container where you want to copy the file or directory.

For example, to copy a file named file.txt from your local machine to a pod named my-pod in the my-namespace namespace, you would run:

kubectl cp file.txt my-namespace/my-pod:/path/to/destination/file.txt

This command will copy the file.txt from your local machine to the specified path inside the container.

To copy a directory, you can use the -r flag:

kubectl cp -r directory my-namespace/my-pod:/path/to/destination/directory

This command will copy the directory and its contents from your local machine to the specified path inside the container.

Please note that you need to have the kubectl command-line tool installed and properly configured to connect to your Kubernetes cluster in order to use the kubectl cp command. Additionally, ensure that you have the necessary permissions to perform this action.

The kubectl cp command is useful for transferring files and directories between your local machine and containers running in a Kubernetes cluster. It allows you to easily move data in and out of containers for debugging, troubleshooting, or other purposes.

Remember to adjust the placeholders ({{}}) with the actual values specific to your use case.