Creating a Pod inside a Namespace
We are going to understand what is namespace in programming and what is namespace in Kubernetes world. Both are actually very much same. So today we'll talk about the namespace ,how to create it, check it and creating a Pod in a namespace.
Namespace
A namespace is set of signs or names that are used to identify and refer to objects of various kinds. A namespace ensures that all of the objects have unique name so that they can be easily identified. You can
correlate this with schema in SQL server database where you can have multiple tables with same name but in different schema.
Similarly you can have multiple pods with same name but in different namespace.
How to check all available namespaces?
You can run kubectl get namespaces to get all available namespaces on cluster.
kubectl get namespaces
NAME STATUS AGE
default Active 2d
kube-public Active 2d
kube-system Active 2d
You can also run kubectl get ns , where ns is short form of namespace.
How to create a namespace
To create a namespace you need to run kubectl create namespace namespace_name
kubectl create namespace test
namespace/test created
kubectl get namespaces
NAME STATUS AGE
default Active 82d
kube-public Active 82d
kube-system Active 82d
test Active 5s
You can see test namespace has been created.
Now you'll notice what are these namespaces other than test. I didn't create them. Let me explain.
- kube-system: Namespace for objects created by kubernetes system
- default: It's default namespace when you don't specify name then objects will be created in default namespace
- kube-public: This is created automatically and readable by all users. This namespace is mostly reserved for cluster usage.
Let' create a Pod in test namespace now using below commands.
kubectl run mypod --image=nginx -n test
kubectl run mypod --image=nginx -n test
pod/mypod created
Let's check the pod and make sure you look at test namespace.
kubectl get pods -n test
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 2m10s
I have created mypod in prod namespace as well. So we can have an application with same name but in different namespace.
kubectl run mypod --image=nginx -n prod
pod/mypod created
kubectl get pods -n prod
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 14s
Conclusion
Namespace is very useful when deploying your application on cluster. It's always a best practice to create a namespace and deploy your application. You can think of a scenario where one team creates a pod with name testPod and other team also tries to name a pod testPod. So in this case Pod creation will be failed due to duplicate name. So best practice says create your pod inside a namespace.
Note: If you think this helped you and you want to learn more stuff on devops, then I would recommend joining the Kodecloud devops course and go for the complete certification path by clicking this link