Nextgen Blog
HomeLearnAIQuantumRoboticsSecuritySpaceAbout
Nextgen Blog

By Anubhav — full stack engineer, IIT Mandi CSE. Deep dives into AI, software engineering, and the technologies shaping the next generation of tech.

Navigate

  • Home
  • Learn
  • Blog
  • Archive
  • About

Topics

  • Artificial Intelligence
  • Quantum Computing
  • Robotics
  • Cybersecurity
  • Space Tech

Connect

Built with ❤️ & By - Anubhav

© 2026 Nextgen Blog by Anubhav. All rights reserved.

Privacy PolicyTerms of Service
Phase 02 · Core WorkloadsChapter 9 of 33
08.
Phase 02Read time ~11 minDifficulty intermediate

Kubernetes Namespaces

Warrior, now that you understand Pods → Deployments → Services, the next important concept is Namespaces.

In this chapter
  • 01What is a Namespace?
  • 02Why Do We Need Namespaces?
  • 03Important: Namespace Is NOT a Separate Cluster
  • 04Default Namespaces
  • 05default
  • 06kube-system
  • 07kube-public
  • 08kube-node-lease
  • 09Creating a Namespace
  • 10Creating Namespace Using YAML
  • 11Deploying Into a Namespace
  • 12-n Means Namespace
  • 13See Everything Across All Namespaces
  • 14Namespace-Scoped Resources
  • 15Same Name in Different Namespaces
  • 16Setting Your Default Namespace
  • 17Service + Namespace
  • 18Cross-Namespace Communication
  • 19Namespace Does NOT Automatically Provide Security
  • 20Namespaces + RBAC
  • 21ResourceQuota
  • 22LimitRange
  • 23Real-World Application
  • 24Namespace Architecture
  • 25Practical Exercise
  • 26Step 1 — Create namespace
  • 27Step 2 — Create Deployment
  • 28Step 3 — Check
  • 29Step 4 — Check Pods
  • 30Step 5 — Describe
  • 31Step 6 — See all namespaces
  • 32Step 7 — Change default namespace
  • 33Important Namespace Commands
  • 34Interview Questions
  • 35Q1. What is a Kubernetes Namespace?
  • 36Q2. Why use namespaces?
  • 37Q3. Is a namespace a separate Kubernetes cluster?
  • 38Q4. Can two namespaces contain resources with the same name?
  • 39Q5. How do you get Pods from another namespace?
  • 40Q6. How do you get Pods from every namespace?
  • 41Q7. Does Namespace provide complete security isolation?
  • 42The Big Picture So Far
  • 43🧠 One-line memory trick
  • 44Next: Chapter 9 — ConfigMaps & Secrets
In this chapter (44 sections)
01What is a Namespace?02Why Do We Need Namespaces?03Important: Namespace Is NOT a Separate Cluster04Default Namespaces05default06kube-system07kube-public08kube-node-lease09Creating a Namespace10Creating Namespace Using YAML11Deploying Into a Namespace12-n Means Namespace13See Everything Across All Namespaces14Namespace-Scoped Resources15Same Name in Different Namespaces16Setting Your Default Namespace17Service + Namespace18Cross-Namespace Communication19Namespace Does NOT Automatically Provide Security20Namespaces + RBAC21ResourceQuota22LimitRange23Real-World Application24Namespace Architecture25Practical Exercise26Step 1 — Create namespace27Step 2 — Create Deployment28Step 3 — Check29Step 4 — Check Pods30Step 5 — Describe31Step 6 — See all namespaces32Step 7 — Change default namespace33Important Namespace Commands34Interview Questions35Q1. What is a Kubernetes Namespace?36Q2. Why use namespaces?37Q3. Is a namespace a separate Kubernetes cluster?38Q4. Can two namespaces contain resources with the same name?39Q5. How do you get Pods from another namespace?40Q6. How do you get Pods from every namespace?41Q7. Does Namespace provide complete security isolation?42The Big Picture So Far43🧠 One-line memory trick44Next: Chapter 9 — ConfigMaps & Secrets

Namespaces become extremely useful when your Kubernetes cluster starts containing many applications, teams, or environments.


1. What is a Namespace?#

