例如,我有两个名称空间k8s-app1和k8s-app2
我可以使用下面的命令列出来自特定名称空间的所有pod
kubectl get pods -n <namespace>
我们需要将namespace附加到所有命令中,以列出来自各自名称空间的对象。是否有一种方法可以设置特定的名称空间和列表对象而不显式地包括名称空间?
例如,我有两个名称空间k8s-app1和k8s-app2
我可以使用下面的命令列出来自特定名称空间的所有pod
kubectl get pods -n <namespace>
我们需要将namespace附加到所有命令中,以列出来自各自名称空间的对象。是否有一种方法可以设置特定的名称空间和列表对象而不显式地包括名称空间?
当前回答
Kubie可以切换上下文和名称空间。 如果您安装了FZF,体验将大大改善。
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
其他回答
我希望我的回答简明扼要,并参考官方文件:
答:
kubectl config set-context --current --namespace=my-namespace
来自:
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
我不喜欢kubectx和kubens,因为他们在kubectl命令中增加了一个bash-补全字母。
所以我写了一个小的kubectl-use插件:
# 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".
如果你感兴趣,请登录https://github.com/kvaps/kubectl-use
Kubectx—用于切换上下文/集群。可以安装“brew install kubectx” Kubens—用于在集群中的名称空间之间切换。也可以安装“brew install kubens”
Kubie可以切换上下文和名称空间。 如果您安装了FZF,体验将大大改善。
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
您可以使用以下名为kubectx的包,它可以很容易地在使用kubectx的集群之间切换
以及使用kubens在名称空间之间切换