WEBVTT

00:00.180 --> 00:03.370
Hello and welcome to this lecture in this lecture.

00:03.390 --> 00:09.870
We will discuss about namespaces in kubernetes, let us begin with an analogy.

00:09.920 --> 00:15.460
There are two boys named Mark to differentiate them from each other.

00:15.530 --> 00:17.580
We call them by their last names.

00:17.840 --> 00:21.760
Smith and Williams they come from different houses.

00:21.860 --> 00:28.320
Of course the Smiths and the Williams There are other members in the house.

00:28.320 --> 00:33.810
The individuals within the house address each other simply by their first names.

00:33.870 --> 00:38.740
For example the father addresses Mark simply as Mark.

00:38.910 --> 00:47.180
However if the father wishes to address the mark in the other house he would use the full name someone

00:47.180 --> 00:54.190
outside of these houses would also use the full name to refer to the boys or anyone within these houses.

00:55.910 --> 01:03.150
Each of these houses have their own set of rules that defines who does what each of these houses have

01:03.150 --> 01:08.280
their own set of resources that they can consume.

01:08.290 --> 01:16.200
Now let's get back to kubernetes these houses correspond to namespaces in kubernetes.

01:16.560 --> 01:23.000
So far in this course we've created objects such as pods deployments and services in our cluster.

01:23.430 --> 01:28.890
Whatever we have been doing we have been doing within a namespace.

01:28.950 --> 01:37.620
We were inside a house all this while this namespace is known as the default namespace and it is created

01:37.740 --> 01:45.150
automatically kubernetes, when the cluster is first set up kubernetes, creates a set of pods

01:45.150 --> 01:50.650
and services for its internal purpose such as those required by the networking solution.

01:50.820 --> 01:57.960
The DNS service etc. to isolate these from the user and to prevent you from accidentally deleting or

01:57.960 --> 02:04.890
modifying these services kubernetes creates them under another namespace created at cluster startup

02:05.310 --> 02:13.930
named Kube system a third namespace created by Coburn that is automatically is called kube public.

02:13.960 --> 02:18.450
This is where resources that should be made available to all users are created.

02:20.130 --> 02:25.980
If your environment is small or your learning and playing around with a small cluster you shouldn't

02:25.980 --> 02:31.990
really have to worry about namespaces you could continue to work in the default namespace.

02:32.070 --> 02:38.880
However as and when you grow and use a kubernetes cluster for enterprise or production purposes you

02:38.880 --> 02:46.430
may want to consider the use of namespace as you can create your own namespaces as well.

02:46.440 --> 02:52.890
For example, if you wanted to use the same cluster for both dev and production environment but at the

02:52.890 --> 02:59.860
same time isolate the resources between them you can create a different namespace for each of them.

02:59.970 --> 03:06.390
That way while working in the dev environment you don't accidentally modify a resources in production.

03:06.390 --> 03:12.300
Each of these namespace can have its own set of policies that define who can do what.

03:12.330 --> 03:19.560
You can also assign quota of resources to each of these named spaces that we each namespace is guaranteed

03:19.650 --> 03:22.920
a certain amount and does not use more than it's allowed.

03:22.920 --> 03:29.810
Limit going back to the default namespace that we have been working on just like how the members within

03:29.810 --> 03:32.840
the House refer to each other by their first names.

03:32.990 --> 03:38.190
The resources within a namespace can refer to each other simply by their names.

03:38.210 --> 03:47.630
In this case the web app part can reach the db service simply using the hostname db service if required.

03:47.710 --> 03:52.400
The Web app Pod can reach a service in another namespace as well.

03:52.420 --> 03:56.950
For this you must append the name of the namespace to the name of the service.

03:57.040 --> 04:02.950
For example for the web pod in the default namespace to connect to the database in the dev environment

04:03.300 --> 04:04.570
or namespace.

04:04.570 --> 04:13.320
Use the servicename.namespace.svc.cluste.local format that would be db service.

04:13.500 --> 04:22.260
Dev.SVC.cluster.local you're able to do this because when the service is created a DNS entry

