Kubectx & Kubens

kubectx and kubens cheatsheet — switch Kubernetes contexts and namespaces instantly. kubectx prod, kubens kube-system, kubectx -, kubens -. Fast K8s context management.

4 min read

What it is

kubectx and kubens are command-line tools to quickly switch between Kubernetes clusters and namespaces, respectively, and to see which ones you’re currently using.

Installation

Linux

Using Homebrew (recommended):

brew install kubectx

Manual download:

  1. Download the appropriate binary from the releases page.
  2. Make it executable: chmod +x kubectx_linux_amd64
  3. Move it to your PATH: sudo mv kubectx_linux_amd64 /usr/local/bin/kubectx
  4. Repeat for kubens.

Mac

Using Homebrew (recommended):

brew install kubectx

Windows

Using Chocolatey:

choco install kubectx

Using Scoop:

scoop install kubectx

Manual download:

  1. Download the appropriate binary from the releases page.
  2. Add the directory containing the binary to your system’s PATH environment variable.

Core Concepts

  • Context: A Kubernetes context is a named combination of a cluster, a user, and a default namespace. kubectl config use-context <context-name> switches your active context. kubectx manages these contexts.
  • Namespace: A Kubernetes namespace provides a mechanism for isolating groups of resources within a single cluster. kubectl config set-context --current --namespace=<namespace-name> sets the default namespace for the current context. kubens manages these namespaces within the current context.

Commands / Usage

Managing Contexts (kubectx)

List available contexts:

kubectx

Lists all contexts defined in your ~/.kube/config file.

Switch to a context:

kubectx my-production-cluster

Switches your active kubectl context to my-production-cluster.

View current context:

kubectx

When run without arguments, it shows the current context with an asterisk.

Create a new context alias (for convenience):

kubectx staging=staging-cluster-12345

Creates a shortcut staging that points to staging-cluster-12345. Subsequent kubectx staging calls will switch to staging-cluster-12345.

Delete a context alias:

kubectx --del staging

Removes the staging alias.

View context details:

kubectx --show-details

Shows detailed information about each context, including the cluster, user, and namespace.

View current context details:

kubectx --current-context

Displays the name of the currently active context.

Managing Namespaces (kubens)

List available namespaces:

kubens

Lists all namespaces in the current Kubernetes cluster. The current namespace is indicated with an asterisk.

Switch to a namespace:

kubens kube-system

Switches your active kubectl context’s default namespace to kube-system.

View current namespace:

kubens

When run without arguments, it shows the current namespace with an asterisk.

Create a new namespace alias (for convenience):

kubens dev=development-team-ns

Creates a shortcut dev that points to development-team-ns. Subsequent kubens dev calls will switch to development-team-ns.

Delete a namespace alias:

kubens --del dev

Removes the dev alias.

View namespace details:

kubens --show-details

Shows detailed information about each namespace.

View current namespace details:

kubens --current-namespace

Displays the name of the currently active namespace for the current context.

List all namespaces and their contexts:

kubens --all-contexts

Lists all namespaces across all contexts, prefixed with the context name.

Common Patterns

Quickly switch context and then namespace:

kubectx production && kubens app-prod-1

Switches to the production context and then sets the default namespace to app-prod-1.

See current context and namespace:

echo "Current context: $(kubectx)"
echo "Current namespace: $(kubens)"

A simple way to display your current Kubernetes environment.

List pods in the current namespace of a specific context without switching:

kubectl --context my-cluster --namespace my-namespace get pods

While kubectx and kubens are for switching, you can always use kubectl with explicit --context and --namespace flags.

Create a namespace alias for a frequently used namespace:

kubens my-long-namespace-name=mlnn
kubens mlnn

This is useful for shortening long namespace names you use often.

Gotchas

  • kubectx operates on contexts, kubens operates on namespaces within the current context. You cannot use kubens to switch namespaces in a context you are not currently using.
  • Aliases are local to your ~/.kube/config file. They are not shared across different machines.
  • kubectx does not create or delete contexts. It only switches between existing ones defined in your kubeconfig. Similarly, kubens does not create or delete namespaces; it only switches the default for the current context.
  • If you have multiple kubeconfig files merged, kubectx and kubens will operate on the combined set of contexts and namespaces.
  • The --current-context and --current-namespace flags for kubectx and kubens respectively, are primarily for scripting. They output just the name, which can be captured.