arundhaj

all that is technology

Kubernetes Namespace Explained with Examples

 

A Namespace in Kubernetes is a way to divide a single cluster into multiple virtual clusters. It gives you a scope for resource names, so two teams or two environments (say dev and staging) can each have a Deployment called api in the same cluster without clashing.

Namespaces are useful for:

  • Organizing resources by team, project, or environment
  • Access control — RBAC Roles and RoleBindings are scoped to a namespace
  • Resource limits — ResourceQuota and LimitRange apply per namespace

Note that namespaces provide logical isolation, not network isolation. Pods in different namespaces can still talk to each other unless you add a NetworkPolicy.

Create a namespace

Using the imperative command:

$ kubectl create namespace dev
namespace/dev created

Or declaratively with a YAML file, namespace.yaml:

apiVersion: v1
kind: Namespace
metadata:
  name: staging
  labels:
    env: staging
$ kubectl apply -f namespace.yaml
namespace/staging created

Create resources in a namespace

Pass the -n (or --namespace) flag to work inside a specific namespace.

$ kubectl create deployment nginx --image=nginx -n dev
deployment.apps/nginx created

$ kubectl get pods -n dev
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7854ff8877-x2k9p   1/1     Running   0          12s

You can also set the namespace in the manifest itself.

apiVersion: v1
kind: Pod
metadata:
  name: busybox
  namespace: staging
spec:
  containers:
    - name: busybox
      image: busybox
      command: ["sleep", "3600"]

To list resources across all namespaces, use -A (short for --all-namespaces).

$ kubectl get pods -A

Switch the default namespace

Typing -n dev on every command gets tedious. Set the namespace for your current context instead.

$ kubectl config set-context --current --namespace=dev
Context "minikube" modified.

$ kubectl config view --minify | grep namespace:
    namespace: dev

Now kubectl get pods returns pods from dev by default.

Limit resources with a ResourceQuota

A ResourceQuota caps the total resources a namespace can consume. Create quota.yaml:

apiVersion: v1
kind: ResourceQuota
metadata:
  name: dev-quota
  namespace: dev
spec:
  hard:
    pods: "10"
    requests.cpu: "2"
    requests.memory: 4Gi
    limits.cpu: "4"
    limits.memory: 8Gi
$ kubectl apply -f quota.yaml
$ kubectl describe resourcequota dev-quota -n dev

Once CPU or memory quotas are in place, every pod in the namespace must declare requests and limits for them, otherwise it will be rejected. A LimitRange can supply default values so you don't have to add them to every pod.

Delete a namespace

Deleting a namespace deletes everything inside it, so double-check before running this.

$ kubectl delete namespace dev
namespace "dev" deleted

Conclusion

Namespaces are a simple way to keep a shared cluster organized. Use them to separate environments or teams, apply RBAC and quotas per namespace, and set your context's default namespace so you don't have to type -n every time.

Comments