Kubernetes
Namespace
Tutorial
DevOps
Container Management

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.

Practice system design

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:

  1. default: The default namespace for objects with no other namespace.
  2. kube-system: Namespace for objects created by the Kubernetes system.
  3. kube-public: Readable by all users, traditionally used for public resources.
  4. 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:

bash
kubectl get pods -n <namespace-name>

For example:

bash
kubectl get pods -n development

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:

  1. Create a duplicate context if needed.
bash
kubectl config get-contexts
kubectl config set-context <new-context-name> --namespace=<namespace-name> --cluster=<cluster-name> --user=<user-name>
  1. Modify the current context directly:
bash
kubectl config set-context --current --namespace=<namespace-name>

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:

bash
export K8S_NAMESPACE=<namespace-name>
kubectl get pods -n $K8S_NAMESPACE

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

ActionCommandDescription
Temporary Switchkubectl get pods -n <namespace-name>Use for one-off operations in different namespaces
Persist Contextkubectl config set-context --current --namespace=<namespace-name>Change the default namespace persistent across sessions
Environment Variableexport K8S_NAMESPACE=<namespace-name>Dynamic namespace usage in scripts without change in configuration
Context Duplicationkubectl 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.