/kubectl-mount

A kubectl plugin to mount remote filesystem on Kubernetes pods

Primary LanguageGoMIT LicenseMIT

kubectl-mount

A kubectl plugin to mount a directory on Kubernetes pods to the local filesystem.

💿 Installation

$ go install github.com/ueokande/kubectl-mount@latest

📔 Usage

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

🤿 How does it work

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.

Architecture

🛑 Limitation

Read-only filesystem

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.

Linux distribution requirements

The kubectl mount requires the following commands to be installed in the container:

  • find or ls
  • stat
  • cat

The kubectl mount does not work well if the pod does not contain these commands, such as a container built from scratch.

🛠️ Developing

Create a cluster:

$ kind create cluster --config .kind/cluster.yaml

Then deploy deployment nginx:

$ kubectl apply -f .kind/deployment.yaml

📝 LICENSE

MIT