---
title: "A Getting Started Guide to Kubernetes Namespaces"
description: "This guide will provide a comprehensive overview of Kubernetes namespaces, including their purpose, how to create and manage them!"
canonical: "https://adamtheautomator.com/kubernetes-namespaces/"
---

# A Getting Started Guide to Kubernetes Namespaces

> This guide will provide a comprehensive overview of Kubernetes namespaces, including their purpose, how to create and manage them!

Source: https://adamtheautomator.com/kubernetes-namespaces/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![A Getting Started Guide to Kubernetes Namespaces](https://adamtheautomator.com/wp-content/uploads/2023/04/kubernetes-namespaces.jpg)

# A Getting Started Guide to Kubernetes Namespaces

[![](https://secure.gravatar.com/avatar/b1faae2f957a0d43de36dfd25e8c08537718fb87c49cfd0a15b0e860a7f7b9a0?s=192&d=mm&r=g)Mercy Bassey](https://adamtheautomator.com/author/mercy-bassey/)24 April 20235 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Kubernetes](/tag/kubernetes/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Creating Kubernetes Namespaces via the kubectl](#creating-kubernetes-namespaces-via-the-kubectl)
*   [Creating Kubernetes Namespaces via Manifest Files](#creating-kubernetes-namespaces-via-manifest-files)
*   [Deploying Resources to Namespaces via the kubectl](#deploying-resources-to-namespaces-via-the-kubectl)
*   [Deploying Resources to Namespaces via Manifest Files](#deploying-resources-to-namespaces-via-manifest-files)
*   [Managing Namespaces with kubens](#managing-namespaces-with-kubens)
*   [Conclusion](#conclusion)

If you need help separating resources for different projects, environments, or applications, do not prolong the agony. Why not turn to namespaces? In particular, Kubernetes namespaces let you partition resources within a Kubernetes cluster.

In this tutorial, you will learn about creating and managing Kubernetes namespaces in your own Kubernetes environment and how to use them effectively.

Dive in and master containerizing applications at scale!

## Prerequisites

This tutorial comprises hands-on demonstrations. To follow along, be sure you have the following:

*   A Kubernetes cluster already running.

Related:[How to Get Started Building Kubernetes Clusters with kubeadm](https://adamtheautomator.com/kubeadm/)

*   [Krew installed](https://krew.sigs.k8s.io/docs/user-guide/setup/install/) (Kubernetes’ package manager).

## Creating Kubernetes Namespaces via the `kubectl`

Namespaces work great in isolating groups of resources in a cluster. Each group is logically separated from others but capable of communicating with each other.

There are two ways to create a Kubernetes namespace: the `kubectl` or a manifest file. But for a start, you will create a Kubernetes namespace via the `kubectl` command-line tool. `kubectl` lets you communicate with a Kubernetes cluster’s [control plane](https://kubernetes.io/docs/reference/glossary/?all=true#term-control-plane), like creating a namespace, via the Kubernetes API.

Open a terminal, and run the following `kubectl` command to `create` a `namespace` called `demo` (arbitrary).

```bash
kubectl create namespace demo
```

![Creating a namespace via kubectl](https://adamtheautomator.com/wp-content/uploads/2023/04/image-119.png)

Creating a namespace via kubectl

Now, run the below command to `get` all Kubernetes `namespaces` available and verify your newly-created namespace.

```bash
kubectl get namespaces
```

Below, you can verify that the newly-created namespace called **demo** exists, is **Active**, and is **19** seconds old.

![Verifying the newly-created namespace exists](https://adamtheautomator.com/wp-content/uploads/2023/04/image-120.png)

Verifying the newly-created namespace exists

## Creating Kubernetes Namespaces via Manifest Files

Besides the `kubectl` command-line tool, you can also create a Kubernetes namespace via a manifest file. A manifest file is a YAML file that describes each deployment component or resource and defines your cluster’s state once applied.

Why use a manifest file? Doing so can be useful when version-controlling your namespace creation or if you wish to deploy your namespace as part of a more extensive configuration.

To create a namespace via a manifest file:

1\. Create a YAML file with your preferred [editor](https://adamtheautomator.com/powershell-text-editor/), and populate the following code to the file. This tutorial uses the filename name**space.yaml**, but you can name the file differently.

Related:[How to Edit Files with a Real PowerShell Text Editor](https://adamtheautomator.com/powershell-text-editor/)

This manifest file creates a new Kubernetes namespace called `my-namespace` (arbitrary) using the Kubernetes API version `v1`.

```yaml
# Set the API version
apiVersion: v1
# Create a namespace
kind: Namespace
metadata:
  # The name of the namespace
  name: my-namespace
```

2\. Next, run the below [`kubectl apply`](https://jamesdefabia.github.io/docs/user-guide/kubectl/kubectl_apply/) command to create the namespace specified in your manifest file (`namespace.yaml`).

```bash
kubectl apply -f namespace.yaml
```

![Creating the namespace specified in the manifest file](https://adamtheautomator.com/wp-content/uploads/2023/04/image-121.png)

Creating the namespace specified in the manifest file

3\. Lastly, run the following command to `get` the list of all available `namespaces`.

```bash
kubectl get namespaces
```

The output below shows the newly-created namespace (**my-namespace)**.

![Verifying the newly-created namespace](https://adamtheautomator.com/wp-content/uploads/2023/04/image-122.png)

Verifying the newly-created namespace

## Deploying Resources to Namespaces via the `kubectl`

Regardless of your method of creating your namespace, you can now better organize and manage your resources and provide better isolation and security. But how do you know your namespace works?

You will deploy a Kubernetes resource to your namespace in two ways: via the `kubectl` command-line tool or by using a manifest file. But for a start, you will deploy a resource via the `kubectl` first.

To deploy a Kubernetes resource via the `kubectl`:

Execute the following [`create deploy`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#-em-deployment-em-) command to deploy a [busybox](https://hub.docker.com/_/busybox) image from the Docker Hub registry to your `demo` namespace. The deployment uses the default settings, such as creating a replica and running in a single container.

```bash
kubectl create deploy nginx-web-server --image nginx -n demo
```

![Creating an NGINX deployment to the demo namespace](https://adamtheautomator.com/wp-content/uploads/2023/04/image-123.png)

Creating an NGINX deployment to the demo namespace

Related:[Securely Manage Containers with Kubernetes RBAC](https://adamtheautomator.com/kubernetes-rbac/)

Now, run the following command to `get` all `deployments` in the `demo` namespace.

```javascript
kubectl get deployments -n demo
```

Like shown below, you will see the **nginx-web-server** deployment, which confirms the deployment is successful.

![Verifying the deployment is successful](https://adamtheautomator.com/wp-content/uploads/2023/04/image-124.png)

Verifying the deployment is successful

## Deploying Resources to Namespaces via Manifest Files

Manifest files do not only create namespaces but also deploy resources to your namespaces. You will specify configurations like creating namespaces, but deploying resources requires writing more.

To deploy a resource to your namespace via a manifest file:

1\. Create a YAML file (i.e., _busybox.yaml_) and populate the following configuration to the file.

The code below creates a `Pod` in your specified namespace (`my-namespace`), which runs a container based on the `busybox` image.

```yaml
apiVersion: v1
# Specify the resource to create (a Pod)
kind: Pod
metadata:
  # The name of the Pod.
  name: busybox-pod
  # The namespace where the resource should be created.
  namespace: my-namespace
spec:
  containers:
  # The container name
  - name: busybox
  # The image to use
    image: busybox
    # Make the container sleep for 3600 seconds (one hour) before exiting
    command:
      - sleep
      - "3600"
```

2\. Next, run the below command to create and deploy (`apply`) the resource specified in your manifest file (`busybox.yaml`).

```bash
kubectl apply -f busybox.yaml
```

![Deploying a resource to a namespace via a manifest file](https://adamtheautomator.com/wp-content/uploads/2023/04/image-125.png)

Deploying a resource to a namespace via a manifest file

3\. Now, run the following command to `get` all `pods` available in your namespace (`my-namespace`).

```bash
kubectl get pods -n my-namespace
```

The output below shows the newly-created Pod, which confirms your deployment via a manifest file is successful.

![Getting all pods available](https://adamtheautomator.com/wp-content/uploads/2023/04/image-126.png)

Getting all pods available

## Managing Namespaces with **`kubens`**

You now have one namespace with deployed resources, which is a good start in containerization. But suppose you have multiple namespaces; how do you switch from one namespace to another?

Traditionally, managing and switching between namespaces takes work. But the good news is that [`kubens`](https://github.com/ahmetb/kubectx#what-are-kubectx-and-kubens), a command-line tool, lets you quickly switch between namespaces.

> _The `kubens` command’s close neighbor is the **[`kubectx`](https://github.com/ahmetb/kubectx#what-are-kubectx-and-kubens)** command-line tool, which lets you switch between Kubernetes clusters._

To manage and switch between namespaces:

1\. Run the following command to `install` kubens (`ns`) as a `kubectl` plugin.

> _The command below installs `kubens` on a Linux and MacOS machine. See [the installation](https://github.com/ahmetb/kubectx) guide if you are using a different OS._

```bash
kubectl krew install ns
```

![Installing Kubens as a plugin](https://adamtheautomator.com/wp-content/uploads/2023/04/image-127.png)

Installing Kubens as a plugin

2\. Next, run the command below to see all active namespaces in your Kubernetes cluster.

```bash
kubectl ns
```

Pick one namespace from the list, but this tutorial’s choice is the **demo** namespace.

![Viewing all namespaces](https://adamtheautomator.com/wp-content/uploads/2023/04/image-128.png)

Viewing all namespaces

3\. Run the below `kubectl ns` command to set your target namespace (`demo`_**)**_ as your current namespace.

```bash
kubectl get pods
```

The output below shows you have successfully switched to the **demo** namespace.

![Switching to the demo namespace with Kubens](https://adamtheautomator.com/wp-content/uploads/2023/04/image-130.png)

Switching to the demo namespace with Kubens

4\. After switching, run the below command to `get` all `pods` available in the current namespace, which is the demo namespace.

```javascript
kubectl get pods
```

![Viewing all pods in the current namespace (demo)](https://adamtheautomator.com/wp-content/uploads/2023/04/image-131.png)

Viewing all pods in the current namespace (demo)

5\. Now, run the following command switch to another namespace (`my-namespace`).

```javascript
kubectl ns my-namespace
```

![Switching to another namespace (my-namespace)](https://adamtheautomator.com/wp-content/uploads/2023/04/image-132.png)

Switching to another namespace (my-namespace)

6\. Now list all the pods in this namespace using the following command:

```javascript
kubectl get pods
```

Do you see a different pod than the one in step four? Congratulations! The output below confirms your namespace switch is successful.

![Viewing all pods in the current namespace (busy-box)](https://adamtheautomator.com/wp-content/uploads/2023/04/image-133.png)

Viewing all pods in the current namespace (busy-box)

## Conclusion

In this tutorial, you have learned to isolate resources confidently via Kubernetes namespaces, regardless of how many projects you are working on simultaneously.

Confusion between resources, environments, and applications no longer exists when you have Kubernetes namespaces in your cluster.

But besides namespaces, why not try using [`kubectx`](https://github.com/ahmetb/kubectx) to manage your Kubernetes clusters? Enjoy the journey of mastering containerization!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fkubernetes-namespaces%2F&text=A%20Getting%20Started%20Guide%20to%20Kubernetes%20Namespaces)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fkubernetes-namespaces%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fkubernetes-namespaces%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/06/Effortless-Storage-Management-With-Kubernetes-PVC.jpg)

### [Kubernetes PVC for Data Reliability: A Comprehensive Guide](/kubernetes-pvc/)

Tired of Kubernetes storage headaches? Discover how Kubernetes PVC simplifies data persistence and ensures your applications stay up and running.

![](https://adamtheautomator.com/wp-content/uploads/2024/01/kustomize.jpg)

### [A Beginners Guide to Kubernetes Kustomize](/kustomize/)

Dive into Kubernetes Kustomize to master dynamic deployments — A beginner’s guide for personalized configurations and seamless adaptability!

![](https://adamtheautomator.com/wp-content/uploads/2022/06/How-to-Keep-Kubernetes-Secrets-Safe.jpg)

### [Kubernetes Secrets Management: Enhance Safety & Reliability](/kubernetes-secrets/)

Explore expert techniques for securing Kubernetes secrets in production with this guide. Learn to enhance safety and reliability in your Kubernetes deployments.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
