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.
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.
You can confirm it exists with:
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.
Apply it with:
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.
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.
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:
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:
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
defaultnamespace 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 namespacefor 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
- How if I interact with different kubernetes clusters in different terminals sessions with out having to switch contexts all the the time?
- How is Docker Swarm different than Kubernetes?
- How is Python scaling with Gunicorn and Kubernetes?
- How is rancher different from Kubernetes
- How is concurrency in Spring AMQP Listener Container implemented?
- How is Docker different from a virtual machine?
- How is Amazon DynamoDB throughput calculated and limited?
- How is Amazon DynamoDB throughput calculated and limited?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.