How to Change Namespace in Kubernetes ?
Last Updated :
26 Apr, 2024
Kubernetes namespaces offer an indispensable instrument for resource organization and isolation in the ever-changing realm of container orchestration. Like distinct districts in a busy metropolis, each namespace in your Kubernetes cluster serves as a dedicated place where you may organize related storage, services, and applications into logical units.
What Is Kubernetes Namespace?
Kubernetes Namespace is a mechanism that enables you to organize resources. It is like a virtual cluster inside the cluster. A namespace isolates the resources from the resources of other namespaces. For example, You need to have different names for deployments/services in a namespace but you can have the same name for deployment in two different namespaces.
Kubernetes Namespace - Read
Switching Namespaces Using the kubectl Command
Kubernetes namespace provides the isolation for the different types of resources within the cluster you can use the following command to switch the namespace in the Kubernetes cluster,
kubectl config set-context --current --namespace=<desired-namespace>
- kubectl: Kubectl is a command-line interface (CLI) tool for interacting with Kubernetes clusters.
- config: This is a subcommand of kubectl used for managing kubeconfig files. Kubernetes uses a kubeconfig file to store information about clusters, users, and contexts.
- set-context: This is another subcommand of the kubectl config used to modify the current context in the kubeconfig file.
- --current: This flag specifies that you want to modify the current context in the kubeconfig file. The current context is the set of cluster, user, and namespace information that kubectl uses by default for executing commands.
- --namespace=<desired-namespace>: This flag sets the namespace to the specified <desired-namespace>. Replace <desired-namespace> with the name of the namespace you want to switch to.
Steps To Switch Namespace
Step 1: List all the namespaces in Kubernetes.
kubectl get namespaces
Step 2: Select the namespace that you want to switch to and want to set it as a default and use the command shown in below. Here I am using "kube-public".

Step 3: Know get the current namespace you are in using the given command below.
kubectl config view | grep namespace:
Using the kubectl Command With the –namespace Flag
The namespace for which you wish to perform operations can be specified by using the kubectl command with the --namespace flag. The basic syntax is as follows:
kubectl [command] --namespace=<namespace-name>
kubectl
- kubectl is the Kubernetes command-line tool used for interacting with Kubernetes clusters.
[command]
- The precise kubectl operation you want to carry out is represented by [command]. Any legitimate kubectl command, including get, describe, create, delete, and so forth, might be this. Use get, for instance, to acquire details about Kubernetes resources; describe, to learn more about a particular resource in detail; create, to build a new resource; and so on.
--namespace=<namespace-name>
- --namespace=The flag <namespace-name> designates the namespace in which the action defined by [command] is to be executed. The namespace you wish to target should be substituted for <namespace-name>.
- Within a cluster, Kubernetes namespaces can be used to arrange and segregate resources. You can restrict the kubectl command's scope to only work with resources that are part of the specified namespace.
In the place of command use the command like "get pods". In the place of "<namespace-name>" in this give the namespace name which you want to perform the command.
kubectl get pods --namespace=kube-system

Setting Namespace in Kubernetes Configuration Files
In some organization they frequently work on some namespace for example "dev-ns","prod-ns" and etc in that cases you can set the particular namespace as a default to the cluster for that follow the steps below.
Step 1: list all the hidden folder in the kubernetes cluster
ls -a
use the cd command and change the directory to the ".kube" directory there you can find and file with name config.
Step 2: Edit the config file by using the following test in the namespace field
contexts:
- context:
cluster: kubernetes #specify your cluster name
user: kubernetes-admin #specifiy your user name
namespace: <specifiy you namespace>
name: my-context
Replace the above with required details.

People Also Read
|
---|
Kuberneets cheat sheet
| Read
|
---|
kubernetes Deployment
| Read
|
---|
How to Host Tomcat in Kubernetes
| Read
|
---|
Similar Reads
How To Delete Namespace In Kubernetes?
Kubernetes container orchestration tool that helps in managing the containerized application in pods and provides additional features such as security, storage, and network. Kubernetes comes with a service known as a namespace that helps in working and managing of resources. In this article we are g
3 min read
How to Restart Container in Kubernetes?
Kubernetes is a platform for container orchestration created to simplify application deployment, scaling, and administration. Suppose you have a big package of LEGO parts and you want to use them to make something very spectacular. But as your LEGO creation becomes larger, it becomes harder to organ
8 min read
How to Careate Static Pod in Kubernetes ?
Static pods in Kubernetes make it simple to run pods directly on individual cluster nodes without the Kubernetes API server being involved. Not at all like ordinary pods oversaw by the Kubernetes control plane, static pods are characterized and overseen straight by the Kubelet, the essential node ag
7 min read
How to Add Roles to Nodes in Kubernetes?
Kubernetes (or k8) is an open-source container orchestration platform that helps in managing various containers. Its architecture consists of nodes that represent worker machines and the containers run inside those machines. The machines, or nodes, may require some roles attached to them as well in
2 min read
How to Resart Pod In kubernetes?
Kubernetes is an open-source container orchestration tool that helps in scaling, deploying, and managing containerized applications. In Kubernetes, the pods are the smallest deployable unit. In this guide, I will first discuss what Kubernetes is. Then I will discuss what is pods in Kubernetes. After
5 min read
How To Stop Pod In Kubernetes ?
In the Kubernetes cluster stopping a pod is typically deleting the pod. Pods are meant to be ephemeral, meaning they can be created, destroyed, and replaced dynamically as needed. When the pod is deleted in Kubernetes the resources associated with the pod will be released. What Is Kubernetes Pod? In
5 min read
How to Manage Kubernetes Secrets ?
Most applications deployed through Kubernetes require access to databases, services, and other resources located externally. The easiest way to manage the login information necessary to access those resources is by using Kubernetes secrets. Secrets help organize and distribute sensitive information
12 min read
Kubernetes NameSpace: Complete Guide.
In Kubernetes, Namespaces are used to organize resources. You can have multiple Namespaces in a Cluster And these Namespaces are kind of virtual Clusters of their own. The official definition of Namespace says "In Kubernetes, namespaces provide a mechanism for isolating groups of resources within a
12 min read
Kubernetes - Namespaces
Kubernetes Namespace is a mechanism that enables you to organize resources. It is like a virtual cluster inside the cluster. A namespace isolates the resources from the resources of other namespaces. For example, You need to have different names for deployments/services in a namespace but you can ha
9 min read
How To Manage Kubernetes ConfigMaps?
ConfigMaps in Kubernetes are used to store non-confidential data in key-value pairs. They allow you to decouple configuration from image content, making your applications more portable. Managing ConfigMaps involves creating, updating, and using them within your Kubernetes environment. Pods can consu
5 min read