Hello and welcome to this lecture in this lecture.
We will discuss about namespaces in kubernetes, let us begin with an analogy.
There are two boys named Mark to differentiate them from each other.
We call them by their last names.
Smith and Williams they come from different houses.
Of course the Smiths and the Williams There are other members in the house.
The individuals within the house address each other simply by their first names.
For example the father addresses Mark simply as Mark.
However if the father wishes to address the mark in the other house he would use the full name someone
outside of these houses would also use the full name to refer to the boys or anyone within these houses.
Each of these houses have their own set of rules that defines who does what each of these houses have
their own set of resources that they can consume.
Now let's get back to kubernetes these houses correspond to namespaces in kubernetes.
So far in this course we've created objects such as pods deployments and services in our cluster.
Whatever we have been doing we have been doing within a namespace.
We were inside a house all this while this namespace is known as the default namespace and it is created
automatically kubernetes, when the cluster is first set up kubernetes, creates a set of pods
and services for its internal purpose such as those required by the networking solution.
The DNS service etc. to isolate these from the user and to prevent you from accidentally deleting or
modifying these services kubernetes creates them under another namespace created at cluster startup
named Kube system a third namespace created by Coburn that is automatically is called kube public.
This is where resources that should be made available to all users are created.
If your environment is small or your learning and playing around with a small cluster you shouldn't
really have to worry about namespaces you could continue to work in the default namespace.
However as and when you grow and use a kubernetes cluster for enterprise or production purposes you
may want to consider the use of namespace as you can create your own namespaces as well.
For example, if you wanted to use the same cluster for both dev and production environment but at the
same time isolate the resources between them you can create a different namespace for each of them.
That way while working in the dev environment you don't accidentally modify a resources in production.
Each of these namespace can have its own set of policies that define who can do what.
You can also assign quota of resources to each of these named spaces that we each namespace is guaranteed
a certain amount and does not use more than it's allowed.
Limit going back to the default namespace that we have been working on just like how the members within
the House refer to each other by their first names.
The resources within a namespace can refer to each other simply by their names.
In this case the web app part can reach the db service simply using the hostname db service if required.
The Web app Pod can reach a service in another namespace as well.
For this you must append the name of the namespace to the name of the service.
For example for the web pod in the default namespace to connect to the database in the dev environment
or namespace.
Use the servicename.namespace.svc.cluste.local format that would be db service.
Dev.SVC.cluster.local you're able to do this because when the service is created a DNS entry
is added automatically in this format looking closely at the DNS name of the service.
The last part Cluster.local is the default domain name of the kubernetes cluster SVC is the subdomain
for service followed by the namespace and then the name of the service itself.
Let us now look at some of the operational aspects of namespace.
Let's start with a Kube control commands.
For example, this command is used to list all the pods but it only lists the pods in the default namespace
to list pods in another namespace.
Use the namespace option in the command along with the name of the namespace.
In this case Kube system here I have a pod definition file when you create a pod using this file.
The pod is created in the default namespace to create a pod in another namespace.
Use the namespace option if you want to make sure that this board gets created in the dev environment
all the time.
Even if you don't specify the namespace in the command line you can move the namespace definition into
the pod definition file like this under the metadata section.
This is a good way to ensure your resources are always created in the same namespace.
So how do you create a new namespace like any other object.
Use an namespace definition file the API version is V1 kind is namespace and under metadata specify
the name.
In this case.
Dev run the Kube control create command to create the namespace.
Another way to create a namespace is by simply running the command Kube control.
Create namespace followed by the name of the namespace now say we're working in three namespaces.
As we discussed before by default we are in the default namespace which is why we can see the resources
inside the default namespace using the kube control get pods command and to view those in the dev namespace.
We have to use the namespace option but what if we want to switch to the dev namespace permanently so
that we don't have to specify the namespace option anymore.
Well, in that case, use the Kube control config command to set the namespace in the current context.
Dev you can then simply run the Kube control get pods command without the namespace option to list pods
in the dev environment but you will need to specify the option for other environments such as default
or prod similarly you can switch to the prod namespace the same way.
Finally to view pods in all namespace use the all namespace option in the command.
This will list all the pods in all of the namespace is taking a closer look at the command.
This command first identifies the current context and then sets the namespace to the desired one for
that current context.
Well contexts are used to manage multiple clusters in multiple environments from the same management
system.
It is a totally separate topic to discuss and requires its own lecture so we will discuss context in
another lecture to limit resources in a namespace.
Create a resource quota to create one.
Start with a definition file for resource quota specify the namespace for which you want to create the
quota and then under spec provide your limits such as 10 pods 10 CPU units 10 GB byte of memory
etc..
Well that's it for this lecture.
Head over to the coding exercise and section and practice working with namespaces and I will see you
in the next lecture.