Hi let us understand about Namespace. Namespace is nothing but a way of
consolidating the objects together, so that the management is going to be
much easier. I can consolidate all the similar objects or the object needs to be
accessed by a team or a group of users, I can put in within a namespace. So
namespace going to act like an abstract layer where I can apply the permissions
or resource utilization all such criterias I can apply against the namespace and all
the objects within the namespace will adhere to that particular policy. Let us
understand more about namespace, let's go ahead and do a demo. I am into the
minikube installation I can use the command kubectl get ns or namespace
that's going to list all the name space available within the installation. These are
all the default installation got installed during the installation of minikube and
by default all the objects will get added into the default namespace. I can go
ahead and create my own namespace as well and I can get all the objects
within all the namespace for example if I want to get all the pods, I can say get
pods and provide all namespace -namespaces. So it's going to list all the pods available across
all namespaces and it's going to list the namespace as well, for example this
particular pod is available as a part of the kube system namespace. I can
go ahead and create my own namespace and make the default
namespace as a different one than the default. Let's go ahead and check the
yaml file to create the namespace. I do have a yaml file where I have mentioned
the kind as namespace and I have provided the name as well. So a
namespace with this specific name my-ns-1 will get created. Let me go ahead and
create the namespace using this specific file, I'll provide the command
kubectl create-f and then the yaml file name. The namespace got created. Now
I'm going to use another yaml file where it is going to create a namespace my-ns-2
Let me go ahead and execute the yaml file, so the name space would have got
created. I can go ahead and list the namespaces available. I do have these
two namespaces, that got created very recently and I can go ahead and start a
pod or any object within the specific namespace. If I doesn't provide the
namespace parameter it will get created within the default namespace, I'm going
to create a pod with the name my-redis and it is going to use the image redis and
it'll get created within the namespace my-ns-1, don't worry about the deprecated
warning or otherwise I can go ahead and use the option create. I'm going to create
another object with the name my-nginx that will get created within another
namespace my-ns-2. Now I can go ahead and get all the deployments the
run command would have created a deployment, now let me go ahead and list
the deployments available. There are no deployments the reason because if I
doesn't provide the name space it is going to list the deployments available
within the default namespace but the deployments got created in a specific
name space and I can provide the option --all namespace, so that its going to list all
the deployments in all the namespace. Here I do have a deployment and that is
available in this specific name space and I can get the deployment specific to
a namespace by providing the parameter --namespace and then the name of the
namespace. So it's going to list the deployment available within this specific
namespace and whenever I'm working on the kubectl by default it is going to
point to the default namespace, I can go ahead and change that default
namespace to a different namespace. For that I will be setting a context let me
go ahead and set the context and that can be viewed from the config command
let me go ahead and view the default config available using the command
kubectl config view, so here what cluster the kubectl is going to work on and what
are all the context, so the cluster name, user all the details will be available. I can
set and create a new context by using the config command, let me go
ahead and create a new config for the namespace. So I'm using the command
kubectl config and set the context to my context 1 where it is going to use the
my-ns-1 as the default namespace and minikube as the cluster and user as
minikube. So the context is not available, so it's going to get created, I can see the
details about this specific context and I can change the current context to this
specific custom context. Let me go ahead and check the current context that is
minikube, I am going to change that to context 1. So I am changing or switching
the context from minikube to my context 1 the one that we created. Now let me go
ahead and list the deployments, so it's going to list the deployments available in
the context, that is the current context that I am using. So in case if I wanted to
see what context or the configuration I am working on I can use the option
kubectl config view. So that's going to provide the information on what context
that, this particular shell is pointing it to and the cluster, username other details
will be available. The same way I can go ahead and create multiple context as
well and this is very similar to the workspace we are creating for individual
users or for the team. Let me go ahead and create another context which is
going to point to the namespace 2. So I have created another context and the
shell is using the context 1 and whenever I wanted to see the objects
available within that specific context I should be in a position to see that.
In case if I wanted to see any objects available in another namespace then
I have to provide the parameter --namespace, the same way if I try to see
details about an object available in another namespace, I'm going to get the
error message because currently it is pointing to the namespace 1 and
my nginx is available as a part of namespace 2. So if I change the context
and if I see the details about that specific deployment my nginx then it
should be visible. So this is very similar to how we have the user object or the home
directory available as a part of operating system and I can go ahead and delete
any specific namespace and see what's going to happen? Let me go ahead and
delete the name space 2. I'm trying to list all the deployments and all the
namespace and I will not have the deployment as well, so deleting a
namespace is equivalent to deleting all other objects available as a part of the
namespace as well. So handling the namespace should be very careful
reason because while deleting the name space it is going to delete all the objects
available within the namespace as well. For cleaning purpose let me go ahead and
delete the other namespace as well and namespace 1 is also deleted and I can go
ahead and get the deployments available as a part of minikube
installation minikube context is the default context. I can go ahead and the switch
the context to minikube using the option use context and the context will get
switched to minikube. Now all the commands by default it is going to point
to the default namespace. As a part of advanced configurations we can go
ahead and do the resource limitations for the namespace as well as updating
and changing the name space, resource utilization and access control to the
namespaces all these details we will be seeing it as a part of advanced
discussion of namespace. So in a quick summary we have seen how to use
namespace, create a new namespace and switch the context to the new
namespace and the consequence of deleting a namepace
by having some objects within the namespace.