A Namespace is a logical partition inside a Kubernetes cluster.

Think of your Kubernetes cluster as a large apartment building:

                    Kubernetes Cluster
                           │
          ┌────────────────┼────────────────┐
          │                │                │
        dev              staging           prod
          │                │                │
      ┌───┴───┐        ┌───┴───┐        ┌───┴───┐
      │       │        │       │        │       │
    Pods    Services  Pods   Services  Pods   Services

Instead of putting everything into one giant pool, you can organize resources into namespaces.

For example:

Cluster
│
├── development
│   ├── frontend
│   ├── backend
│   └── redis
│
├── staging
│   ├── frontend
│   ├── backend
│   └── redis
│
└── production
    ├── frontend
    ├── backend
    └── redis

2. Why Do We Need Namespaces?#

Imagine your company has:

100 applications
20 teams
3 environments

Without namespaces:

cluster
├── frontend
├── backend
├── api
├── redis
├── postgres
├── frontend
├── backend
├── api
...

Things quickly become difficult to manage.

With namespaces:

dev
├── frontend
├── backend
└── redis
 
staging
├── frontend
├── backend
└── redis
 
production
├── frontend
├── backend
└── redis

Much easier.

Namespaces help with:

  • Organization
  • Resource management
  • Access control
  • Environment separation
  • Resource quotas
  • Team separation
  • Service discovery

3. Important: Namespace Is NOT a Separate Cluster#

This is very important.

Suppose:

Kubernetes Cluster
│
├── dev namespace
├── staging namespace
└── production namespace

They are still inside the same Kubernetes cluster.

It is NOT:

Cluster 1 → Dev
Cluster 2 → Staging
Cluster 3 → Production

Namespaces are logical boundaries.

For stronger isolation, organizations may use separate clusters, cloud accounts/projects/subscriptions, or other infrastructure boundaries.


4. Default Namespaces#

Run:

kubectl get namespaces

or:

kubectl get ns

You'll commonly see namespaces such as:

NAME              STATUS
default           Active
kube-system       Active
kube-public       Active
kube-node-lease   Active

Let's understand them.


default#

This is where resources go if you don't specify a namespace.

For example:

kubectl create deployment nginx --image=nginx

will normally create the Deployment in:

default

kube-system#

Contains Kubernetes/system components.

For example:

kubectl get pods -n kube-system

You may see things such as:

coredns
kube-proxy

depending on your Kubernetes distribution.

Don't randomly delete things from kube-system.


kube-public#

A namespace intended for resources that should be publicly readable across the cluster.

It's not commonly where application workloads are deployed.


kube-node-lease#

Used for node heartbeat/lease information.

You generally don't interact with this directly during normal application development.


5. Creating a Namespace#

You can create one imperatively:

kubectl create namespace development

Check:

kubectl get namespaces

You should see:

development   Active

6. Creating Namespace Using YAML#

Kubernetes is fundamentally declarative, so YAML is often preferred.

Create:

apiVersion: v1
kind: Namespace
metadata:
  name: development

Save as:

namespace.yaml

Then:

kubectl apply -f namespace.yaml

Check:

kubectl get ns

7. Deploying Into a Namespace#

Suppose we create:

development

Now create a Deployment:

apiVersion: apps/v1
kind: Deployment
 
metadata:
  name: nginx
  namespace: development
 
spec:
  replicas: 2
 
  selector:
    matchLabels:
      app: nginx
 
  template:
    metadata:
      labels:
        app: nginx
 
    spec:
      containers:
        - name: nginx
          image: nginx:1.27
          ports:
            - containerPort: 80

Notice:

namespace: development

This tells Kubernetes:

Create this Deployment inside the development namespace.

Apply:

kubectl apply -f deployment.yaml

Now:

kubectl get deployments -n development

Output:

NAME    READY   UP-TO-DATE   AVAILABLE
nginx   2/2     2            2

And:

kubectl get pods -n development

8. -n Means Namespace#

This command:

kubectl get pods

means:

Get Pods from the current/default namespace

Usually:

default

But:

kubectl get pods -n development

means:

Get Pods from development namespace

Similarly:

