Understanding Kubernetes Namespaces.
What is a Kubernetes namespace?
Let’s explain a Kubernetes namespace using an example of your computer’s hard disk. We normally organize our files into different folders. While we could put all the files in one folder, we often don’t. Why is this? Majorly we separate our files into folders for organization and convenience.
Likewise, a Kubernetes namespace allows us to partition our cluster into separate subdivisions. In a real ecosystem, you would have different teams or projects sharing the same cluster. If everyone were to deploy their application in the same cluster, it would end up being chaotic. You would, therefore, create a namespace for the different teams, projects, or project stages. For instance, you would have a namespace for development, another for production, and another one also for testing. Objects in the different namespaces are invisible to each other which means there won't be any conflict.
Let’s have a hand’s on a real Kubernetes cluster and see how this works. You can use an online Kubernetes Playground to try this out if you don’t have a Kubernetes cluster already set up. https://www.katacoda.com/courses/kubernetes/playground.
Kubernetes has three initial namespaces for a fresh cluster.
- Default — This namespace is used for your objects when you don’t specify a namespace.
- Kube-system — The namespace for objects created by the Kubernetes system.
- Kube-public — This namespace is mainly used for cluster usage. It is visible and readable to every user in the cluster.
kubectl get namespace
Let’s view all the pods that I had been previously created in the default namespace when I didn’t specify the namespace.
How to Create your own Kubernetes namespace.
Like all other Kubernetes resources, you can create your namespace by specifying it in the YAML file or by running the kubectl command.
- Using the YAML file.
- In your cluster, use a Linux text editor (vim in this case)to create a YAML file and insert contents to it.
- Insert contents in your YAML file. Save, exit vim.
- Saving content in vim will not create a namespace. You have to create the namespace by using the kubectl create command.
Our namespace has been created. Let’s now view all the namespaces in our cluster.
We can now see our namespace is now an additional namespace to the initial Kubernetes namespaces. A point to note is that the name of the namespace created is not the name of the YAML file but the name you specified in the YAML file under metadata.
2. A quick win though would be to use the kubectl create command directly to create your namespace.
How to use your created namespaces.
You can specify the namespace you want to use when creating a resource such as a deployment from the kubectl command directly.
kubect run deploymentname --image=specifyimage --replicas=numberOfReplicas --port=80 --namespace=namespace name
In a real ecosystem, when creating your Deployment, you will specify your namespace in the metadata section of your Deployment file. Find the below link If you need to understand more about how Deployments works.
Save changes, exit vim and apply the changes to create a deployment.
Great! Up to this point, we have a namespace called “mynamespace “and a deployment called my-app. We have specified that we want to use “mynamespace” as the namespace for the my-app deployment.
Let’s view all our namespaces now
Let’s now view if our deployment has been created in the “mynamespace” namespace
kubectl get deployment -n namespacename
The deployment my-app is in the “mynamespace” namespace and it will not be visible to view it from another namespace.
How to delete the namespace
kubectl delete namespace yourNameSpaceName
Points to Note
- You can use Kubernetes Network Policies to control traffic in and out of a particular namespace.
- Deleting a namespace would delete all the resources within it.