kubectx & kubens Switch context and namespace

Version 0.11.0 · source

Start here

kubectx
List or pick a context
kubectx <name>
Switch cluster
kubectx -
Back to previous context
kubens
List or pick a namespace
kubens <name>
Switch namespace
kubens -
Back to previous namespace
kubectx -c
Show current context
kubectx <new>=<old>
Rename a context

Concepts

kubeconfig ~/.kube/config or $KUBECONFIG
Context (kubectx) cluster + user
Namespace (kubens) default for kubectl
What each tool changes

Contexts (kubectx)

kubectx
List contexts, or pick with fzf
kubectx <name>
Switch to a context
kubectx -
Switch to the previous context
kubectx -c
Show the current context
kubectx <new>=<name>
Rename a context
kubectx <new>=.
Rename the current context
kubectx -d <name> [<name>...]
Delete one or more contexts
Only the context entry goes; the cluster and user entries it referenced stay in the kubeconfig.
kubectx -u
Unset the current context
kubectx --version
Print the version

Namespaces (kubens)

kubens
List namespaces, or pick with fzf
kubens <name>
Switch the active namespace
kubens -
Switch to the previous namespace
kubens -c
Show the current namespace
kubens <name> -f
Switch even if it does not exist yet
kubens -u
Unset namespace, back to default
kubens --version
Print the version

Isolated shells

kubectx -s <name>
Shell scoped to one context
Switching context inside this shell does not touch other terminals; exit returns you to where you were.
kubectx -s
Pick the context for a scoped shell
kubectx -r <name>
Read-only shell for a context
Write operations are blocked, which makes it a safer way to look around production.
kubectx -r
Pick the context for a read-only shell

Interactive mode

kubectx | cat
Plain list even with fzf installed
export KUBECTX_IGNORE_FZF=1
Turn off the fzf menu
export NO_COLOR=1
Turn off coloured output
export KUBECTX_CURRENT_FGCOLOR=$(tput setaf 6)
Text colour of the current item
export KUBECTX_CURRENT_BGCOLOR=$(tput setab 7)
Background colour of the current item

Install and setup

brew install kubectx
Install with Homebrew
sudo apt install kubectx
Install on Debian or Ubuntu
kubectl krew install ctx ns
Install as kubectl plugins
They then run as kubectl ctx and kubectl ns.
sudo pacman -S kubectx
Install on Arch
winget install --id ahmetb.kubectx
Install on Windows (kubens: ahmetb.kubens)
alias kx=kubectx kn=kubens
Shorter names

Multiple kubeconfigs

export KUBECONFIG=~/.kube/a.yaml:~/.kube/b.yaml
Merge several files into one view
kubectx then lists the contexts of all files; a switch is written to the first file.
KUBECONFIG=<file> kubectx
Use one file for a single call
kubectl config view --flatten
Merge all files into one document

Recipes

Hop between two clusters
kubectx prod-eukubens paymentskubectx -   # back to the previous clusterkubectx -   # and forth again
Short context names
kubectx -ckubectx prod=gke_acme_europe-west1_prod-clusterkubectx prod
Safe look at production
kubectx -r prodkubectl get pods -A   # reads workkubectl delete pod <name>   # blocked
Two clusters in two terminals
# terminal 1kubectx -s staging# terminal 2kubectx -s prod