Greg Charles
Greg Charles

Reputation: 2000

kubectl ls -- or some other way to see into a POD

I'm using kubectl cp to copy a jar file from my local file system into a the home directory of a POD in my minikube environment. However, the only way I can confirm that the copy succeeded is to issue a new kubectl cp command to copy the file back to a temp directory and compare the checksums. Is there a way to view the copied files directly?

Upvotes: 49

Views: 97067

Answers (4)

adamency
adamency

Reputation: 1508

All other answers have a crucial shortcoming: they require the running container of the given pod to include a shell (sh, bash, ...) or a ad-hoc command for filesystem discovery like ls, cat, tree, etc...

However, this is absolutely not necessary to inspect the filesystem of the container as one should know that a container's root filesystem is simply a directory on the host mounted as / in the container. Thus, the issue is only in finding the directory in question to be able to freely explore the container's filesystem from the host, i.e. with all the usual CLI utils we are familiar with instead of being forced to rely on a scarce CLI environment within the container.

Universal Solution

The solution will vary depending on the underlying container runtime and CLI utility used to interact with it, but the steps are pretty much always the same. Here is the breakdown for a k8s cluster running on containerd:

1. Find Container ID of Container in Pod

$ kubectl get pod <myapp_pod> -o jsonpath='{.status.containerStatuses[0].containerID}'

containerd://<container_id>

(assuming there is only one container running the pod)

2. Find Node where Pod is running

$ kubectl get pod <myapp_pod> -o jsonpath='{.spec.nodeName}'

<node>

3. SSH into <node>

4. Find Host PID of container

user@<node> $ ctr -n k8s.io t ls | grep <container_id> | awk '{print $2}'

<pid>

5. Go to host directory used as root mountpoint for the Container

user@<node> $ cd /proc/<pid>/root
6. Enjoy

=> There it is, You can now freely explore the container filesystem for what you're looking for.

As an example, here is the listing of the root directory of a coredns k8s container:

user@<node> $ cd /proc/7956/root $ ls
total 53M
drwxr-xr-x   1 root  root  4.0K Jan  1  1970 var
drwx------   1 root  root  4.0K Jan  1  1970 root
drwxr-xr-x   1 65532 65532 4.0K Jan  1  1970 home
drwxr-xr-x   1 root  root  4.0K Jan  1  2000 usr
drwxrwxrwt   1 root  root  4.0K Aug  3  2017 tmp
drwxr-xr-x   2 root  root  4.0K Apr  2  2023 sbin
drwxr-xr-x   2 root  root  4.0K Apr  2  2023 run
drwxr-xr-x   2 root  root  4.0K Apr  2  2023 lib
drwxr-xr-x   2 root  root  4.0K Apr  2  2023 boot
drwxr-xr-x   2 root  root  4.0K Apr  2  2023 bin
-rwxr-xr-x   1 root  root   53M Aug 15  2023 coredns
dr-xr-xr-x  13 root  root     0 Mar 30 04:48 sys
dr-xr-xr-x 230 root  root     0 Mar 30 04:48 proc
drwxr-xr-x   1 root  root  4.0K Mar 30 04:48 etc
drwxr-xr-x   5 root  root   360 Mar 30 04:48 dev

Upvotes: 11

Ayushmati
Ayushmati

Reputation: 1592

I think it is really convenient to use shell or bash to navigate the files in the file system of the container as @JRA_TLL has pointed out.

Remember to open shell in the interactive mode though:

kubectl exec -it <podname> -- sh

When you open the shell, by default it opens in the application directory i.e. app folder. You can use shell commands like ls to view folders , cd .. to navigate etc. Using cd .. in the app folder will get you to the root folder of the container.

We can also use:

kubectl exec -it --namespace <namespace> <podname> -- bash

Upvotes: 7

JRA_TLL
JRA_TLL

Reputation: 1361

Most practical would be to start a shell in the container if you want to execute multiple commands, ie.

kubectl exec <pod_name> -- sh

Upvotes: 0

nickgryg
nickgryg

Reputation: 28713

You can execute commands in a container using kubectl exec command.

For example:

to check files in any folder:

kubectl exec <pod_name> -- ls -la /

or to calculate md5sum of any file:

kubectl exec <pod_name> -- md5sum /some_file

Upvotes: 67

Related Questions