04:22.290 --> 04:29.000
is added automatically in this format looking closely at the DNS name of the service.

04:29.000 --> 04:37.810
The last part Cluster.local is the default domain name of the kubernetes cluster SVC is the subdomain

04:37.870 --> 04:42.490
for service followed by the namespace and then the name of the service itself.

04:46.190 --> 04:50.080
Let us now look at some of the operational aspects of namespace.

04:50.140 --> 04:52.290
Let's start with a Kube control commands.

04:52.430 --> 05:00.020
For example, this command is used to list all the pods but it only lists the pods in the default namespace

05:00.860 --> 05:03.330
to list pods in another namespace.

05:03.350 --> 05:08.000
Use the namespace option in the command along with the name of the namespace.

05:08.000 --> 05:16.830
In this case Kube system here I have a pod definition file when you create a pod using this file.

05:16.910 --> 05:22.400
The pod is created in the default namespace to create a pod in another namespace.

05:22.400 --> 05:30.340
Use the namespace option if you want to make sure that this board gets created in the dev environment

05:30.400 --> 05:31.480
all the time.

05:31.570 --> 05:37.450
Even if you don't specify the namespace in the command line you can move the namespace definition into

05:37.450 --> 05:42.300
the pod definition file like this under the metadata section.

05:42.310 --> 05:48.530
This is a good way to ensure your resources are always created in the same namespace.

05:48.640 --> 05:52.910
So how do you create a new namespace like any other object.

05:53.020 --> 06:01.390
Use an namespace definition  file the API version is V1  kind is namespace and under metadata specify

06:01.390 --> 06:02.410
the name.

06:02.410 --> 06:03.100
In this case.

06:03.100 --> 06:07.900
Dev run the Kube control create command to create the namespace.

06:07.930 --> 06:12.790
Another way to create a namespace is by simply running the command Kube control.

06:12.790 --> 06:20.560
Create namespace followed by the name of the namespace now say we're working in three namespaces.

06:20.770 --> 06:27.070
As we discussed before by default we are in the default namespace which is why we can see the resources

06:27.130 --> 06:34.240
inside the default namespace using the kube control get pods command and to view those in the dev namespace.

06:34.240 --> 06:41.410
We have to use the namespace option but what if we want to switch to the dev namespace permanently so

06:41.410 --> 06:45.320
that we don't have to specify the namespace option anymore.

06:45.340 --> 06:52.210
Well, in that case, use the Kube control config command to set the namespace in the current context.

06:52.210 --> 06:59.810
Dev you can then simply run the Kube control get pods command without the namespace option to list pods

06:59.900 --> 07:06.590
in the dev environment but you will need to specify the option for other environments such as default

07:06.710 --> 07:12.010
or prod similarly you can switch to the prod namespace the same way.

07:13.310 --> 07:20.390
Finally to view pods in all namespace use the all namespace option in the command.

07:20.390 --> 07:26.320
This will list all the pods in all of the namespace is taking a closer look at the command.

07:26.420 --> 07:33.740
This command first identifies the current context and then sets the namespace to the desired one for

07:33.740 --> 07:35.840
that current context.

07:35.840 --> 07:41.660
Well contexts are used to manage multiple clusters in multiple environments from the same management

07:41.660 --> 07:42.610
system.

07:42.740 --> 07:49.100
It is a totally separate topic to discuss and requires its own lecture so we will discuss context in

07:49.100 --> 07:52.460
another lecture to limit resources in a namespace.

07:52.460 --> 07:56.490
Create a resource quota to create one.

07:56.510 --> 08:02.570
Start with a definition file for resource quota specify the namespace for which you want to create the

08:02.570 --> 08:10.940
quota and then under spec provide your limits such as 10 pods 10 CPU units 10 GB byte of memory

08:10.940 --> 08:12.260
etc..

08:12.410 --> 08:14.570
Well that's it for this lecture.

08:14.570 --> 08:20.660
Head over to the coding exercise and section and practice working with namespaces and I will see you

08:20.870 --> 08:21.770
in the next lecture.

