Friday, July 4, 2025

How to Use fzf with Kubernetes

 

✅ How to Use fzf with Kubernetes

Here are some powerful ways to combine fzf with kubectl:

1. Get Pods and Inspect One



kubectl get pods --all-namespaces -o name | fzf | xargs kubectl describe
  • You’ll interactively pick a pod.

  • Then kubectl describe will show its details.


2. Exec into a Pod (Select with fzf)


kubectl get pods -o name | fzf | xargs -I {} kubectl exec -it {} -- bash
  • Launches a bash shell into the selected pod (if it supports bash).


3. List Namespaces and Switch


kubectl get ns --no-headers | awk '{print $1}' | fzf
  • Outputs a list of namespaces to select from.

To switch context (assuming a helper function):


kubens $(kubectl get ns --no-headers | awk '{print $1}' | fzf)

4. Use Aliases or Functions in Shell

Add to your .bashrc or .zshrc:


kpod() { kubectl get pods -o name | fzf | xargs kubectl describe }

Then run kpod to trigger the interactive workflow.


5. Kube Tools with Built-in Fuzzy Support

Some CLI tools already integrate fzf:

🧰 k9s

  • Terminal UI for managing Kubernetes.

  • Has built-in fuzzy search.

  • Fully interactive.

🧰 kubectx and kubens

  • kubectx | fzf lets you fuzzy-pick a context to switch.

  • kubens | fzf for namespace switching.


🔄 Combining Tools for Better UX

You can pipe commands together:


kubectl get pods -n $(kubens | fzf) | fzf

Or create custom interactive dashboards using fzf, kubectl, and shell functions.

✅ Bash/Zsh Function

  1. Lists all namespaces using kubectl get ns.

  2. Lets you select a namespace using fzf.

  3. Lists pods in that namespace.

  4. Lets you select a pod using fzf.

  5. Runs kubectl describe pod on the selected pod.


Add this to your ~/.bashrc or ~/.zshrc:



kdescribe() { local namespace pod namespace=$(kubectl get ns --no-headers -o custom-columns=NAME:.metadata.name | fzf --prompt="Select Namespace: ") [ -z "$namespace" ] && echo "No namespace selected" && return pod=$(kubectl get pods -n "$namespace" --no-headers -o custom-columns=NAME:.metadata.name | fzf --prompt="Select Pod in $namespace: ") [ -z "$pod" ] && echo "No pod selected" && return kubectl describe pod "$pod" -n "$namespace" }

Then reload your shell:


source ~/.bashrc # or ~/.zshrc

💡 Usage

Simply type:


kdescribe

And follow the interactive prompts to:

  1. Choose a namespace

  2. Choose a pod

  3. View detailed description (kubectl describe pod output)

Fuzzy Finder - Linux

In Linux and other Unix-like environments, a fuzzy finder is a command-line tool that helps users quickly search and select items (like files, commands, or options) by typing partial or approximate input. It is especially useful when you can't remember the exact name of a file or command.


🔍 What Does “Fuzzy” Mean?

“Fuzzy” means the tool matches patterns even if the input isn’t exact. For example, typing rdc might match read_config_file.txt because the letters appear in order, even if not consecutively.


🛠️ Popular Fuzzy Finder: fzf

The most widely used fuzzy finder in Linux is fzf:

Features:

  • Interactive command-line fuzzy finder

  • Can be used to search:

    • Files in the directory tree

    • Command history

    • Git commits or branches

    • Environment variables

  • Integrates well with tools like vim, tmux, bash, and zsh

Example Usage:



# Find files in current directory tree find . -type f | fzf # Search through command history history | fzf # Git branch switcher git checkout $(git branch | fzf)

🔧 Installation

For most systems:



# Using a package manager sudo apt install fzf # Debian/Ubuntu brew install fzf # macOS/Homebrew

Or via Git:


git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf ~/.fzf/install

🧠 Why Use It?

  • Speeds up navigation and selection

  • Reduces the need to remember exact names

  • Works with many CLI tools and workflows