Figure 2. kubernetes dashboard displaying newly created namespaces
This article represents concepts related to Kubernetes Namespace and how to create/delete the namespaces. The following topics are discussed in this article:
Kubernetes namespace can be seen as a logical entity used to represent cluster resources for usage of a particular set of users. This logical entity can also be termed as a virtual cluster. One physical cluster can be represented as a set of multiple such virtual clusters (namespaces). The namespace provides scope for names. Names of resource within one namespace need to be unique.
By default, Kubernetes starts with the following three namespaces:
The following command can be used to get a list of all namespaces:
kubectl get namespaces
The following command displays namespace with labels
kubectl get namespaces --show-labels
The namespaces list can be accessed in Kubernetes dashboard as shown in the diagram below:
Figure 1. Kubernetes Namespaces
In real-world scenarios, one can create namespace for development (dev), testing (QA) and production (prod) environment. The objects in dev/QA namespace such as pods, services, and deployments will be available for developers/testers respectively to build and run the applications. There will be lesser restrictions on modifying the resources in dev/QA namespaces. In production (prod) namespace, there will be greater control on who could manage the resources.
In this article, we will look into creation/deletion of the namespaces for dev/QA/prod.
Let’s create namespaces for development/QA/prod environments. The following would be required to be done:
Development Namespace: Save the filename as namespace-dev.json.
{ "kind": "Namespace", "apiVersion": "v1", "metadata": { "name": "dev", "labels": { "name": "dev" } } }
QA Namespace: Save the filename as namespace-qa.json
{ "kind": "Namespace", "apiVersion": "v1", "metadata": { "name": "qa", "labels": { "name": "qa" } } }
Production Namespace: Save the filename as namespace-prod.json
{ "kind": "Namespace", "apiVersion": "v1", "metadata": { "name": "prod", "labels": { "name": "prod" } } }
The following commands create namespaces for Dev/QA/Prod environments:
# Namespace for Developers kubectl create -f namespace-dev.json # Namespace for Testers kubectl create -f namespace-qa.json # Namespace for Production kubectl create -f namespace-prod.json
Check whether the namespaces got created by executing the following command:
kubectl get namespaces --show-labels | grep name=
The following represents the output of above command:
Figure 2. kubectl get namespace details
One can also check the details of newly created namespaces by accessing dashboard:
Figure 3. kubernetes dashboard displaying newly created namespaces
Once the namespaces have been created, in order to have kubectl commands work with a specific namespace, the following needs to be done:
# Assign dev context to development namespace kubectl config set-context dev --namespace=dev --cluster=minikube --user=minikube # Assign qa context to QA namespace kubectl config set-context qa --namespace=qa --cluster=minikube --user=minikube # Assign prod context to production namespace kubectl config set-context prod --namespace=prod --cluster=minikube --user=minikube
Execute the following commands to switch to the specific context. Once switched to a context, any execution of kubectl command would create/update/delete objects in that namespace.
# Switch to Dev context kubectl config use-context dev # Switch to QA context kubectl config use-context qa # Switch to Prod context kubectl config use-context prod
Just to check what is current context, execute the following command:
kubectl config current-context
Once switched to a context, commands such as following would give objects detail for that namespace:
kubectl get pods kubectl get deployments
The command below would delete the namespaces.
# Delete dev namespace kubectl delete namespaces dev # Delete qa namespace kubectl delete namespaces qa # Delete prod namespace kubectl delete namespaces prod
The above commands execute in asynchronous mode. Thus, the status of the namespace would show up as terminating until the deletion gets completely deleted.
Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…
Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…
Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…
Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…
The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…
Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…
View Comments
Excellent post and blog too, i recommend to you use https://disqus.com/ for better box comment.
THANKS!