Set a Default Namespace for Kubectl (And Easily Jump Between Namespaces)

A few quick and easy ways to switch contexts rapidly.

If you have several namespaces that you need to jump between, then you might find it helpful to make kubectl jump in and out of namespaces for you so that you don't have to constantly add the --namespace flag to every command. Here's the bare-bones command to do that.

kubectl config set-context --current --namespace=my-namespace

That's it. No frills. But if you're going to be switching a lot, keep reading! You will want to create some contexts that are more permanent so that you can simplify this a little. Here's how:

1. Create a Context

There are two primary ways to do this:

  • Modifying your kubeconfig file. (In Linux, it lives at ~/.kube/config, and in Windows, I think it lives at %USERPROFILE%\.kube\config, although I'm not sure about that one.)

Method 1: Using Kubectl

To create a context using kubectl, you really only need two commands (or three if you want to split hairs). First, this will retrieve information about your current context:

kubectl config get-contexts $(kubectl config current-context)

Now that you see some information about your context, let's borrow some values to create a new context. Here's the generic form of the command you'll be running:

kubectl config set-context [NAME] [--cluster=cluster_nickname] [--user=user_nickname] [--namespace=namespace]
  • Replace NAME with your choice for naming the context (I recommend the name of your namespace if you're using it to simply jump around namespaces.)
  • Replace cluster_nickname with the value in the CLUSTER column from the get-contexts result.
  • Replace user_nickname with the value in the AUTHINFO column fromthe get-contexts result.
  • Replace namespace with the name of the namespace you would like to map this new context to.

Once you run that command, you will now have a context you can use. Skip to the next section to see how to use it.

Method 2: Modify the Kube Config File

If you are using a standard installation of kubectl, you should have a file at ~/.kube/config. Open this file with your favorite editor. In the contexts: array, you should see something like this:

- context:
    cluster: your-cluster-name
    user: some-user-name
  name: context-name

Duplicate that block, and in the new block, change the name and add a line in the new block for namespace. Like this:

- context:
    cluster: your-cluster-name
    user: some-user-name
    namespace: my-new-namespace
  name: my-new-context-name

Save the file, and you're ready for the next step.

2. Set the Context

This one is simple. All you have to do is run this command:

kubectl config use-context my-new-context-name

That's it. You've created a context and switched to it. You now have a context that you can continue to use very conveniently.

3. Create a Bash Alias

To make things even easier, you can create a bash alias so that you don't have to type the full command above anymore. Here it is:

alias ns='k config use-context'

Now you can just run the following command to rapidly jump between namespaces.

ns my-namespace

Final Words

If you find yourself jumping around namespaces within your cluster, or even across clusters, you can use this technique to rapidly switch without having to type much at all. This is a great example of how you can reduce cognitive load so that you can focus on the more important tasks at hand.

Happy Kuberneting!