Open In App

How to Change Namespace in Kubernetes ?

Last Updated : 26 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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”.

kubectl-namespaces

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

namespace-details

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.

kube

People Also Read

Kuberneets cheat sheet

Read

kubernetes Deployment

Read

How to Host Tomcat in Kubernetes

Read

Change Namespace in Kubernetes – FAQs

How to communicate one namespace to another namespace in Kubernetes?

To communicate between namespaces in Kubernetes, you can utilize services, cluster DNS, or ingress controllers. By creating services targeting pods in other namespaces or configuring DNS resolution, applications can interact across namespace boundaries. Additionally, ingress controllers facilitate routing external traffic to services in different namespaces, while service mesh solutions offer advanced traffic management capabilities for inter-namespace communication.

Can we have two namespace with same name?

No, in Kubernetes, each namespace must have a unique name within a cluster. Attempting to create a namespace with a name that already exists will result in an error. Namespaces are used to logically segregate resources within a cluster, and having duplicate names would lead to ambiguity and conflicts. It’s important to choose distinct and descriptive names for namespaces to maintain organization and clarity in your Kubernetes environment.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads