Kubernetes
Namespace
DevOps
Cloud Computing
Container Orchestration

How I create new namespace in Kubernetes

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Creating a namespace in Kubernetes is simple, but using namespaces well takes a bit more thought than running one command. A namespace is a logical boundary for names, access rules, quotas, and default context. If you treat it as just a folder name, you miss most of the operational value.

The Fastest Way To Create A Namespace

For quick manual work, kubectl create namespace is enough.

bash
kubectl create namespace payments

You can confirm it exists with:

bash
kubectl get namespaces

This approach is useful for experiments, debugging, and one-off administrative tasks.

Use YAML For Repeatable Environments

If the namespace belongs to an application or environment that should be recreated reliably, declare it in YAML and apply it like any other Kubernetes resource.

yaml
1apiVersion: v1
2kind: Namespace
3metadata:
4  name: payments
5  labels:
6    team: backend
7    environment: staging

Apply it with:

bash
kubectl apply -f namespace.yaml

This is usually the better option for production clusters because the namespace becomes part of version-controlled infrastructure.

Put Resources In The Namespace

Creating the namespace does not move anything into it automatically. Each namespaced resource must either be created with -n payments or declared with metadata.namespace.

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4  name: api
5  namespace: payments
6spec:
7  replicas: 2
8  selector:
9    matchLabels:
10      app: api
11  template:
12    metadata:
13      labels:
14        app: api
15    spec:
16      containers:
17        - name: api
18          image: nginx:1.27
19          ports:
20            - containerPort: 80

If you omit the namespace, Kubernetes will usually place the resource in default.

Set A Default Namespace For Your Current Context

If you work in the same namespace often, change the current context so you do not have to type -n payments every time.

bash
kubectl config set-context --current --namespace=payments

Now kubectl get pods will target payments by default. This is convenient, but it also makes mistakes easier if you forget the context changed, so check with:

bash
kubectl config view --minify | grep namespace

Namespaces Become More Useful With Policies

A namespace is most valuable when paired with controls such as quotas, RBAC, and network policies.

Here is a simple resource quota:

yaml
1apiVersion: v1
2kind: ResourceQuota
3metadata:
4  name: payments-quota
5  namespace: payments
6spec:
7  hard:
8    pods: "10"
9    requests.cpu: "4"
10    requests.memory: 8Gi

This prevents a team or workload from consuming unlimited cluster resources inside that namespace.

When Not To Create More Namespaces

Namespaces are useful boundaries, but too many of them create operational noise.

Good reasons to create a namespace:

  • a separate environment such as staging or production,
  • a distinct team boundary,
  • separate security or quota rules,
  • clear ownership of workloads.

Weak reasons:

  • every small microservice gets its own namespace by default,
  • you are trying to simulate folders,
  • the cluster is tiny and the workloads are all owned by one team.

The right number of namespaces depends on operational boundaries, not aesthetics.

Common Pitfalls

  • Creating the namespace but forgetting to create workloads inside it.
  • Relying on the default namespace for everything and losing isolation.
  • Changing the current context namespace and then applying resources to the wrong place.
  • Treating namespaces as security by themselves without RBAC or network policies.
  • Creating too many namespaces for no operational reason.

Summary

  • Use kubectl create namespace for quick manual creation.
  • Use YAML manifests for repeatable, version-controlled environments.
  • Remember that workloads must explicitly target the namespace.
  • Set the current context namespace carefully if you want shorter commands.
  • Namespaces are most effective when combined with quotas, RBAC, and clear ownership boundaries.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.