How to switch namespace in kubernetes

P Ekambaram picture P Ekambaram · Mar 27, 2019 · Viewed 63.7k times · Source

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?

Answer

PussInBoots picture PussInBoots · May 28, 2019

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