Set kubectl contexts/namespaces per shell/terminal to manage multi Kubernetes cluster at the same time.
i.e. same as kubectx/kubens
but per shell/terminal.
This is a fork of https://github.com/aabouzaid/kubech.
This is a fork of the original kubech repo, i update the original repo to better suit my use-case, so i can't (and don't want to) take credit for the work that had been done by the author of kubech.
All the modifications have been done in a limit time, so i'm sure improvement can still be made. Any PRs or questions are welcome! ;)
- Work on both Linux and MacOS (the original repo have some shell commands that are not compliance with BSD (MacOS))
- Some bugs that can occur when kubecontext's name contains special characters like: colon
(:)
or slash(/)
- Better consistency of output message when change kube context/namespace
- Have kubectl installed
- Have KUBECONFIG predefined in your shell rc. (eg.
export KUBECONFIG=${PATH_TO_KUBECONFIG_1}:${PATH_TO_KUBECONFIG_2}...
)
- 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 git@github.com:salutgeek/kubech.git ~/.config/.kubech
echo 'source ~/.config/.kubech/kubech' >> ~/.bashrc
echo 'source ~/.config/.kubech/completion/kubech.bash' >> ~/.bashrc
Completion for zsh
with oh-my-zsh
enabled.
ZPLUGINDIR=${ZPLUGINDIR:-${ZDOTDIR:-$HOME/.config/zsh}/plugins};
echo 'source ~/.config/.kubech/kubech' >> ~/.zshrc;
ln -s ~/.config/.kubech/completion/kctx.zsh ${ZPLUGINDIR}/zsh-completions/src/_kctx.zsh;
ln -s ~/.config/.kubech/completion/kns.zsh ${ZPLUGINDIR}/zsh-completions/src/_kns.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 "kctx/kns/kundo" 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"
USAGE:
kctx : List all contexts
kctx <CONTEXT> : Switch to context <CONTEXT>
kns : List all namespaces
kns <NAMESPACE> : Switch to namespace <NAMESPACE>
kundo : Unset the active context. This is just a safty net
to avoid applying config by mistake to the wrong cluster.
$ kctx
k8s_cluster01
k8s_cluster02
No need to write the full name, press tab for auto complete.
$ kctx k8s_cluster01
$ kns
default
kube-public
kube-system
monitoring
No need to write the full name, press tab for auto complete.
$ kns monitoring
- This tools has been tested with
zsh 5.8.1
only. However it should work with other shells likebash
. - 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/Linux