{"id":1636,"date":"2024-08-14T07:40:13","date_gmt":"2024-08-14T07:40:13","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1636"},"modified":"2026-04-29T14:09:49","modified_gmt":"2026-04-29T14:09:49","slug":"kubernetes-namespace","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1636","title":{"rendered":"Kubernetes: NameSpace"},"content":{"rendered":"<p>The namespaces in Kubernetes provides a way for separating groups of resources hosted within a single cluster.<\/p>\n<p>While the default namespace where all the pods and applications get hosted is in the &#8220;default&#8221; namespace, the below command helps to add a new namespace.<\/p>\n<p><strong>[root@kubmaster01 ~]#<\/strong> kubectl create ns sv-ns1<br \/>\nnamespace\/sv-ns1 created<\/p>\n<p><strong>[root@kubmaster01 ~]#<\/strong> kubectl get ns<\/p>\n<p><em>==== ===<\/em><br \/>\n<em>NAME STATUS AGE<\/em><br \/>\n<em>default Active 2d15h<\/em><br \/>\n<em>kube-node-lease Active 2d15h<\/em><br \/>\n<em>kube-public Active 2d15h<\/em><br \/>\n<em>kube-system Active 2d15h<\/em><br \/>\n<span style=\"color: #ff0000;\"><em>sv-ns1 Active 50s<\/em><\/span><br \/>\n<em>==== ===<\/em><\/p>\n<p>There are two ways to add a new Namespace:<\/p>\n<ul>\n<li>Imperative<\/li>\n<li>declarative<\/li>\n<\/ul>\n<p>Imperative and declarative Kubernetes commands are the two different methods to configure\u00a0a new namespace. The same applies to configuring several other features too that we will see later.<\/p>\n<p>The method we used to create a new namespace in the above example is\u00a0<em>Imperative<\/em>, where we set the namespace using a CLI. Below is a\u00a0<em>declarative<\/em> method of creating a new namespace, using a yaml manifest file.<\/p>\n<p><strong>[root@CFX-91-KUBEMASTER]#<\/strong> kubectl api-resources | grep namespace<br \/>\n===== ====<br \/>\nNAME\u00a0 \u00a0 \u00a0 SHORTNAMES\u00a0 \u00a0 \u00a0 APIVERSION\u00a0 \u00a0 \u00a0 NAMESPACED\u00a0 \u00a0 \u00a0KIND<br \/>\nnamespaces\u00a0 \u00a0 \u00a0ns\u00a0 \u00a0 \u00a0 v1\u00a0 \u00a0 \u00a0 false\u00a0 \u00a0 \u00a0 Namespace<br \/>\n===== ====<\/p>\n<p><strong>[root@kubmaster01 sample]#<\/strong> cat ns2.yml<\/p>\n<p><em>====== ===<\/em><br \/>\n<em>apiVersion: v1<\/em><br \/>\n<em>kind: Namespace<\/em><br \/>\n<em>metadata:<\/em><br \/>\n<em>\u00a0 name: sv-ns2<\/em><br \/>\n<em>===<\/em>==<\/p>\n<p><strong>[root@kubmaster01 sample]#<\/strong> kubectl create -f ns2.yml<br \/>\nnamespace\/sv-ns2 created<\/p>\n<p><strong>[root@kubmaster01 sample]#<\/strong> kubectl get ns<\/p>\n<p><em>====== ===<\/em><br \/>\n<strong><em>NAME STATUS AGE<\/em><\/strong><br \/>\n<em><span style=\"color: #993300;\">default<\/span> Active 2d16h<\/em><br \/>\n<em>kube-node-lease Active 2d16h<\/em><br \/>\n<em>kube-public Active 2d16h<\/em><br \/>\n<em>kube-system Active 2d16h<\/em><br \/>\n<span style=\"color: #ff0000;\"><em>sv-ns1<span style=\"color: #000000;\"> Active 46m<\/span><\/em><\/span><br \/>\n<span style=\"color: #ff0000;\"><em>sv-ns2<span style=\"color: #000000;\"> Active 27s<\/span><\/em><\/span><\/p>\n<p><em>===== ===<\/em><\/p>\n<p>The below command is used to explain the parameters used to create a namespace. It is very helpful when creating a namespace using declarative\u00a0method.<\/p>\n<p><strong>[root@kubmaster01 sample]#<\/strong> kubectl explain ns<\/p>\n<p><em>===== ==<\/em><br \/>\n<em>KIND: Namespace<\/em><br \/>\n<em>VERSION: v1<\/em><\/p>\n<p><em>DESCRIPTION:<\/em><br \/>\n<em> Namespace provides a scope for Names. Use of multiple namespaces is<\/em><br \/>\n<em> optional.<\/em><\/p>\n<p><em>FIELDS:<\/em><br \/>\n<em> apiVersion &lt;string&gt;<\/em><br \/>\n<em>APIVersion defines the versioned schema of this representation of an object.<\/em><br \/>\n<em> Servers should convert recognized schemas<\/em><br \/>\n<em>===== ==<\/em><\/p>\n<p>The below command helps to explain in detail the parameters used when configuring metadata section in namespace<\/p>\n<p><strong>[root@kubmaster01 sample]#<\/strong> kubectl explain ns.metadata<\/p>\n<p>The below command explains the detail. But\u00a0<strong><em><span style=\"color: #ff0000;\">&#8211;recursive<\/span><\/em><\/strong> prints the same but with more deep details. It also marks the fields that are <span style=\"color: #ff0000;\"><em>required<\/em> <\/span>and ones <em><span style=\"color: #ff0000;\">optional<\/span><\/em>.<\/p>\n<p><strong>[root@kubmaster01 ~]#<\/strong> kubectl explain ns.metadata &#8211;recursive<\/p>\n<p>==== ====<br \/>\nKIND: Namespace<br \/>\nVERSION: v1<\/p>\n<p>DESCRIPTION:<br \/>\nNamespace provides a scope for Names. Use of multiple namespaces is<br \/>\noptional.<\/p>\n<p>FIELDS:<br \/>\napiVersion &lt;string&gt;<br \/>\nkind &lt;string&gt;<br \/>\nmetadata &lt;ObjectMeta&gt;<br \/>\nannotations &lt;map[string]string&gt;<br \/>\ncreationTimestamp &lt;string&gt;<br \/>\ndeletionGracePeriodSeconds &lt;integer&gt;<br \/>\ndeletionTimestamp &lt;string&gt;<br \/>\nfinalizers &lt;[]string&gt;<br \/>\ngenerateName &lt;string&gt;<br \/>\ngeneration &lt;integer&gt;<br \/>\nlabels &lt;map[string]string&gt;<br \/>\nmanagedFields &lt;[]ManagedFieldsEntry&gt;<br \/>\napiVersion &lt;string&gt;<br \/>\nfieldsType &lt;string&gt;<br \/>\nfieldsV1 &lt;FieldsV1&gt;<br \/>\nmanager &lt;string&gt;<br \/>\noperation &lt;string&gt;<br \/>\nsubresource &lt;string&gt;<br \/>\ntime &lt;string&gt;<br \/>\nname &lt;string&gt;<br \/>\nnamespace &lt;string&gt;<br \/>\nownerReferences &lt;[]OwnerReference&gt;<br \/>\napiVersion &lt;string&gt; -required-<br \/>\nblockOwnerDeletion &lt;boolean&gt;<br \/>\ncontroller &lt;boolean&gt;<br \/>\nkind &lt;string&gt; -required-<br \/>\nname &lt;string&gt; -required-<br \/>\nuid &lt;string&gt; -required-<br \/>\nresourceVersion &lt;string&gt;<br \/>\nselfLink &lt;string&gt;<br \/>\nuid &lt;string&gt;<br \/>\nspec &lt;NamespaceSpec&gt;<br \/>\nfinalizers &lt;[]string&gt;<br \/>\nstatus &lt;NamespaceStatus&gt;<br \/>\nconditions &lt;[]NamespaceCondition&gt;<br \/>\nlastTransitionTime &lt;string&gt;<br \/>\nmessage &lt;string&gt;<br \/>\nreason &lt;string&gt;<br \/>\nstatus &lt;string&gt; -required-<br \/>\ntype &lt;string&gt; -required-<br \/>\nphase &lt;string&gt;<\/p>\n<p>[root@CFX-91-KUBEMASTER shiju-manifest-files]#<br \/>\n==== =====<\/p>\n<p><strong>[root@kubmaster01 ~]#<\/strong> kubectl explain ns.metadata &#8211;recursive<\/p>\n<p><strong>[root@kubmaster01 ~]#<\/strong> kubectl describe ns sv-ns1<\/p>\n<p><em>===== ===<\/em><br \/>\n<em>Name: sv-ns1<\/em><br \/>\n<em>Labels: kubernetes.io\/metadata.name=sv-ns1<\/em><br \/>\n<em>Annotations: &lt;none&gt;<\/em><br \/>\n<em>Status: Active<\/em><br \/>\n<em>No resource quota.<\/em><br \/>\n<em>No LimitRange resource.<\/em><br \/>\n<em>===== ===<\/em><\/p>\n<p><strong>[root@kubmaster01 ~]#<\/strong> kubectl describe ns sv-ns2<\/p>\n<p>==== ====<br \/>\nName: sv-ns2<br \/>\nLabels: kubernetes.io\/metadata.name=sv-ns2<br \/>\nAnnotations: &lt;none&gt;<br \/>\nStatus: Active<br \/>\nNo resource quota.<br \/>\nNo LimitRange resource.<br \/>\n===== ===<\/p>\n<p>The below command prints the configuration of the namespace in yaml format.<\/p>\n<p><strong>[root@kubmaster01 ~]#<\/strong> kubectl get -o yaml ns sv-ns1<\/p>\n<p><em>=== =========<\/em><br \/>\n<em>apiVersion: v1<\/em><br \/>\n<em>kind: Namespace<\/em><br \/>\n<em>metadata:<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0creationTimestamp: &#8220;2024-08-14T07:41:50Z&#8221;<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0labels:<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0\u00a0 \u00a0 \u00a0kubernetes.io\/metadata.name: sv-ns1<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0name: sv-ns1<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0resourceVersion: &#8220;57258&#8221;<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0uid: 70bac5da-254d-4ab3-9ce2-b54d55985bcd<\/em><br \/>\n<em>spec:<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0finalizers:<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0&#8211; kubernetes<\/em><br \/>\n<em>status:<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0phase: Active<\/em><br \/>\n<em>=== =========<\/em><\/p>\n<p style=\"text-align: center;\"><strong>======== <span style=\"color: #993300;\">Context, Namespace\u00a0and pods<\/span> ========<\/strong><\/p>\n<p style=\"text-align: left;\">In a Kubernetes cluster there are context<strong>s<\/strong>, and namespaces in each &#8220;context&#8221;. The namespace in one &#8220;context&#8221; is entirely different the namespace on another &#8220;context&#8221;, though the names of the namespace may be similar. Same way, a pod associated to a namespace within a &#8220;context&#8221;.<\/p>\n<p style=\"text-align: left;\">Let us change the namepace used in the currect &#8220;context&#8221; to sv-sn01 using the below command<\/p>\n<p style=\"text-align: left;\"><strong>[root@kubmaster01 sample]#<\/strong> kubectl config set-context &#8211;current &#8211;namespace sv-ns01<br \/>\nContext &#8220;kubernetes-admin@kubernetes&#8221; modified.<\/p>\n<p><strong>[root@kubmaster01 sample]#<\/strong> kubectl config get-contexts<\/p>\n<p style=\"text-align: left;\"><em>=== ===<\/em><br \/>\n<em>CURRENT\u00a0 \u00a0 \u00a0NAME\u00a0 \u00a0 \u00a0 \u00a0CLUSTER\u00a0 \u00a0 \u00a0 AUTHINFO\u00a0 \u00a0 \u00a0 NAMESPACE<\/em><br \/>\n<em>* kubernetes-admin@kubernetes\u00a0 \u00a0 \u00a0kubernetes\u00a0 \u00a0 \u00a0 kubernetes-admin\u00a0 \u00a0 sv-ns01<\/em><\/p>\n<p style=\"text-align: left;\"><em>==== ===<\/em><\/p>\n<p style=\"text-align: left;\">\u00a0The above command alters the config file used by the user. This config file was initially copied to the users home folder when the below command was run on the <em>master<\/em> node when deploying the cluster<\/p>\n<ul>\n<li style=\"text-align: left;\">kubeadm init<\/li>\n<\/ul>\n<p>The below command displays the config file in a yaml format.<\/p>\n<p><strong>[root@kubmaster01 ~]#<\/strong> kubectl config view<\/p>\n<p><em>===== ===<\/em><br \/>\n<em>apiVersion: v1<\/em><br \/>\n<em>clusters:<\/em><br \/>\n<em>&#8211; cluster:<\/em><br \/>\n<em> \u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0certificate-authority-data: DATA+OMITTED<\/em><br \/>\n<em> \u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0server: https:\/\/192.168.146.129:6443<\/em><br \/>\n<em> \u00a0 \u00a0\u00a0name: kubernetes<\/em><br \/>\n<em>contexts:<\/em><br \/>\n<em>&#8211; context:<\/em><br \/>\n<em> \u00a0 \u00a0\u00a0cluster: kubernetes<\/em><br \/>\n<em> \u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0namespace: <span style=\"color: #ff6600;\">sv-ns01<\/span><\/em><br \/>\n<em> \u00a0 \u00a0\u00a0user: kubernetes-admin<\/em><br \/>\n<em> \u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0name: kubernetes-admin@kubernetes<\/em><br \/>\n<em>current-context: kubernetes-admin@kubernetes<\/em><br \/>\n<em>kind: Config<\/em><br \/>\n<em>preferences: {}<\/em><br \/>\n<em>users:<\/em><br \/>\n<em>\u00a0 \u00a0\u00a0&#8211; name: kubernetes-admin<\/em><br \/>\n<em> \u00a0 \u00a0\u00a0user:<\/em><br \/>\n<em>\u00a0 \u00a0\u00a0\u00a0 \u00a0 client-certificate-data: DATA+OMITTED<\/em><br \/>\n<em> \u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0client-key-data: DATA+OMITTED<\/em><\/p>\n<p><em>===== ===<\/em><\/p>\n<p>The below command sets the default namespace back to &#8220;default&#8221;<\/p>\n<p><strong>[root@kubmaster01 ~]#<\/strong> kubectl config set-context &#8211;current &#8211;namespace default<br \/>\nContext &#8220;kubernetes-admin@kubernetes&#8221; modified.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>The namespaces in Kubernetes provides a way for separating groups of resources hosted within a single cluster. While the default namespace where all the pods <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1636\" title=\"Kubernetes: NameSpace\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":1784,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,21,34,3,30],"tags":[],"class_list":["post-1636","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cluster","category-devops","category-kubernetes","category-linux","category-rhel-9"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1636"}],"version-history":[{"count":26,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1636\/revisions"}],"predecessor-version":[{"id":1838,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1636\/revisions\/1838"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/1784"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1636"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}