I want to create a namespace and switch to created namespace in Kubernetes

9/2/2020

My requirement is: Add a namespace to achieve virtually cluster on a physical cluster. Also, segregate or isolate the service by namespace.

Is there any provision to switch the namespace on the Kubernetes cluster. So, My all subsequent request will go to the server with reference to the current namespace and resource creation also happen with reference to the current namespace. I don't want to mention namespace label in my deployment, service YAML file.

-- ompratap
kubectl
kubernetes
kubernetes-pod

1 Answer

9/2/2020

You could do it with kubectl config set-context --current --namespace <yournamespace but I personally do not like it. It can quickly lead to confusion.

I would recommend you to have a look at the concept of kustomize. With Kustomize, you define everything in a kustomization.yaml what should be applied (e.q. wich Namespace) to your service, deployment,etc. It makes it easier for you if you want to deploy an DEV and TEST version in different Namespaces. You only have to create a new kustomization.yaml with the new Namespace-Name and apply it.

You don't need any additional tooling because Kustomize is directly integrated into kubectl.

-- CLNRMN
Source: StackOverflow