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]
NAMEwith your choice for naming the context (I recommend the name of your namespace if you're using it to simply jump around namespaces.)
cluster_nicknamewith the value in the
CLUSTERcolumn from the get-contexts result.
user_nicknamewith the value in the
AUTHINFOcolumn fromthe get-contexts result.
namespacewith 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:
Duplicate that block, and in the new block, change the name and add a line in the new block for
namespace. Like this:
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.
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.