Change namespaces in Kubernetes

Easy way to change namespace in Kubernetes

Suraj Deshmukh

3 minute read

There is no easy way to change namespace in Kubernetes using kubectl command line utility. But here are some commands that you can alias in your bashrc file so that it’s just a single command that you can use to change the namespace in the Kubernetes cluster.

Change namespace

Let’s see step by step what goes in to change the namespace. So the first step is to find the context.

A context element in a kubeconfig file is used to group access parameters under a convenient name. Each context has three parameters: cluster, namespace, and user. By default, the kubectl command-line tool uses parameters from the current context to communicate with the cluster. Read more.

$ kubectl config current-context 

Now I have a namespace which I have created and want to switch to it.

$ kubectl create ns mywebapp
namespace "mywebapp" created

Now we have two needed information to change the current default namespace, context and namespace name.

$ kubectl config set-context minikube --namespace mywebapp
Context "minikube" modified.

Notice that we have put the context here as minikube and namespace name as we have created, mywebapp.

So this can be put in one command and used conveniently. Just copy following bash function into your bashrc file and you will have change-ns as a command available.

function change-ns() {
    if [ -z $namespace ]; then
        echo "Please provide the namespace name: 'change-ns mywebapp'"
        return 1

    kubectl config set-context $(kubectl config current-context) --namespace $namespace

Verify change of namespace

How do you verify that the namespace is changed? How do you find what is the current namespace? Run following command:

kubectl get sa default -o jsonpath='{.metadata.namespace}'

Let’s deconstruct that. Every namespace that is created has a ServiceAccount created by default with name default (Read more).

For any object/artifact in Kubernetes there is a field called namespace inside it’s metadata field. Which tells you about what namespace a particular object is part of. If you don’t set it while creating, Kubernetes will set it to the current default namespace. So this is a source of knowing what current namespace is.

Look at following pod object definition, the namespace for pod storage-provisioner is kube-system.

$ kubectl get pod storage-provisioner -o yaml
apiVersion: v1
kind: Pod
  creationTimestamp: 2018-06-10T09:56:24Z
  name: storage-provisioner
  namespace: kube-system

Now you can use the flag -o with jsonpath type of input to tell what specific object you would like to fetch information about, in our case we want metadata.namespace.

The above command can also be converted to one command, just add following snippet to bashrc and you will have a command current-ns.

function current-ns() {
	kubectl get sa default -o jsonpath='{.metadata.namespace}'

Hope that helps, you with being productive with Kubernetes. Happy hacking!

comments powered by Disqus