Kubernetes Namespaces Practical - Hands-on Namespace Management Guide & Commands

Practical step-by-step tutorial for Kubernetes namespace management with essential kubectl commands. Learn to create, switch between, and manage namespaces with copy-paste examples and real-world scenarios.

Get Namespaces

kubectl get namespaces
kubectl get ns

Displays all the namespaces in your Kubernetes cluster.

Usage:

Use these commands to list all available namespaces. 'ns' is a shorthand for 'namespaces'.

Get Pods from Default Namespace

kubectl get pods

Lists all pods in the default namespace.

Note:

If no namespace is specified, kubectl uses the default namespace.

Get Pods from Specific Namespace

kubectl get pods --namespace=kube-system
kubectl get pods -n kube-system

Lists all pods in the specified namespace.

Usage:

Use these commands to get pods from a specific namespace. '-n' is a shorthand for '--namespace'.

Change Current Namespace

kubectl config set-context --current --namespace=kube-system
kubectl get pods

Changes the default namespace for the current context.

Example:

After running this command, all subsequent kubectl commands will use the kube-system namespace by default.

Change Back to Default Namespace

kubectl config set-context --current --namespace=default
kubectl get pods

Reverts the default namespace back to 'default'.

Usage:

Use this command to switch back to the default namespace after working in another namespace.

Create Namespace

kubectl create ns [name]

Creates a new namespace with the specified name.

Example:

kubectl create ns hello

This command creates a namespace named "hello".

Verify Namespace Creation

kubectl get ns

Lists all namespaces to verify the new namespace was created successfully.

Usage:

After creating a namespace, use this command to confirm it appears in the list of all namespaces.

Delete Namespace

kubectl delete ns [name]

Deletes the specified namespace and all resources within it.

Warning:

This command will delete the namespace and all resources contained within it. Use with caution.

About Kubernetes Namespaces

What is a Namespace?

A namespace in Kubernetes is a way to divide cluster resources between multiple users, teams, or applications. It provides a scope for names and helps organize resources in a cluster.

Namespaces are a powerful feature for implementing multi-tenancy in Kubernetes clusters, allowing different teams to share the same cluster without interfering with each other.

Common Use Cases

  • Separating development, staging, and production environments
  • Isolating different teams or projects within the same cluster
  • Implementing resource quotas for different applications
  • Managing access control with RBAC (Role-Based Access Control)

Default Namespaces

Kubernetes comes with several built-in namespaces:

  • default: The default namespace for objects with no other namespace
  • kube-system: The namespace for objects created by the Kubernetes system
  • kube-public: A namespace that is readable by all users
  • kube-node-lease: A namespace for node lease objects