Kubernetes namespaces explained

0
1027
Kubernetes namespaces explained
Kubernetes namespaces explained

Hello guys! On the last articles we was using kubernetes namespaces on our YAML files and talking about them but we didn’t get a chance to explain what is simply a namespace, what is used for and how we can create/delete it, so here we are this is the subject of this article!

Note: This post assumes that you have a running kubernetes cluster and you have a basic understanding of what a “Pod” is and its purpose, kubeadm and kubectl.

What is a Kubernetes namespace

Let’s take the example of having a multiple users working on the same Kubernetes cluster, every user have his own deployments,  volumes and services etc there. the questions to ask here are:

  • What if you want to apply a resources limits for one of those users?
  • what if a user want to specify an already used name to one of his objects?
  • More than this, what if you want to keep things organized and separated inside your cluster? (something like projects groups maybe)

If you facing one of those questions, your solution here is to use namespaces cause they give you the ability to divide your physical cluster into multiple virtual ones with a specific resources quota and this gives you the ability to use the same object names between your namespaces.

One last thing to mention is that by default, Kubernetes have three initial namespaces which are:

  • default:

    As every object have a namespace, this is the default namespace used for all the objects that you create without defining a namespace on your metadata.

  • kube-system:

    This is where you can find the objects used and created by the Kubernetes system.

  • kube-public:

    Considering the word “public” on its name, This namespace is created automatically and is readable by all users (including those not authenticated). This namespace is mostly reserved for cluster usage, in case that some resources should be visible and readable publicly throughout the whole cluster. The public aspect of this namespace is only a convention, not a requirement.

I think that the theoretical part is clear now so let’s move to the practical part!

How to list your namespaces?

To do that you can use the following command:

kubectl get namespace

and you will get the list of the namespaces exist on your cluster

NAME          STATUS    AGE
default       Active    3d
kube-system   Active    3d
kube-public   Active    3d

How to create a namespace?

Let’s create a new YAML file with the name dev-namespace.yaml for example:

 apiVersion: v1 
 kind: Namespace
 metadata:
   name: dev
   labels:
     name: dev
Now to create a namespace using this YAML file, just run the following command:
kubectl apply -f dev-namespace.yaml

Let’s list again our namespaces:

NAME          STATUS    AGE
dev           Active    1m
default       Active    3d
kube-system   Active    3d
kube-public   Active    3d

As you can see our new namespace is created successfully.

To create any type of object inside the new namespace, all you need to do is to specify the namespace key on your YAML files on the metadata or by adding –namespace=YOUR_NAMESPACE_NAME to your kubectl apply commands.

How to limit resources usage inside a namespace?

First of all you should know that the resources you create inside a namespace are only limited and applied on that namespace.

Let’s try to limit the resources used by containers inside our new namespace for example, to do that we should simply create a LimitRange object:

containers-limits.yaml:

apiVersion: v1
kind: LimitRange
metadata:
  name: containers-limits
spec:
  limits:
  - default:
      memory: 512Mi
    defaultRequest:
      memory: 256Mi
    type: Container

Then run the following command to create this LimitRange:

kubectl apply -f containers-limits.yaml --namespace=dev

Now every containers created on the dev namespace and have no resources limits specifications will yield to the limits declared on this LimitRange.

How to delete a namespace?

To delete all pods inside a namespace, you can run the following command:

kubectl delete pods --all --namespace=YOUR_NAMESPACE_NAME

Finally, to delete a namespace just use this command:

 

kubectl delete namespace YOUR_NAMESPACE_NAME

 

As you can see, Kubernetes namespaces are very useful and helpful to keep things organized inside our cluster also to separate resources limits etc.. Maybe i will write an article soon about how namespaces really works 😀

If you want to learn more, you can take a look on the Official namespaces docs.

LEAVE A REPLY

Please enter your comment!
Please enter your name here