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
I was able to switch namespace using the below steps
kubectl config set-context $(kubectl config current-context) --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