例如,我有两个名称空间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
其他回答
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—用于切换上下文/集群。可以安装“brew install kubectx” Kubens—用于在集群中的名称空间之间切换。也可以安装“brew install kubens”
看看https://krew.sigs.k8s.io/他们有很多不同用例的插件。
安装 https://krew.sigs.k8s.io/docs/user-guide/setup/install/
kubectl krew install kubens
然后运行以下命令将为您设置当前上下文
kubens <namespace>
我在.zshrc中创建了一个函数
ksns() { kubectl config set-context --current --namespace="$1" }
然后我调用ksns默认
我不喜欢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