Kubernetes

How does kubectl cp work?

You may have to transfer files in and out of containers operating on pods when you are using Kubernetes. We used the cp command to transfer files to and from remote machines using a program like SCP (secure copy protocol) before containerization. Luckily, Kubernetes has a comparable feature. The kubectl cp command wraps the kubectl exec command, leveraging the core Kubernetes Pod API’s exec subresource. Kubectl cp depends on the tar command inside the container’s default execution PATH, mostly on the container side. It does not look at the file system of the container directly. This guide will cover some examples of the kubectl cp command for your better understanding.

Pre-requisites

To complete the working of the kubectl cp command, we will use Ubuntu 20.04 Linux operating system. The operating system choice will be wholly dependent on the user choice and preference. We have to install kubectl in it and also have to configure it on our system. The installation and configuration of the minikube are some of the basic and requirements.

Start minikube

Minikube is a program that keeps you up to date. It guides you through installing and running Kubernetes on a device running Linux, macOS, or Windows. You may skip the significant Kubernetes deployment degree of difficulty and jump right into using the container management tool’s functionality. Minikube is a virtual machine that operates a single-node Kubernetes cluster. To start it on the Ubuntu 20.04 Linux system, we have to use the command line terminal. Let’s open the shell by using the “Ctrl+Alt+T” shortcut key or by looking at the terminal application in the Ubuntu 20.04 Linux system applications. Execute the following listed command to get in progress with the minikube cluster.

$ minikube start

The process to start the minikube will take a few minutes, depending on the specifications of the machine on which you are operating. Once it gets started, we can further use the Kubernetes services.

Listing Pod details

When you build a pod, you give it its IP address. If the pod has many containers, they can connect by utilizing localhost. Extending communication beyond the pod is accomplished by exposing a port. The kubectl get command returns tabular data for one or even more resources. Label selectors can be used to filter content. Information can be provided for only the current namespace or the entire cluster. We’ll need to decide on the pod’s name or pods with which we would like to work. To determine the pod’s name (s), we will utilize the kubectl get pod, and we are using these names throughout the examples. Execute the following listed command to list the pods that are currently accessible in the system.

$ kubectl get pods

We have created a file in the home directory of the Ubuntu 20.04 Linux operating system. The file is named “abc.txt”. This file will be used to work the kubectl cp command in the Ubuntu 20.04 Linux operating system.

Example 1:

Let’s say we need to transfer some relevant files from a local PC to a pod. We copied a local file to a pod called “example” in the preceding example. To replicate the file, we have supplied an identical path on the pod. In both situations, you will notice that we utilized an absolute path. Relative pathways can also be used. One significant distinction between kubectl cp and a program like SCP is that now the file is copied according to the working directory rather than the home directory in Kubernetes. In this illustration, the file “abc.txt” will be utilized. Execute the below-cited command in the Ubuntu 20.04 Linux operating system terminal shell.

$ kubectl cp abc.txt example:abc.txt

Example 2:

It will copy the file “abc.txt” from your pod’s working directory to the current working directory. Execute the below-cited command in the Ubuntu 20.04 Linux operating system terminal shell.

$ kubectl sp example:abc.txt abc.txt

Conclusion

This guide is related to the kubectl cp command. The importance of the kubectl cp command has been explained in detail. We have discussed a few examples for a better understanding. I hope they will be valuable for your work.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content