Namespaces:Namespaces is a way to isolate kubernetes elements and organize them in different "compartments".
We can have multiple namespaces in a cluster.
After installation, kubernetes creates some default namespaces.
We can display these namespaces, along other exciting namespaces using the below command:
Displays namespaces including the default ones (kube-system, kube-public, default,...).
We could also use the below command instead:
The default namespace:
When we create resources (pods, services, configmaps, ...) they get created in the default namespace, if we don't create them in a new namespace.
Creating a new namespace:
We can create a new namespace using the below command:
To check if it was created, we use:
We can also use a Yaml configuration file (namespace.yaml) to create a namespace:
If we create all the kubernetes elements in the default namespace, we will soon have a jumble of unrelated resource in one namespace.
To have things organized, we put related resources in the same namespace. We could have a namespace for our database elements and another namespace for our web server elements (example above) and we could also have yet another one for logging operations.
One more useful example would be setting namespace for each department or team, so they don't accidentally delete or overwrite each other data for example.
In the diagram, we put all the resources needed by the first team in the Team_1 namespace and the resources needed by the second team in the Team_2 namespace .
Resource sharing and namespaces:We could create a resource once in a namespace (a service for example) and share it between other namespace.
Shareable and non-shareable resources:
We can put resources in a namespace and share it among another namespaces, an example of these resources could be services.
There are resources that we can't share and need to be created in each namespace like Configmaps and Secrets.
There are some resources we can't create in a namespace, they can only be created in the cluster, volumes are an example of a resource that can't be created inside a namespace.
To list resources that are not bound to a namespace, we use:
Creating a resource in a new namespace "namespace_name", using a YAML file (namespace.yml):
Example: - Creating a pod in a namespace "ns_1" -
We start by creating the Yaml file for the pod "pod_1" as below:
We then create the pod in the ns_1 namespace using :
We could also mention the namespace in the pod.yml file, but we can't assign the pod to another namespace using the --namespace parameter as in the above command.