kubectx / kubens v0.9.5 - AudioDocs
Tool: kubectx and kubens - Kubernetes Context & Namespace Switcher Version: v0.9.5 Duration: ~10 minutes Voice: Algieba (Gemini 2.5 TTS) Source: github.com/ahmetb/kubectx
Listen
Topics Covered
- The Problem: Why context and namespace switching with raw kubectl is tedious
- kubectx: Instant context switching, the dash flag for toggling, context renaming
- kubens: Namespace switching, force flag for non-existent namespaces
- fzf Integration: Interactive fuzzy search for power users
- Installation: Homebrew, apt, pacman, Krew, Chocolatey, winget
- Customization: Colors, NO_COLOR support, ignoring fzf
- Workflow Tips: Pairing with kube-ps1, naming conventions, productivity patterns
Transcript
kubectx and kubens: Power Tools for kubectl
Version 0.9.5.
Today we're diving into two essential tools that will transform how you navigate Kubernetes clusters and namespaces. If you've ever found yourself typing long kubectl config commands just to switch between clusters, or constantly forgetting which namespace you're currently working in, kubectx and kubens are about to become your new best friends.
Let's start with the problem these tools solve. When you work with Kubernetes, you're constantly juggling contexts and namespaces. A context in Kubernetes represents a cluster, user, and namespace combination. Every time you want to switch from your development cluster to production, the standard kubectl approach looks something like this: kubectl config use-context gke-prod-us-west-1-b-production. That's not just verbose, it's error-prone. One typo in a production context name could mean running commands against the wrong cluster.
And namespaces are even more frequently switched. During a typical debugging session, you might move between kube-system, monitoring, logging, and your application namespaces dozens of times. The standard kubectl approach? kubectl config set-context --current --namespace=monitoring. Every. Single. Time.
kubectx and kubens eliminate this friction entirely.
What kubectx Does
At its core, kubectx is a context switcher. Run kubectx with no arguments and you see a list of all your configured contexts. Your current context is highlighted. Type kubectx followed by a context name, and you instantly switch to that context. That's it. No verbose commands, no remembering exact syntax.
But the real magic comes with a few key features:
The dash flag: Run kubectx -, and you switch back to your previous context. This is incredibly useful when you're jumping between two clusters repeatedly. Production to staging, staging to production. Just kubectx - each time.
Context renaming: Those long auto-generated context names from cloud providers? You can rename them. kubectx prod=gke_my-project_us-west1-b_production. Now instead of typing that monster, you just type kubectx prod.
Interactive mode with fzf: If you have fzf installed, running kubectx presents an interactive fuzzy-search menu. Start typing any part of a context name and it filters in real-time. Arrow keys to navigate, enter to select. For anyone managing ten, twenty, or more contexts, this is transformative.
What kubens Does
kubens works exactly the same way, but for namespaces. Run kubens to see all namespaces in your current cluster. Current namespace is highlighted. Type kubens kube-system to switch. Use kubens - to toggle back to your previous namespace.
There's one additional feature in kubens: the force flag. Sometimes you need to set a namespace that doesn't exist yet, maybe you're about to create it, or you're working with a namespace that will be created by a CI pipeline. kubens namespace-name -f forces the switch even if the namespace doesn't exist.
Both tools support tab completion in bash, zsh, and fish. This means you don't even need to remember the full context or namespace name. Type kubectx prod, hit tab, and it autocompletes to your production context.
Installation
The easiest method for macOS and Linux is Homebrew: brew install kubectx. This installs both tools and sets up shell completions automatically.
If you prefer kubectl plugins, you can use Krew: kubectl krew install ctx, and kubectl krew install ns. The tools then become kubectl ctx and kubectl ns.
For Debian and Ubuntu: sudo apt install kubectx. For Arch Linux: sudo pacman -S kubectx. Windows users have Chocolatey, Scoop, or winget options.
If you want the interactive fuzzy search, also install fzf. On macOS: brew install fzf. On Debian: sudo apt install fzf. Once fzf is in your path, kubectx and kubens automatically detect it and enable interactive mode.
If you have fzf installed but sometimes want the non-interactive output, set the environment variable KUBECTX_IGNORE_FZF=1. Or simply pipe the output to another command, like kubectx | cat, which also disables interactive mode.
Customization
You can change how the current context or namespace is highlighted. Set KUBECTX_CURRENT_FGCOLOR to change the foreground color, and KUBECTX_CURRENT_BGCOLOR for the background. These use tput color codes. For example, tput setaf 6 gives you cyan text.
If you prefer no colors at all, set the NO_COLOR environment variable. This follows the no-color.org standard that many command-line tools respect.
Workflow Tips
Pair kubectx with kube-ps1: kube-ps1 adds your current Kubernetes context and namespace to your shell prompt. Combined with kubectx and kubens, you always know where you are and can switch instantly.
Rename contexts immediately: Don't wait until you have twenty contexts with names like gke_project-id_region_cluster-name. Give them human-readable names: prod, staging, dev-local, client-a-prod.
Use the dash flag religiously: It becomes muscle memory. Checking something in production? kubectx prod. Done, back to development? kubectx -. You never need to remember where you were.
Organize with naming conventions: If you manage many clusters, consider naming like client-environment-region. Then fzf filtering becomes incredibly powerful: type "prod" to see all production clusters, type "client-a" to see all of one client's clusters.
Implementation Note
Originally, kubectx and kubens were bash scripts. Starting with version 0.9.0, they've been rewritten in Go. This means faster execution, better cross-platform support, and new features. The bash versions are still available if you need them, but the Go implementations are recommended. The behavior is identical, so existing scripts and muscle memory transfer directly.
Summary
- kubectx switches between Kubernetes contexts with a simple command
- kubens switches between namespaces
- Both support the dash flag to toggle to your previous selection
- Both integrate with fzf for interactive fuzzy search
- Shell completion works in bash, zsh, and fish
- Installation is one command via Homebrew, apt, pacman, or Krew
The key takeaway: Kubernetes cluster and namespace management doesn't have to be tedious. These two small tools remove friction from operations you perform hundreds of times a day. Install them, rename your contexts to something memorable, and your Kubernetes workflow immediately becomes faster and less error-prone.
For more information, visit the GitHub repository at github.com/ahmetb/kubectx.
License & Attribution
This AudioDocs episode is based on the official kubectx v0.9.5 documentation, licensed under the Apache License 2.0.
- Original Documentation: github.com/ahmetb/kubectx
- License: Apache License 2.0
- Copyright: © Ahmet Alp Balkan
This audio content is a derivative work that transforms the written documentation into an educational audio format. All technical content is derived from the original documentation with proper attribution as required by the license.
Other Versions
| Version | Status |
|---|---|
| v0.9.5 | Current |