How to switch namespace in kubernetes
Asked Answered
U

12

286

Say, I have two namespaces k8s-app1 and k8s-app2

I can list all pods from specific namespace using the below command

kubectl get pods -n <namespace>

We need to append namespace to all commands to list objects from the respective namespaces. Is there a way to set specific namespace and list objects without including the namespace explicitly?

Unprovided answered 27/3, 2019 at 9:24 Comment(1)
Hi kubectl config set-context --help might be you are looking forDifferent
U
45

I was able to switch namespace using the below steps

kubectl config set-context $(kubectl config current-context) --namespace=<namespace>
kubectl config view | grep namespace
kubectl get pods

This is how i have tested

# Create namespaces k8s-app1, k8s-app2 and k8s-app3
master $ kubectl create ns k8s-app1
namespace/k8s-app1 created
master $ kubectl create ns k8s-app2
namespace/k8s-app2 created
master $ kubectl create ns k8s-app3
namespace/k8s-app3 created

# Create Service Account app1-sa in k8s-app1
# Service Account app2-sa in k8s-app2
# Service Account app3-sa in k8s-app3
master $ kubectl create sa app1-sa -n k8s-app1
serviceaccount/app1-sa created
master $ kubectl create sa app2-sa -n k8s-app2
serviceaccount/app2-sa created
master $ kubectl create sa app3-sa -n k8s-app3
serviceaccount/app3-sa created

# Switch namespace
master $ kubectl config set-context $(kubectl config current-context) --namespace=k8s-app1
Context "kubernetes-admin@kubernetes" modified.
master $ kubectl config view | grep namespace
    namespace: k8s-app1
master $ kubectl get sa
NAME      SECRETS   AGE
app1-sa   1         1m
default   1         6m
master $
master $ kubectl config set-context $(kubectl config current-context) --namespace=k8s-app2
Context "kubernetes-admin@kubernetes" modified.
master $ kubectl get sa
NAME      SECRETS   AGE
app2-sa   1         2m
default   1         7m
master $
master $ kubectl config set-context $(kubectl config current-context) --namespace=k8s-app3
Context "kubernetes-admin@kubernetes" modified.
master $ kubectl get sa
NAME      SECRETS   AGE
app3-sa   1         2m
default   1         7m
Unprovided answered 27/3, 2019 at 9:37 Comment(1)
You may have to use kubectl config view --minify | grep namespace: to get current namespace.Menorrhagia
C
472

I like my answers short, to the point and with references to official documentation:

Answer:

kubectl config set-context --current --namespace=my-namespace

From:

https://kubernetes.io/docs/reference/kubectl/cheatsheet/

# permanently save the namespace for all subsequent kubectl commands in that context.
kubectl config set-context --current --namespace=ggckad-s2
Chronister answered 28/5, 2019 at 12:15 Comment(2)
It's a good practice to validate that the current namespace has changed by executing the following command kubectl config view | grep namespace:Interstitial
Alternatively, it is possible to just get the namespace without any other tools using kubectl config view -o jsonpath={.contexts[].context.namespace}Poteet
K
124

There are a few options:

  • Switch namespace only using the kubectl commands::
kubectl config set-context --current --namespace=<namespace>
  • Or, Create a new context with namespace defined:
kubectl config set-context gce-dev --user=cluster-admin --namespace=dev
kubectl config use-context gce-dev
  • Or, Use addons, like kubectx & kubens, the below command will switch the context to kube-system:
$ kubens kube-system 
  • Or, Another easy alternative that I like without installing third party tools, is using bash alias(linux).
$ alias kubens='kubectl config set-context --current --namespace '
$ alias kubectx='kubectl config use-context '

// Usage
$ kubens kube-system    //Switch to a different namespace
$ kubectx docker        //Switch to separate context
Krahling answered 27/3, 2019 at 9:45 Comment(2)
how do you unset the currrent namespace?Forelimb
kubectl config set-context --current --namespace=""Krahling
U
45

I was able to switch namespace using the below steps

kubectl config set-context $(kubectl config current-context) --namespace=<namespace>
kubectl config view | grep namespace
kubectl get pods

This is how i have tested

# Create namespaces k8s-app1, k8s-app2 and k8s-app3
master $ kubectl create ns k8s-app1
namespace/k8s-app1 created
master $ kubectl create ns k8s-app2
namespace/k8s-app2 created
master $ kubectl create ns k8s-app3
namespace/k8s-app3 created

# Create Service Account app1-sa in k8s-app1
# Service Account app2-sa in k8s-app2
# Service Account app3-sa in k8s-app3
master $ kubectl create sa app1-sa -n k8s-app1
serviceaccount/app1-sa created
master $ kubectl create sa app2-sa -n k8s-app2
serviceaccount/app2-sa created
master $ kubectl create sa app3-sa -n k8s-app3
serviceaccount/app3-sa created

# Switch namespace
master $ kubectl config set-context $(kubectl config current-context) --namespace=k8s-app1
Context "kubernetes-admin@kubernetes" modified.
master $ kubectl config view | grep namespace
    namespace: k8s-app1
master $ kubectl get sa
NAME      SECRETS   AGE
app1-sa   1         1m
default   1         6m
master $
master $ kubectl config set-context $(kubectl config current-context) --namespace=k8s-app2
Context "kubernetes-admin@kubernetes" modified.
master $ kubectl get sa
NAME      SECRETS   AGE
app2-sa   1         2m
default   1         7m
master $
master $ kubectl config set-context $(kubectl config current-context) --namespace=k8s-app3
Context "kubernetes-admin@kubernetes" modified.
master $ kubectl get sa
NAME      SECRETS   AGE
app3-sa   1         2m
default   1         7m
Unprovided answered 27/3, 2019 at 9:37 Comment(1)
You may have to use kubectl config view --minify | grep namespace: to get current namespace.Menorrhagia
D
32

