What are Kubernetes Namespaces?
Namespaces in Kubernetes are a way to divide cluster resources between multiple users, teams, or projects. They provide a scope for names and help organize resources in a cluster.
Key Benefits:
- Resource isolation between different teams or projects
- Ability to assign resource quotas per namespace
- Access control through RBAC (Role-Based Access Control)
- Logical separation without needing multiple clusters
Default Namespaces:
- default - For resources with no other namespace
- kube-system - For Kubernetes system components
- kube-public - For publicly accessible resources
- kube-node-lease - For node lease objects
Essential Namespace Commands
| Command | Description | Action |
|---|---|---|
| kubectl get namespace | List all namespaces | |
| kubectl get ns | Shortcut for listing namespaces | |
| kubectl config set-context --current --namespace=[name] | Set the current context to use a namespace | |
| kubectl create ns [name] | Create a new namespace | |
| kubectl delete ns [name] | Delete a namespace and all its resources | |
| kubectl get pods --all-namespaces | List all pods across all namespaces |
Usage Tips
Namespace Context
Switching the default namespace using kubectl config set-context saves you from typing -n namespace in every command.
Namespace Management
Use kubectl get ns frequently to monitor available namespaces in your cluster.
Common Namespace Use Cases
Environment Separation
Separate development, staging, and production environments within the same cluster.
Team Isolation
Provide different teams with their own namespaces to work independently.
Security Boundaries
Implement RBAC policies to control access to specific namespaces.