kubectl get services -n development
kubectl get deployments -n development
kubectl get all -n development

9. See Everything Across All Namespaces#

Use:

kubectl get pods -A

-A means:

--all-namespaces

For example:

NAMESPACE       NAME                      READY
default         nginx                     1/1
development     backend-xxx               1/1
development     frontend-xxx              1/1
kube-system     coredns-xxx               1/1
kube-system     kube-proxy-xxx            1/1

This is a very useful troubleshooting command.


10. Namespace-Scoped Resources#

Many Kubernetes resources belong to a namespace.

For example:

Pod
Deployment
ReplicaSet
Service
ConfigMap
Secret
Job

So you can have:

development
└── backend
 
production
└── backend

Both can have the same name:

backend

because their full identity is effectively:

development/backend
production/backend

11. Same Name in Different Namespaces#

This is allowed:

development
└── api
 
staging
└── api
 
production
└── api

You can run:

kubectl get deployment api -n development

and:

kubectl get deployment api -n production

These are different resources.

This is extremely useful for environments.


12. Setting Your Default Namespace#

Typing:

-n development

every time can become annoying.

You can change your current context's default namespace:

kubectl config set-context --current --namespace=development

Now:

kubectl get pods

will look in:

development

instead of:

default

Check your context:

kubectl config current-context

You can also inspect:

kubectl config get-contexts

13. Service + Namespace#

This is where Namespaces become especially interesting.

Suppose we have:

development
│
├── backend
│
└── backend-service

The Service DNS name can be:

backend-service.development.svc.cluster.local

Inside the same namespace, you can usually simply use:

http://backend-service

From another namespace, you can use:

http://backend-service.development

or the full DNS name:

http://backend-service.development.svc.cluster.local

14. Cross-Namespace Communication#

Suppose:

development
└── frontend
 
production
└── backend
    └── backend-service

The frontend could communicate with:

backend-service.production.svc.cluster.local

Conceptually:

development
┌──────────────┐
│   frontend   │
└──────┬───────┘
       │
       │ DNS
       ▼
production
┌──────────────────┐
│ backend-service  │
└────────┬─────────┘
         │
         ▼
      backend Pods

However, whether traffic is actually allowed can depend on NetworkPolicies.

We'll cover that later.


15. Namespace Does NOT Automatically Provide Security#

This is a common interview trap.

❌ Incorrect:

Namespace completely isolates applications from each other.

Better:

Namespace provides logical organization and a scope for many Kubernetes resources, but it is not by itself a strong security boundary.

Security can involve:

Namespace
   +
RBAC
   +
NetworkPolicy
   +
ResourceQuota
   +
LimitRange
   +
Cluster/infrastructure isolation

We'll learn RBAC and NetworkPolicy later.


16. Namespaces + RBAC#

Suppose you have:

development
production

You could configure RBAC so that:

Developer
   ↓
Can access development

but:

Developer
   X
Cannot modify production

Conceptually:

                 Kubernetes
                     │
          ┌──────────┴──────────┐
          │                     │
       development          production
          │                     │
      Developer             Admin
       access               access

This is one of the major reasons namespaces are useful.


17. ResourceQuota#

Namespaces can also have resource quotas.

Imagine:

development

shouldn't consume the entire cluster.

You could configure something like:

apiVersion: v1
kind: ResourceQuota
 
metadata:
  name: dev-quota
  namespace: development
 
spec:
  pods: "10"
  requests.cpu: "2"
  requests.memory: 4Gi

Conceptually:

development namespace
 
Maximum:
-------------------
Pods       = 10
CPU        = 2 cores
Memory     = 4 GB
-------------------

This prevents one namespace from consuming unlimited resources.

We'll study resource management in detail in Chapter 13.


18. LimitRange#

LimitRange can define default or allowed resource requests/limits for containers within a namespace.

For example:

Every container:
CPU request = default 100m
Memory request = default 128Mi

This helps enforce sensible resource usage.

Again, we'll cover this properly in the resource-management chapter.


19. Real-World Application#

Imagine your application:

Next.js
Node.js
Redis
PostgreSQL

You could organize:

