How to switch namespace in kubernetes
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Switching namespaces in Kubernetes can enhance efficiency when managing resources across multiple environments. In Kubernetes, namespaces are used to isolate and organize resources within a single cluster. This isolation allows for resource management at a granular level, enabling teams to develop, test, and deploy their applications independently.
This guide will provide you with insights on how to effectively switch namespaces using command-line tools, along with examples and advanced configurations.
Understanding Kubernetes Namespaces
Namespaces provide a mechanism for dividing cluster resources between multiple users. They are intended for use in environments with many users spread across multiple teams, or projects. Use cases include:
- Segregating team resources.
- Enabling resource quotas.
- Separate environments (development, testing, production).
By default, Kubernetes has the following namespaces:
default: The default namespace for objects with no other namespace.kube-system: Namespace for objects created by the Kubernetes system.kube-public: Readable by all users, traditionally used for public resources.kube-node-lease: Contains Lease objects associated with each node to monitor node availability.
Switching Namespaces
The kubectl command-line tool is integral to switching and managing namespaces. Below are various methods to switch namespaces:
Temporary Namespace Switch
To operate in a different namespace for a single command, you can use the -n or --namespace flag:
For example:
This command lists all pods within the development namespace without changing your current context.
Persistent Context Change
To change namespaces persistently within a context, update your kubectl configuration:
- Create a duplicate context if needed.
- Modify the current context directly:
This method ensures all subsequent commands use the specified namespace until changed again.
Namespace Environment Variable
For those who automate scripts, setting an environment variable can prove handy:
This pattern allows keeping your namespace dynamic based on environment configurations.
Considerations and Best Practices
- Use Meaningful Names: Namespace names should reflect the purpose, e.g.,
dev,test,prod, etc. - RBAC for Namespaces: Configure Role-Based Access Control to define permissions on a per-namespace basis.
- Resource Quotas: Utilize resource quotas to limit consumption within a namespace, ensuring fair use of cluster resources.
- Limit Custom Namespaces: Avoid overly subdividing into namespaces unless necessary; consider other Kubernetes constructs like labels and annotations.
Namespace Switching Summary
| Action | Command | Description |
| Temporary Switch | kubectl get pods -n <namespace-name> | Use for one-off operations in different namespaces |
| Persist Context | kubectl config set-context --current --namespace=<namespace-name> | Change the default namespace persistent across sessions |
| Environment Variable | export K8S_NAMESPACE=<namespace-name> | Dynamic namespace usage in scripts without change in configuration |
| Context Duplication | kubectl config set-context <new-context-name> --namespace=<namespace-name> --cluster=<cluster-name> --user=<user-name> | Create new contexts for separation between configurations |
Namespaces offer powerful mechanisms for organizing and managing applications efficiently within Kubernetes. By understanding how to switch and manage namespaces effectively, you can streamline operations and enhance collaborative efforts across different teams and environments in your Kubernetes deployment.
Related reading
- How to troubleshoot metrics-server on kubeadm?
- how to undo a kubectl port-forward
- how to uninstall minikube from ubuntu, i get an ''Unable to load cached images'' error
- How to update a set of pods running in kubernetes?
- How to tag docker image with docker-compose
- How to unset ENV in dockerfile?
- How to synchronize distributed system data across cassandra clusters
- How to test the connection to RabbitMQ Server?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.