kubectx is a powerful command-line tool for switching between Kubernetes contexts (clusters) quickly and efficiently.
Overview
Thekubectx command can be invoked as either:
kubectx- standalone binarykubectl ctx- as a kubectl plugin
Commands
List Contexts
Lists all available contexts from your kubeconfig file.
- If
fzfis installed and output is to a terminal, launches interactive fuzzy search mode - Otherwise, displays a list of all contexts with the current context highlighted
- The current context is marked with a different color (customizable via environment variables)
Switch Context
Switches to the specified context.
<NAME>- The name of the context to switch to
Switch to Previous Context
Switches back to the previously active context.
- Maintains a state file tracking the previous context
- Allows quick toggling between two contexts
- Similar to
cd -in shell navigation
Show Current Context
Displays the name of the current active context.
-c, --current
Usage:
- Shell scripting and automation
- Display in shell prompts (PS1)
- CI/CD pipeline validation
Rename Context
Renames an existing context to a new name.
<NEW_NAME>- The desired new name for the context<NAME>- The existing context name to rename, or.for current context
- If the new name already exists, it will be overwritten with a warning
- If renaming the current context, the current-context preference is updated
- Does not affect the cluster or user entries in kubeconfig
Unset Current Context
Removes the current-context preference from kubeconfig.
-u, --unset
Usage:
- Clears the
current-contextfield in kubeconfig - kubectl commands will require explicit
--contextflag afterward - Does not delete any context entries
Delete Context
Deletes one or more context entries from kubeconfig.
-d
Syntax:
<NAME>- One or more context names to delete.- Special value to delete the current context
- Can delete multiple contexts in a single command
- If
fzfis installed and no names provided, launches interactive deletion mode - Does not delete the associated user or cluster entries
- Warns if you delete the current context
Display Help
Shows usage information and command reference.
-h, --help
Usage:
Display Version
Shows the version of kubectx.
-V, --version
Usage:
Interactive Mode
Whenfzf is installed and stdout is a terminal, kubectx launches interactive mode for certain commands:
Interactive Context Selection
Runningkubectx without arguments opens an interactive fuzzy finder:
Interactive Context Deletion
Runningkubectx -d without context names opens interactive deletion:
Disabling Interactive Mode
To disablefzf integration:
Environment Variables
Color Customization
Foreground color for the current context indicator.
Background color for the current context indicator.
Disable Colors
Disables all color output when set to any value.
Disable fzf Integration
Disables interactive mode with fzf when set to 1.
Exit Codes
| Code | Description |
|---|---|
0 | Success |
1 | Error occurred (invalid context, file error, etc.) |
Shell Completion
kubectx supports tab completion for:
- bash
- zsh
- fish
Integration with kubectl
When installed as a kubectl plugin via Krew, use:State Management
kubectx stores the previous context in a state file to enable the kubectx - functionality:
- Location:
~/.kube/kubectx - Format: Plain text file containing the name of the previous context
- Automatic: Created and managed automatically
Common Use Cases
Quick Context Switching
Switch between development and production:Simplify Long Context Names
Rename lengthy auto-generated names:Clean Up Old Contexts
Remove contexts for deleted clusters:Scripting and Automation
Save current context before switching:Notes
- Context deletion does not remove the associated cluster and user entries from kubeconfig
- Renaming a context only changes the context entry name, not the cluster or user it references
- The
kubectx -command requires at least one previous context switch in the current session