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:
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:
One can also check the details of newly created namespaces by accessing dashboard:
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.
In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…
Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…
With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…
Anxiety is a common mental health condition that affects millions of people around the world.…
In machine learning, confounder features or variables can significantly affect the accuracy and validity of…
Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…
View Comments
Excellent post and blog too, i recommend to you use https://disqus.com/ for better box comment.
THANKS!