Kubernetes Cluster
│
├── development
│   ├── nextjs
│   ├── node-api
│   └── redis
│
├── staging
│   ├── nextjs
│   ├── node-api
│   └── redis
│
└── production
    ├── nextjs
    ├── node-api
    └── redis

Then:

development/node-api
staging/node-api
production/node-api

can all have the same resource name.


20. Namespace Architecture#

A typical organization might have:

                         Kubernetes Cluster
                                │
          ┌─────────────────────┼─────────────────────┐
          │                     │                     │
         DEV                  STAGING                PROD
          │                     │                     │
     ┌────┼────┐           ┌────┼────┐          ┌────┼────┐
     │    │    │           │    │    │          │    │    │
   FE    API  Redis       FE   API  Redis      FE   API  Redis

But in larger production systems, you may instead see:

Dev Cluster
      │
      └── dev namespaces
 
Staging Cluster
      │
      └── staging namespaces
 
Production Cluster
      │
      └── production namespaces

The second approach gives stronger isolation, at the cost of more infrastructure and management complexity.


21. Practical Exercise#

Let's practice.

Step 1 — Create namespace#

kubectl create namespace dev

Step 2 — Create Deployment#

kubectl create deployment nginx \
  --image=nginx \
  -n dev

Step 3 — Check#

kubectl get deployments -n dev

Step 4 — Check Pods#

kubectl get pods -n dev

Step 5 — Describe#

kubectl describe deployment nginx -n dev

Step 6 — See all namespaces#

kubectl get pods -A

Step 7 — Change default namespace#

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

Now:

kubectl get pods

should show the Pod from dev.


22. Important Namespace Commands#

Command Purpose
kubectl get ns List namespaces
kubectl create ns dev Create namespace
kubectl get pods -n dev Pods in namespace
kubectl get all -n dev Resources in namespace
kubectl get pods -A Pods across all namespaces
kubectl delete ns dev Delete namespace
kubectl config set-context --current --namespace=dev Set default namespace
kubectl config current-context Show current context

⚠️ Be careful with:

kubectl delete namespace dev

Deleting a namespace deletes the namespaced resources inside it.


23. Interview Questions#

Q1. What is a Kubernetes Namespace?#

Answer:

A Namespace is a logical partition within a Kubernetes cluster used to organize and scope resources, support access control, resource quotas, and environment or team separation.


Q2. Why use namespaces?#

For:

  • Organization
  • Environment separation
  • Team separation
  • RBAC
  • Resource quotas
  • Service discovery

Q3. Is a namespace a separate Kubernetes cluster?#

No.

Multiple namespaces can exist inside the same cluster.


Q4. Can two namespaces contain resources with the same name?#

Yes.

For example:

dev/backend
prod/backend

are different resources.


Q5. How do you get Pods from another namespace?#

kubectl get pods -n production

Q6. How do you get Pods from every namespace?#

kubectl get pods -A

Q7. Does Namespace provide complete security isolation?#

No.

RBAC, NetworkPolicy, quotas, infrastructure isolation, and other controls may be required.


24. The Big Picture So Far#

You've now learned:

Kubernetes Cluster
│
├── Namespace
│   │
│   ├── Deployment
│   │      │
│   │      └── ReplicaSet
│   │             │
│   │             └── Pods
│   │                   │
│   │                   └── Containers
│   │
│   └── Service
│          │
│          └── Routes traffic → Pods

And environments can be organized as:

Cluster
│
├── dev
│   ├── frontend
│   ├── backend
│   └── redis
│
├── staging
│   ├── frontend
│   ├── backend
│   └── redis
│
└── production
    ├── frontend
    ├── backend
    └── redis

🧠 One-line memory trick#

Namespace = logical folder for Kubernetes resources.


Next: Chapter 9 — ConfigMaps & Secrets#

This is where Kubernetes starts becoming much more useful for real applications.

We'll learn how to handle things like:

DATABASE_URL
API_URL
NODE_ENV
REDIS_HOST
JWT_SECRET
API_KEY

without hardcoding them inside your application or Docker image.

Finished this chapter?

Mark it as complete to track your mastery.

Previous ChapterChapter 7 — Kubernetes ServicesNext ChapterChapter 9 — ConfigMaps & Secrets
PrevNext