{"id":5280,"date":"2024-09-17T08:22:03","date_gmt":"2024-09-17T08:22:03","guid":{"rendered":"https:\/\/linuxbuz.com\/?p=5280"},"modified":"2024-09-23T03:09:41","modified_gmt":"2024-09-23T03:09:41","slug":"kubectl-create-namespace","status":"publish","type":"post","link":"https:\/\/linuxbuz.com\/devops\/kubectl-create-namespace","title":{"rendered":"Kubectl Create Namespace: How to Create a Namespace in Kubernetes"},"content":{"rendered":"<p style=\"text-align: justify;\">A <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/overview\/working-with-objects\/namespaces\/\" target=\"_blank\" rel=\"noopener\">namespace<\/a> in Kubernetes helps you organize and manage resources within your cluster. It is useful for dividing cluster resources between multiple users or teams. You can create a namespace in Kubernetes using <strong>kubectl<\/strong> and <strong>YAML.<\/strong><\/p>\n<p style=\"text-align: justify;\"><em>In this guide, we&#8217;ll walk you through the steps to create a namespace in Kubernetes.<\/em><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/linuxbuz.com\/devops\/kubectl-create-namespace\/#Method_1_Using_kubectl_create_namespace\" >Method 1: Using kubectl create namespace<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxbuz.com\/devops\/kubectl-create-namespace\/#Method_2_Using_a_YAML_Manifest\" >Method 2: Using a YAML Manifest<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxbuz.com\/devops\/kubectl-create-namespace\/#Verifying_the_Namespace_Creation\" >Verifying the Namespace Creation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxbuz.com\/devops\/kubectl-create-namespace\/#Using_a_Namespace\" >Using a Namespace<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxbuz.com\/devops\/kubectl-create-namespace\/#Deleting_a_Namespace\" >Deleting a Namespace<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxbuz.com\/devops\/kubectl-create-namespace\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxbuz.com\/devops\/kubectl-create-namespace\/#FAQs\" >FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n<h2><span class=\"ez-toc-section\" id=\"Method_1_Using_kubectl_create_namespace\"><\/span>Method 1: Using kubectl create namespace<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The simplest way to create a namespace is by using the <strong>kubectl create namespace<\/strong> command.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>kubectl create namespace namespace-name<\/code><\/pre>\n<p>For example, to create a namespace called <strong>dev,<\/strong> you would run:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>kubectl create namespace dev<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Method_2_Using_a_YAML_Manifest\"><\/span>Method 2: Using a YAML Manifest<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Creating a namespace using a <strong>YAML<\/strong> manifest gives you more control and allows you to version-control your infrastructure configuration.<\/p>\n<p>1. Create a YAML file named <strong>namespace.yaml<\/strong> with the following content:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>apiVersion: v1\r\nkind: Namespace\r\nmetadata:\r\n  name: namespace-name\r\n  labels:\r\n    name: namespace-name<\/code><\/pre>\n<p>Replace <strong>namespace-name<\/strong> with the desired name of your namespace.<\/p>\n<p style=\"text-align: justify;\">For example, to create a namespace called <strong>dev,<\/strong> your <strong>namespace.yaml<\/strong> would look like this:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>apiVersion: v1\r\nkind: Namespace\r\nmetadata:\r\n  name: dev\r\n  labels:\r\n    name: dev<\/code><\/pre>\n<p>2. Apply the YAML file using <strong>kubectl:<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>kubectl apply -f namespace.yaml<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Verifying_the_Namespace_Creation\"><\/span>Verifying the Namespace Creation<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">You can verify that the namespace has been created by listing all namespaces:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>kubectl get namespaces<\/code><\/pre>\n<p>This will sho your newly created namespace in the output.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>NAME              STATUS   AGE\r\ndefault           Active   10d\r\nkube-node-lease   Active   10d\r\nkube-public       Active   10d\r\nkube-system       Active   10d\r\ndev               Active   1m<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Using_a_Namespace\"><\/span>Using a Namespace<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Once a namespace is created, you can use it to isolate resources. Here are some common operations:<\/p>\n<h3>Creating Resources in a Namespace<\/h3>\n<p>To create a resource within a specific namespace, use the <strong>-n<\/strong> flag followed by the namespace name:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>kubectl create deployment my-deployment --image=nginx -n namespace-name<\/code><\/pre>\n<p>For example, to create a <strong>deployment<\/strong> in the <strong>dev<\/strong> namespace:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>kubectl create deployment my-deployment --image=nginx -n dev<\/code><\/pre>\n<h3>Setting a Default Namespace for kubectl<\/h3>\n<p style=\"text-align: justify;\">You can configure kubectl to use a specific namespace by default. This can be done by updating the context in your <strong>kubeconfig<\/strong> file.<\/p>\n<p>1. Set the default namespace for the current context:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>kubectl config set-context --current --namespace=namespace-name<\/code><\/pre>\n<p>For example, to set the default namespace to <strong>dev,\u00a0<\/strong>run:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>kubectl config set-context --current --namespace=dev<\/code><\/pre>\n<p>2. Verify the current context&#8217;s namespace:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>kubectl config view --minify | grep namespace:<\/code><\/pre>\n<p>You should see the following output:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>namespace: dev<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Deleting_a_Namespace\"><\/span>Deleting a Namespace<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To delete a namespace and all the resources within it, use the <strong>kubectl delete namespace<\/strong> command:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>kubectl delete namespace namespace-name<\/code><\/pre>\n<p>For example, to delete the <strong>dev<\/strong> namespace, run:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>kubectl delete namespace dev<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Namespaces in Kubernetes provide a powerful way to organize and manage resources within a cluster. Creating namespaces ensures that resources are properly isolated and managed according to your organizational needs. This guide covered the basic steps to create, use, and delete namespaces in Kubernetes.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h3>1. What is the default namespace in Kubernetes?<\/h3>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tThe default namespace is default. If no namespace is specified, Kubernetes assumes resources belong to the default namespace.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h3>2. Can I create multiple namespaces in a single Kubernetes cluster?<\/h3>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tYes, you can create multiple namespaces, and each namespace provides isolated resources for different workloads.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h3>3. What happens to resources when I delete a namespace?<\/h3>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tWhen you delete a namespace, all the resources inside it (pods, services, etc.) are deleted along with it.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h3>4. Is there a limit to the number of namespaces I can create?<\/h3>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tThere is no hard limit in Kubernetes itself, but it depends on your cluster's configuration and resources.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t\n<script type=\"application\/ld+json\">\n    {\n\t\t\"@context\": \"https:\/\/schema.org\",\n\t\t\"@type\": \"FAQPage\",\n\t\t\"mainEntity\": [\n\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"1. What is the default namespace in Kubernetes?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"The default namespace is default. If no namespace is specified, Kubernetes assumes resources belong to the default namespace.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t,\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"2. Can I create multiple namespaces in a single Kubernetes cluster?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"Yes, you can create multiple namespaces, and each namespace provides isolated resources for different workloads.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t,\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"3. What happens to resources when I delete a namespace?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"When you delete a namespace, all the resources inside it (pods, services, etc.) are deleted along with it.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t,\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"4. Is there a limit to the number of namespaces I can create?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"There is no hard limit in Kubernetes itself, but it depends on your cluster's configuration and resources.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t\t    ]\n}\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>A namespace in Kubernetes helps you organize and manage resources within your cluster. It is useful for dividing cluster resources between multiple users or teams. You can create a namespace &hellip; <\/p>\n","protected":false},"author":1,"featured_media":5281,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"two_page_speed":[],"footnotes":""},"categories":[828],"tags":[830],"class_list":["post-5280","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","tag-kubernetes"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts\/5280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/comments?post=5280"}],"version-history":[{"count":0,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts\/5280\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/media\/5281"}],"wp:attachment":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/media?parent=5280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/categories?post=5280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/tags?post=5280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}