kubectx & kubens¶
Fast switching between Kubernetes contexts and namespaces. Two tools in one: kubectx switches clusters, kubens switches namespaces. Saves massive amounts of typing. Interactive mode with fzf. Essential for multi-cluster workflows.
2026 Update
Both tools now support JSON output for scripting. Tab completion improved for all shells. Supports kubeconfig with multiple contexts. Integration with Starship prompt. Maintained by Ahmet Alp Balkan (Google).
Quick Hits¶
# Installation
brew install kubectx # macOS (includes kubens)
# Or: sudo apt install kubectx kubens # Debian/Ubuntu
# Manual: https://github.com/ahmetb/kubectx
# kubectx - Switch clusters
kubectx # List all contexts # (1)!
kubectx dev-cluster # Switch to dev-cluster
kubectx - # Switch to previous context # (2)!
kubectx -c # Show current context
kubectx new-name=old-name # Rename context # (3)!
kubectx -d dev-cluster # Delete context
# kubectx with fzf (interactive)
kubectx # Opens fuzzy finder # (4)!
# Use arrow keys, type to filter, Enter to select
# kubens - Switch namespaces
kubens # List all namespaces # (5)!
kubens production # Switch to production namespace
kubens - # Switch to previous namespace
kubens -c # Show current namespace
kubens default # Switch back to default
# kubens with fzf (interactive)
kubens # Opens fuzzy finder
# Type to search, Enter to switch
# Combined workflow examples
kubectx prod-cluster && kubens production # (6)!
kubectx staging && kubens default
# Shell aliases (add to .bashrc/.zshrc)
alias kctx='kubectx' # (7)!
alias kns='kubens'
alias k='kubectl'
- Shows list with current context highlighted
-switches to previous (likecd -in bash)- Rename context for easier reference (saves editing kubeconfig)
- If fzf installed, provides interactive fuzzy search
- Only shows namespaces in current context
- Chain with
&&to switch cluster and namespace together - Common aliases to save even more typing
Real talk:
- Saves hours of typing
kubectl config use-context - Interactive mode with fzf is game-changing
- Previous context (
-) is underrated lifesaver - Renaming contexts makes multi-cluster work sane
- Works with any kubectl-compatible tool (k9s, lens, etc.)
# Multi-cluster workflow
# Check production
kubectx production
kubens monitoring
kubectl get pods
kubectl logs -f prometheus-0
# Quick check dev
kubectx dev
kubectl get deployments
# Back to production
kubectx - # Returns to production
# Dev workflow with namespace switching
kubectx minikube
kubens development
kubectl apply -f app.yaml
kubens testing
kubectl apply -f app.yaml
kubens - # Back to development
# Scripting with kubectx/kubens
#!/bin/bash
# Save current context
ORIGINAL_CTX=$(kubectx -c)
ORIGINAL_NS=$(kubens -c)
# Do work in different cluster/namespace
kubectx prod-cluster
kubens production
kubectl apply -f deployment.yaml
# Restore original context
kubectx "$ORIGINAL_CTX"
kubens "$ORIGINAL_NS"
# Shell prompt integration
# Add to .bashrc or .zshrc
parse_kube_context() {
kubectx -c 2>/dev/null || echo "none"
}
parse_kube_namespace() {
kubens -c 2>/dev/null || echo "none"
}
# Bash PS1
export PS1='[\u@\h \W $(parse_kube_context):$(parse_kube_namespace)]\$ '
# Or use Starship prompt (better):
# https://starship.rs/config/#kubernetes
# Shows context and namespace automatically
# fzf integration for advanced filtering
# Requires fzf: brew install fzf
# kubectx automatically uses fzf if installed
kubectx # Interactive fuzzy search
# Filter contexts by pattern
kubectx | grep prod # Show only prod contexts
# Switch to context matching pattern (no fzf)
kubectx $(kubectx | grep -i prod | head -1)
# Custom fzf options
export FZF_DEFAULT_OPTS="--height 40% --reverse"
kubectx # Uses custom fzf layout
# Kubeconfig organization for kubectx
# ~/.kube/config (multi-cluster setup)
apiVersion: v1
kind: Config
current-context: dev-cluster # (1)!
clusters:
- name: dev-cluster
cluster:
server: https://dev.example.com:6443
- name: staging-cluster
cluster:
server: https://staging.example.com:6443
- name: prod-cluster
cluster:
server: https://prod.example.com:6443
contexts:
- name: dev-cluster # (2)!
context:
cluster: dev-cluster
user: dev-user
namespace: default # (3)!
- name: staging # (4)!
context:
cluster: staging-cluster
user: staging-user
namespace: default
- name: prod
context:
cluster: prod-cluster
user: prod-user
namespace: production # (5)!
users:
- name: dev-user
user:
token: <token>
- name: staging-user
user:
client-certificate-data: <cert>
client-key-data: <key>
- name: prod-user
user:
exec: # (6)!
apiVersion: client.authentication.k8s.io/v1beta1
command: aws
args:
- eks
- get-token
- --cluster-name
- prod-cluster
- Current context used by kubectl
- Context name shown in
kubectxlist - Default namespace for this context
- Short, memorable context names (rename with
kubectx) - Can set default namespace per context
- Dynamic token via exec (AWS EKS example)
Why this works:
- Context switching takes <1 second (vs 5+ seconds typing)
- Previous context (
-) enables rapid toggling - Interactive mode prevents typos
- Shell integration shows current context/namespace
- Works with any number of clusters/namespaces
Best Practices
- Install fzf - Makes kubectx/kubens interactive (essential)
- Rename contexts - Use
kubectx short=long-cluster-name-production - Aliases -
kctxandknssave typing - Prompt integration - Show current context/namespace in shell
- Starship prompt - Better than manual PS1 configuration
- Previous context - Use
-frequently (faster than typing name) - Namespace defaults - Set default namespace per context in kubeconfig
Security
- Production contexts - Rename with
prod-prefix for visibility - Namespace matters - Always verify namespace before applying
- Prompt indicators - Visual cues prevent production accidents
- Separate kubeconfigs - Use
KUBECONFIGenv var for isolation - Context validation - Add pre-flight checks in scripts
Productivity
- Muscle memory -
kubectx -andkubens -become instinctive - Tab completion - Works in bash, zsh, fish
- k9s integration - k9s respects kubectx/kubens changes
- Lens integration - Context switching syncs with Lens IDE
- IDE plugins - VS Code Kubernetes extension shows current context
Gotchas
- No kubectl - kubectx/kubens are wrappers, need kubectl installed
- Context != namespace - Switching context doesn't change namespace
- Default namespace - Defined in kubeconfig, not always
default - Multiple kubeconfigs - KUBECONFIG env var with
:separator - Prompt lag - Parsing context/ns in prompt can slow shell (use cache)
- Renamed contexts - Renames only local, not in cluster
- fzf required - Interactive mode needs fzf installed separately
Learning Resources¶
Official Docs¶
- kubectx GitHub - Source and documentation
- fzf GitHub - Fuzzy finder (recommended)
- Starship Prompt - Modern prompt with k8s support
Key Features¶
- Fast context switching - Sub-second cluster changes
- Fast namespace switching - Sub-second namespace changes
- Previous context/namespace - Toggle with
- - Interactive mode - Fuzzy search with fzf
- Context renaming - Simplify long context names
- Shell completion - Tab completion for bash/zsh/fish
Alternatives & Related Tools¶
- kubie - Context switching with shell isolation
- k9s - Terminal UI with built-in context/namespace switching
- Lens - Desktop IDE with visual context switching
- kubectl ctx/ns plugins - Native kubectl plugins
Last Updated: 2026-02-02 | Vibe Check: Essential Tool - Must-have for anyone managing multiple clusters. Saves hours every week. Interactive mode with fzf is perfect. Zero learning curve. Install immediately.
Tags: kubectx, kubernetes, productivity, cli, kubectl