如何在 kubernetes 中切换命名空间 [英] How to switch namespace in kubernetes
本文介绍了如何在 kubernetes 中切换命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
比如说,我有两个命名空间 k8s-app1 和 k8s-app2
Say, I have two namespaces k8s-app1 and k8s-app2
我可以使用以下命令列出来自特定命名空间的所有 Pod
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?
推荐答案
我能够使用以下步骤切换命名空间
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
这是我测试的方式
# 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
这篇关于如何在 kubernetes 中切换命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文