Set kubectl contexts/namespaces per shell/terminal to manage multi Kubernetes clusters at the same time, like kubectx/kubens but per shell/terminal.
- If you manage multi K8s clusters, you will need a handy way to work more than one cluster at the same time.
- kubectx/kubens tools fix this problem but globally, that means all shells and terminals will have the same context/namespace.
- However, this is not helpful at all if you are actually working with multi clusters at the same time.
- Thus,
kubech
comes to fix this problem so you can set K8s cluster context/namespace per shell/terminal.
- Each shell/terminal will have its own context (K8s cluster) or namespace!
- Easy to use! Just source it in your favorite shell.
- It works out of the box! No aliases, no extra customizations, it doesn't matter if you work with 1 or 100 K8s clusters!
- It still can be used along with other tools like
kubectx/kubens
. - It doesn't change anything in
~/.kube/config
file. So it works perfectly with static and dynamic kube config files. - It supports extra kubectl config (in addition to
~/.kube/config
). - It supports autocomplete for contexts and namespaces.
Clone this repo, then source kubech
in your shell config file.
git clone https://github.com/DevOpsHiveHQ/kubech ~/.kubech
echo 'source ~/.kubech/kubech' >> ~/.bashrc
echo 'source ~/.kubech/completion/kubech.bash' >> ~/.bashrc
Completion for zsh
with oh-my-zsh
enabled.
echo 'source ~/.kubech/kubech' >> ~/.zshrc
ln -s ~/.kubech/completion/kubechn.zsh ~/.oh-my-zsh/completions/_kubechn.zsh
ln -s ~/.kubech/completion/kubechc.zsh ~/.oh-my-zsh/completions/_kubechc.zsh
Please make sure autoload -U compinit && compinit
is added to ~/.zshrc
if not please execute below command.
echo 'autoload -U compinit && compinit' >> ~/.zshrc
NOTE:
- The command "kubech" is just a meta for other commands. So kubech does nothing by itself.
- Also short names like "kchc/kchn/kchu" are available.
VARS:
KUBECONFIG_SRC_DIR : Set directory with extra kubectl config files to read in kubech commands.
This allow to have multiple config files in addition to
the one at "\$HOME/.kube/config".
Default: "$HOME/.kube/config.src.d"
KUBECONFIG_DEST_DIR : Set directory for temporary kubectl config files. The files
in this directory are auto-generated by kubech commands
and you don't need to interact with them or even change that var.
Default: "$HOME/.kube/config.dest.d"
KUBECH_NAMESPACE_CHECK : Method used to check if namespace exists before switching to it.
The default lists all namespaces which can be slow in large
clusters. After kubernetes 1.22+ all namespaces have a label
that can be used to speed this up. Set to "label" to enable this.
Available options: "list", "label".
Default: "list"
USAGE:
kubechc : List all contexts
kubechc <CONTEXT> : Switch to context <CONTEXT>
kubechn : List all namespaces
kubechn <NAMESPACE> : Switch to namespace <NAMESPACE>
kubechu : Unset the active context. This is just a safty net
to avoid applying config by mistake to the wrong cluster.
$ kubechc
k8s_cluster01
k8s_cluster02
No need to write the full name, press tab for auto complete.
$ kubechc k8s_cluster01
$ kubechn
default
kube-public
kube-system
monitoring
No need to write the full name, press tab for auto complete.
$ kubechn monitoring
- This tools has been tested with
bash 4.4
only. However it should work with other shells likezsh
. - It's recommended to use this tool with kube-ps1 so current cluster is more visible.
- It's still recommended to have kubectx/kubens to manage contexts/namespaces globally.
- Test it with older
bash
versions like bash v3 on MacOS.