kubens is a powerful command-line tool for switching between Kubernetes namespaces quickly and efficiently within the current context.
Overview
Thekubens command can be invoked as either:
kubens- standalone binarykubectl ns- as a kubectl plugin
Commands
List Namespaces
Lists all namespaces in the current Kubernetes context.
- If
fzfis installed and output is to a terminal, launches interactive fuzzy search mode - Otherwise, displays a list of all namespaces with the current namespace highlighted
- The current namespace is marked with a different color (customizable via environment variables)
- Requires connection to the Kubernetes cluster to fetch namespace list
Switch Namespace
Changes the active namespace for the current context.
<NAME>- The name of the namespace to switch to
- By default, validates that the namespace exists in the cluster
- Updates the
namespacefield for the current context in kubeconfig - All subsequent kubectl commands will use this namespace by default
Force Switch Namespace
Changes the active namespace without validating its existence.
-f, --force
Syntax:
<NAME>- The name of the namespace to switch to--forceor-f- Skip namespace existence validation
- Does not check if the namespace exists in the cluster
- Useful for setting up namespaces that will be created later
- Updates kubeconfig even if cluster is unreachable
- Prepare configuration before namespace creation
- Work with namespaces in disconnected environments
- Automate setup scripts that create namespaces after setting them
Switch to Previous Namespace
Switches back to the previously active namespace in the current context.
- Maintains a state file tracking the previous namespace per context
- Allows quick toggling between two namespaces
- Similar to
cd -in shell navigation - Previous namespace is context-specific
Show Current Namespace
Displays the name of the current active namespace.
-c, --current
Usage:
- Shell scripting and automation
- Display in shell prompts (PS1)
- CI/CD pipeline validation
- Verify namespace before executing commands
Display Help
Shows usage information and command reference.
-h, --help
Usage:
Display Version
Shows the version of kubens.
-V, --version
Usage:
Interactive Mode
Whenfzf is installed and stdout is a terminal, kubens launches interactive mode:
Interactive Namespace Selection
Runningkubens without arguments opens an interactive fuzzy finder:
- Real-time fuzzy search filtering
- Current namespace highlighted
- Arrow key navigation
- Multi-character search patterns
Disabling Interactive Mode
To disablefzf integration:
Environment Variables
Color Customization
Foreground color for the current namespace indicator.
Background color for the current namespace indicator.
These environment variables are shared with
kubectx and affect both tools.Disable Colors
Disables all color output when set to any value.
Disable fzf Integration
Disables interactive mode with fzf when set to 1.
This environment variable is shared with
kubectx and affects both tools.Exit Codes
| Code | Description |
|---|---|
0 | Success |
1 | Error occurred (invalid namespace, cluster unreachable, etc.) |
Shell Completion
kubens supports tab completion for:
- bash
- zsh
- fish
- Tab completion for namespace names
- Dynamic completion from current cluster
- Flag and option completion
Integration with kubectl
When installed as a kubectl plugin via Krew, use:State Management
kubens stores the previous namespace per context in a state file:
- Location:
~/.kube/kubens/ - Format: Separate files for each context, named by context name
- Content: Plain text containing the name of the previous namespace
- Automatic: Created and managed automatically
Common Use Cases
Quick Namespace Switching
Toggle between application and system namespaces:Development Workflow
Switch between development and testing namespaces:Pre-create Namespace Configuration
Set namespace before it exists:Scripting and Automation
Save and restore namespace in scripts:Multi-tenant Clusters
Quickly navigate between team namespaces:Comparison with kubectl
Traditional kubectl approach vs kubens:- Shorter, easier to remember command
- Interactive mode with fuzzy search
- Quick toggling with
- - Tab completion for namespace names
- Visual indication of current namespace
Notes
- Namespace changes are context-specific and persist in kubeconfig
- The previous namespace is tracked separately for each context
- Without
--force, kubens validates namespace existence by querying the cluster - When switching contexts with
kubectx, the namespace for that context is automatically used - Use
kubens -cto verify the current namespace before running destructive operations