ScriptsApr 16, 2026·3 min read

kubectx — Fast Kubernetes Context & Namespace Switching

Switch between Kubernetes clusters and namespaces instantly with kubectx and kubens, essential tools for multi-cluster workflows.

TL;DR
Switch Kubernetes clusters and namespaces instantly with kubectx and kubens CLI tools.
§01

What it is

kubectx is a pair of CLI tools (kubectx for cluster contexts, kubens for namespaces) that simplify switching between Kubernetes clusters and namespaces. Instead of typing kubectl config use-context my-long-cluster-name, you type kubectx my-cluster. Tab completion and fuzzy matching make navigation even faster across many clusters.

DevOps engineers and developers working with multiple Kubernetes clusters benefit most from kubectx. If you manage staging, production, and development clusters, kubectx eliminates the friction of context switching.

§02

How it saves time or tokens

kubectx saves seconds on every context switch, which adds up to minutes per day for multi-cluster operators. The previous context shortcut (kubectx -) lets you toggle between two clusters like cd - in a shell. kubens provides the same speed for namespace switching, avoiding the verbose kubectl config set-context commands.

§03

How to use

  1. Install kubectx via Homebrew or download the script
  2. Run kubectx to list contexts or kubectx <name> to switch
  3. Run kubens to list namespaces or kubens <name> to switch
§04

Example

# Install
brew install kubectx

# List all contexts
kubectx
# Output: dev-cluster, staging-cluster, prod-cluster

# Switch context
kubectx prod-cluster
# Switched to context 'prod-cluster'

# Switch back to previous
kubectx -
# Switched to context 'dev-cluster'

# Switch namespace
kubens monitoring
# Context 'dev-cluster' set to namespace 'monitoring'

# Interactive fuzzy selection (with fzf)
kubectx  # launches fzf picker if installed
§05

Related on TokRepo

§06

Common pitfalls

  • kubectx modifies your kubeconfig context; running commands in another terminal will use the switched context too
  • Renaming contexts with kubectx new=old is convenient but can confuse automation that relies on original names
  • kubens only works within the current context; switch context first if you need a different cluster's namespace

Frequently Asked Questions

What is the difference between kubectx and kubens?+

kubectx switches between Kubernetes cluster contexts (which cluster kubectl talks to). kubens switches between namespaces within the current context (which namespace kubectl targets by default). Both ship together in the kubectx package.

Does kubectx support fuzzy search?+

Yes. If fzf (fuzzy finder) is installed, running kubectx without arguments opens an interactive fuzzy picker. This is useful when you have many clusters with similar names.

Can I rename contexts with kubectx?+

Yes. Use `kubectx new-name=old-name` to rename a context. This is helpful for giving short, memorable names to clusters with auto-generated long names from cloud providers.

Does kubectx work with all Kubernetes distributions?+

Yes. kubectx works with any kubeconfig file, regardless of the Kubernetes distribution. EKS, GKE, AKS, k3s, minikube, and kind are all supported.

Is kubectx safe for production use?+

kubectx is safe in that it only modifies which context is active in your kubeconfig. However, switching to a production context means subsequent kubectl commands affect production. Use caution and consider visual indicators like kube-ps1 to show the current context in your prompt.

Citations (3)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets