A kubectl plugin to mount a directory on Kubernetes pods to the local filesystem.
$ go install github.com/ueokande/kubectl-mount@latest
Mount a log directory on the pod nginx to the local directory:
$ mkdir -p /tmp/nginx-logs
$ kubectl mount nginx:/var/log /tmp/nginx-logs
The output is similar to this:
Mounted nginx:/var/log on /tmp/nginx-logs
The command runs until you interrupt the command (pressing Ctrl+C). If the directory is during use and you exit the command, the command output the following error:
Unable to unmount: /bin/fusermount: failed to unmount /tmp/nginx-logs: Device or resource busy
That means the kubectl mount
unable to unmount the filesystem. This error can occur when any processes open the mounted file or directory. That also cause when your shell enters the mounted directory. To resolve this error, exit the process which is using the file or directory, and then type the following:
$ fusermount -u /tmp/nginx
The kubectl mount
command works with the FUSE (Filesystem in Userspace) to mount a directory to the local filesystem. The FUSE is an interface to userspace programs to export a filesystem to the kernel. It allows showing users an interface to mount a variety of filesystems like a physical device, network storage, ramfs, and so on. Users can implement it to create any programmable filesystem. The go-fuse is a library to implement a FUSE interface in golang. It works on Linux with FUSE and macOS with OSXFUSE.
The kubectl mount
provides a filesystem to show files in the Kubernetes pods. It retrieve files or directories or read files in the pod via the Kubernetes exec
API. When you get the list in the directory, the ls
command runs on the pod and returns files on the directory via FUSE. Getting file information (creation time, modification time, owner, group) works with the result of the stat
command.
It is required to update files safely to write to files or create a file on the mounted files system, and it is not implemented yet. The kubectl mount
mounts as a read-only file system to protect files on the pod.
The kubectl mount
requires the following commands to be installed in the container:
find
orls
stat
cat
The kubectl mount
does not work well if the pod does not contain these commands, such as a container built from scratch.
Create a cluster:
$ kind create cluster --config .kind/cluster.yaml
Then deploy deployment nginx:
$ kubectl apply -f .kind/deployment.yaml