Skip to content
Latchkey

kubens: Switch the Active Namespace

kubens <namespace> sets the default namespace on the current context, so later kubectl commands skip the -n flag.

kubens is the namespace sibling of kubectx. Set the namespace once and every kubectl in the job targets it without -n.

What it does

kubens writes the namespace field of the current context in kubeconfig. With no argument it lists all namespaces and highlights the active one; - switches back to the previous namespace.

Common usage

Terminal
kubens                  # list namespaces, active one highlighted
kubens payments         # make "payments" the default
kubens -                # back to the previous namespace
kubectl get pods        # now scoped to payments, no -n needed

Options

FormWhat it does
kubensList namespaces
kubens <name>Set the default namespace
kubens -Switch to the previous namespace
kubens -c, --currentPrint the current namespace only

In CI

Run kubens <ns> early in a job so subsequent kubectl, helm, and stern calls inherit it. Note it edits kubeconfig, so on a shared runner give each job its own KUBECONFIG copy to avoid cross-job namespace bleed.

Common errors in CI

error: no namespace exists with the name: "payments" means the namespace is not yet created in that cluster; create it or check the spelling. If kubeconfig is read-only (a common mounted-secret setup), kubens fails to persist with permission denied; pass -n per command instead.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →