The problem

You work with multiple Kubernetes clusters — maybe dev, staging, and production. Or you juggle several namespaces in the same cluster. Every context switch looks like this:

kubectl config use-context my-company-production-cluster-eu-west-1
kubectl config set-context --current --namespace=monitoring

Typing that dozens of times a day is slow, error-prone, and frankly annoying. One typo and you are running a command against the wrong cluster.

Enter kubectx and kubens

kubectx and kubens are two small, focused tools by Ahmet Alp Balkan that solve exactly this:

  • kubectx — switch between Kubernetes contexts (clusters) instantly
  • kubens — switch between namespaces instantly

Both support interactive fuzzy search via fzf — type a few letters, pick from a list, done.

They do not replace kubectl itself. They only change what your kubeconfig points at — the same change you would make with kubectl config, just faster.

Installation

Via Krew:

kubectl krew install ctx
kubectl krew install ns

After Krew, the commands are available as kubectl ctx and kubectl ns depending on your setup; many people still call the standalone binaries kubectx and kubens when installed via package managers.

Via Homebrew (macOS/Linux):

brew install kubectx

This installs both kubectx and kubens.

Manual install:

git clone https://github.com/ahmetb/kubectx.git ~/.kubectx
export PATH="$PATH:$HOME/.kubectx"

Optional but recommended — fzf for interactive selection:

brew install fzf
# or on Debian/Ubuntu
sudo apt install fzf

Basic usage

kubectx

# List all contexts
kubectx

# Switch to a context
kubectx my-production-cluster

# Switch back to previous context
kubectx -

# Rename a context (shorter name for daily use)
kubectx prod=my-company-production-cluster-eu-west-1

# Delete a context
kubectx -d old-cluster

Renaming is underrated. Long GKE or EKS context names are accurate and miserable to type. A short alias you understand beats copying from a wiki every morning.

kubens

# List all namespaces
kubens

# Switch to a namespace
kubens monitoring

# Switch back to previous namespace
kubens -

kubens updates the current context’s default namespace in kubeconfig — the same as kubectl config set-context --current --namespace=.... Your next kubectl get pods without -n uses that namespace.

With fzf installed, run kubectx or kubens with no arguments and get an interactive picker: arrow keys, fuzzy search, Enter to confirm.

The real power: aliases

Many engineers shorten these in their shell config:

alias kx='kubectx'
alias kn='kubens'

Now switching clusters can be two keystrokes plus a short name. Pair that with a prompt that shows current context and namespace (Starship, kube-ps1, or similar) and wrong-env mistakes get easier to spot.

Why this matters beyond convenience

Working on the wrong cluster is a category of mistake that has caused real production incidents. When context switching is slow and painful, people avoid it — and end up running commands in the environment that was already open.

kubectx and kubens make the right behaviour the easy behaviour. That is safety culture applied to tooling, not just ergonomics.

They complement other kubectl helpers. Once you are on the right cluster and namespace, kubectl tree helps with ownership inside that scope; kubectl essentials covers the verbs you run after the switch.

Quick reference

Installkubectl krew install ctx ns or brew install kubectx
kubectxSwitch between clusters / contexts
kubensSwitch default namespace on current context
Bonusfzf for interactive fuzzy selection
Aliaseskx for kubectx, kn for kubens (common convention)

Two tools. Installed in a few minutes. You will wonder how you worked without them — or you will at least stop pasting full context names from the cloud console.