kubectl-ns icon indicating copy to clipboard operation
kubectl-ns copied to clipboard

A kubectl plugin for interactively switching between namespaces in your cluster

kubectl ns

A kubectl plugin for interactively switching the current namespace:

Demo

This makes it easier to work with different namespaces in the same cluster.

Also see kubectl-ctx for switching between contexts (e.g. clusters).

Installation

You can install the plugin by following these simple steps:

  1. Download the kubectl-ns script:

    curl -O https://raw.githubusercontent.com/weibeld/kubectl-ns/master/kubectl-ns
    
  2. Make it executable:

    chmod +x kubectl-ns
    
  3. Move it to any directory in your PATH (you might want to create a ~/.kubectl-plugins folder for all your kubectl plugins):

    mv kubectl-ns ~/.kubectl-plugins
    

Now, you can verify that the plugin is correctly installed by running the following command and checking that the kubectl-ns script is included in the output:

kubectl plugin list

To uninstall the plugin, simply delete the kubectl-ns script.

Dependencies

The plugin depends on the fzf command being available on your system.

You can install fzf as follows:

  • Homebrew (macOS) and Linuxbrew (Linux):
    brew install fzf
    
  • From source (macOS and Linux):
    git clone https://github.com/junegunn/fzf.git ~/.fzf
    ~/.fzf/install
    
  • For further installation options, see here

Usage

Interactively change the current namespace:

kubectl ns

List all namespaces:

kubectl ns -l

Update the namespaces cache:

kubectl ns -u

Cache

The cache feature speeds up the kubectl ns and kubectl ns -l commands.

This is because these commands need to retrieve the full list of namespaces, which is usually only stored in the cluster and thus needs to be retrieved from the API server. Without the cache, these commands would take as long as it takes to execute kubectl get namespaces. With the cache, they are blazingly fast, as they get the full list of namespaces from the local cache rather than from the API server.

The cache is created automatically when running kubectl ns or kubectl ns -l for the first time with a given cluster (cache location is ~/.kube/kubectl-ns). You can manually update the cache at any time by running kubectl ns -u.

Note that there are no automatic updates of the cache, so you should run kubectl ns -u whenever you create or delete namespaces.

How It Works

When you run kubectl ns, the plugin changes your local kubeconfig configuration as shown below:

How it works

As you can see, the plugin changes the namespace element of the current context in your kubeconfig configuration (the plugin always works on the current context).

This means that the next time you use kubectl, it will use the new namespace for the same cluster.

Note that the ns command physically changes one of your kubeconfig files (the default kubeconfig file is ~/.kube/config, but you can have multiple kubeconfig files by by listing them in the KUBECONFIG environment variable).

The ns -l command displays the namespaces of the cluster which is referenced by your current context.