I didn't like kubectx and kubens because they are adding one more letter for bash-complection to kubectl command.

So I just wrote tiny kubectl-use plugin:

# kubectl use prod
Switched to context "prod".

# kubectl use default
Switched to namespace "default".

# kubectl use stage kube-system
Switched to context "stage".
Switched to namespace "kube-system".

If you interesting to it, check https://github.com/kvaps/kubectl-use

Debbi answered 26/10, 2019 at 19:34 Comment(1)
This has to be my favorite answer by far! While there is a quite reasonable but unfortunately long command to do this without extending or aliasing kubectl commands, this solution is not only short and sweet to use, but it also - quite nicely - demonstrates how to properly extend kubectl with a simple and understandable subcommand. I tip my hat to you, good Sir! ;-)Tedda
A
29

You could use the following package called kubectx which make it very easy to switch between clusters using kubectx

enter image description here

and switching between namespaces using kubens

enter image description here

Acroter answered 4/5, 2019 at 23:39 Comment(1)
this extension is the best optionMesognathous
J
7

I created a function in .zshrc

ksns() { kubectl config set-context --current --namespace="$1" }

Then I call ksns default

Janie answered 31/5, 2020 at 12:21 Comment(1)
love this commandScouring
R
1

Check out https://krew.sigs.k8s.io/ They have plenty of plugins for different use cases.

For installation https://krew.sigs.k8s.io/docs/user-guide/setup/install/

kubectl krew install kubens

then after running the following command will set the current context for you

kubens <namespace> 
Recapitulate answered 16/7, 2021 at 19:19 Comment(0)
C
1

The kubectl config set-context --current --namespace <namespace> command will set the current namespace.

However, I like to use a little bash function (kn) which updates the namespace and also displays all the available namespaces on the Kubernetes cluster:

function kn () {
  kubectl get ns ; echo 
  if [[ "$#" -eq 1 ]]; then
    kubectl config set-context --current --namespace $1 ; echo
  fi
  echo "Current namespace [ $(kubectl config view --minify | grep namespace | cut -d " " -f6) ]"
}

This can live inside your ~/.bash_profile or ~/.zprofile (Z Shell on Macs) file.

Setting the namespace as follows:

$ kn postgres

NAME              STATUS   AGE
default           Active   27d
kube-node-lease   Active   27d
kube-public       Active   27d
kube-system       Active   27d
postgres          Active   2d2h

Context "docker-desktop" modified.

Current namespace [ postgres ]

The Context "docker-desktop" modified. indicates that the current namespace was updated.

The function also works with no parameters and will display all the namespaces + current namespace.

$ kn

NAME              STATUS   AGE
default           Active   27d
kube-node-lease   Active   27d
kube-public       Active   27d
kube-system       Active   27d
postgres          Active   2d2h

Current namespace [ postgres ]
Countermove answered 1/2, 2024 at 17:3 Comment(0)
D
0
  • kubectx - for swtiching contexts/clusters. Can be installed with "brew install kubectx"

  • kubens- for switching between namespaces in a cluster. Can also be installed with "brew install kubens"

Demonology answered 31/3, 2022 at 13:45 Comment(0)
P
0

kubie can switch context and namespaces. if you have fzf installed, the experience will be greatly improved.

  • kubie ctx show the list of available contexts (if fzf is installed, display a selectable menu of contexts)
  • kubie ctx <context> switch the current shell to the given context (spawns a shell if not a kubie shell)
  • kubie ctx - switch back to the previous context
  • kubie ctx <context> -r spawn a recursive shell in the given context
  • kubie ctx <context> -n <namespace> spawn a shell in the given context and namespace
  • kubie ns show the list of available namespaces (if fzf is installed, display a selectable menu of namespaces)
  • kubie ns <namespace> switch the current shell to the given namespace
  • kubie ns - switch back to the previous namespace
  • kubie ns <namespace> -r spawn a recursive shell in the given namespace
Paracelsus answered 27/4, 2022 at 2:57 Comment(0)
S
0

A kubectl replacement maintained by Red Hat for their OCP / OKD will get this done with less typing (as many other things).

So after downloading oc Client Tools from Github (link), change namespace (yes, in a standard k8s cluster, not just in OKD) by typing:

oc project <my_ns>

And to validate:

oc project

Note that project and namespace are not fully interchangeable, so in a k8s cluster you cannot list all projects by oc get projects (this works only in OKD/OCP), you need this command to list all namespaces:

oc get ns

Segmental answered 3/3, 2023 at 11:5 Comment(0)
L
-2

A Solution

npm install -g k8ss

k8ss switch --namespace=your_namespace
kubectl get pods

TLDR; Explanation as requested

There is a npm package called k8ss which stands for K8S Switching between clusters and namespaces.

The full usage is

k8ss switch --cluster=your_new_cluster --namespace=your_new_namespace

As in your case, you only need to switch namespace, so you can use the command without any configuration (as you already put a config file in the ~/.kube/config).

Advanced Usage

If you need to switch between different clusters then you need to put multiple config files in your home directory. In this case you can go to the package README to learn more.

Lice answered 23/6, 2019 at 9:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.