Kubectl Create Namespace: How to Create a Namespace in Kubernetes

Create a Namespace in Kubernetes Using kubectl

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 in Kubernetes using kubectl and YAML.

In this guide, we’ll walk you through the steps to create a namespace in Kubernetes.

Method 1: Using kubectl create namespace

The simplest way to create a namespace is by using the kubectl create namespace command.

 # kubectl create namespace namespace-name

For example, to create a namespace called dev, you would run:

 # kubectl create namespace dev

Method 2: Using a YAML Manifest

Creating a namespace using a YAML manifest gives you more control and allows you to version-control your infrastructure configuration.

1. Create a YAML file named namespace.yaml with the following content:

apiVersion: v1
kind: Namespace
metadata:
  name: namespace-name
  labels:
    name: namespace-name

Replace namespace-name with the desired name of your namespace.

For example, to create a namespace called dev, your namespace.yaml would look like this:

apiVersion: v1
kind: Namespace
metadata:
  name: dev
  labels:
    name: dev

2. Apply the YAML file using kubectl:

 # kubectl apply -f namespace.yaml

Verifying the Namespace Creation

You can verify that the namespace has been created by listing all namespaces:

 # kubectl get namespaces

This will sho your newly created namespace in the output.

NAME              STATUS   AGE
default           Active   10d
kube-node-lease   Active   10d
kube-public       Active   10d
kube-system       Active   10d
dev               Active   1m

Using a Namespace

Once a namespace is created, you can use it to isolate resources. Here are some common operations:

Creating Resources in a Namespace

To create a resource within a specific namespace, use the -n flag followed by the namespace name:

 # kubectl create deployment my-deployment --image=nginx -n namespace-name

For example, to create a deployment in the dev namespace:

 # kubectl create deployment my-deployment --image=nginx -n dev

Setting a Default Namespace for kubectl

You can configure kubectl to use a specific namespace by default. This can be done by updating the context in your kubeconfig file.

1. Set the default namespace for the current context:

 # kubectl config set-context --current --namespace=namespace-name

For example, to set the default namespace to dev, run:

 # kubectl config set-context --current --namespace=dev

2. Verify the current context’s namespace:

 # kubectl config view --minify | grep namespace:

You should see the following output:

namespace: dev

Deleting a Namespace

To delete a namespace and all the resources within it, use the kubectl delete namespace command:

 # kubectl delete namespace namespace-name

For example, to delete the dev namespace, run:

 # kubectl delete namespace dev

Conclusion

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.

FAQs

1. What is the default namespace in Kubernetes?

The default namespace is default. If no namespace is specified, Kubernetes assumes resources belong to the default namespace.

2. Can I create multiple namespaces in a single Kubernetes cluster?

Yes, you can create multiple namespaces, and each namespace provides isolated resources for different workloads.

3. What happens to resources when I delete a namespace?

When you delete a namespace, all the resources inside it (pods, services, etc.) are deleted along with it.

4. Is there a limit to the number of namespaces I can create?

There is no hard limit in Kubernetes itself, but it depends on your cluster's configuration and resources.

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva