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)

No comments:

Post a Comment