Wednesday, September 17, 2025

What is Kubernetes namespace?

🔹 What is a Kubernetes Namespace?

A namespace in Kubernetes is a way to divide cluster resources into isolated, virtual clusters.

Think of it as a way to group resources logically (like projects, teams, or environments) within the same physical cluster.

Default namespaces:

default → where resources go if you don’t specify a namespace.

kube-system → system resources like CoreDNS, kube-proxy.

kube-public → public resources accessible cluster-wide.

kube-node-lease → for node heartbeats.

Why namespaces?

1. Separation of concerns (team A vs team B apps).

2. Resource quotas and limits per namespace.

3. Easier RBAC (role-based access control).

4. Prevent naming collisions (two pods with same name but in different namespaces).

🔹 How it Works

1. Every resource (Pod, Service, Deployment, etc.) belongs to a namespace.

2. Names are unique per namespace, not across the whole cluster.

3. You can control resources in one namespace without affecting others.

4. Some cluster-scoped resources (like Nodes, PersistentVolumes, StorageClasses) are not namespaced.

🔹 Verify Namespace Features (Hands-On in Minikube)

Here’s a set of YAML files you can try.

1. Create a Namespace

namespace.yaml

apiVersion: v1

kind: Namespace

metadata:

  name: dev-team

Apply it:

kubectl apply -f namespace.yaml

kubectl get namespaces

2. Create a Pod in that Namespace

pod-dev.yaml

apiVersion: v1

kind: Pod

metadata:

  name: nginx-dev

  namespace: dev-team

labels:

  app: nginx

spec:

  containers:

  - name: nginx

    image: nginx:latest

    ports:

    - containerPort: 80

Apply it:

kubectl apply -f pod-dev.yaml

kubectl get pods -n dev-team

3. Same Pod Name in Different Namespace

Let’s create another pod with the same name nginx-dev but in default.

pod-default.yaml

apiVersion: v1

kind: Pod

metadata:

  name: nginx-dev

  namespace: default

spec:

  containers:

  - name: nginx

    image: nginx:latest

Apply:

kubectl apply -f pod-default.yaml

kubectl get pods -n default

👉 Now you’ll see two pods with the same name, one in dev-team, one in default. This shows name isolation.

4. Access Pods by Namespace

kubectl get pods -A          # list pods across all namespaces

kubectl describe pod nginx-dev -n dev-team

5. (Optional) Set Default Namespace for kubectl

Instead of -n dev-team every time:

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

Now all commands default to dev-team.

6. Clean Up

kubectl delete namespace dev-team

kubectl delete pod nginx-dev -n default

✅ With this flow, you’ll see how namespaces isolate resources, allow duplicate names, and provide flexibility.

No comments:

Post a Comment