Managing kubeconfig files can become tedious when you have multiple clusters and contexts to switch between. ktx
aims to reduce friction caused by switching between various configurations.
ktx
takes the approach of modifying the KUBECONFIG
environment variable to select the desired config.
- Your shell is bash or zsh.
git
is installed.grep
find
# Clone the ktx repo
git clone https://github.com/heptiolabs/ktx
cd ktx
# Install the bash function
cp ktx "${HOME}"/.ktx
# Add this to your "${HOME}/".bash_profile (or similar)
source "${HOME}"/.ktx
# Install the auto-completion
cp ktx-completion.sh "${HOME}"/.ktx-completion.sh
# Add this to your "${HOME}/".bash_profile (or similar)
source "${HOME}"/.ktx-completion.sh
# Reload your shell
exec bash
Once ktx
is installed you can use it as auto-complete:
$ kubectl get po
The connection to the server localhost:8080 was refused - did you specify the right host or port?
# useful to see what clusters you have in ${HOME}/.kube/
$ ktx <tab><tab>
alpha beta gamma delta epsilon
$ ktx gamma
$ kubectl get po
No resources found.
It is helpful to display the active cluster in the command prompt.
- Find out what the current value of
PS1
:echo "${PS1}"
- Put
"\$(basename \${KUBECONFIG:=\"\"})"
in front of the existing value ofPS1
Note: The backslashes are very important. This tells bash to re-evaluate every time instead of once on load.
salazar:ktx cha$ echo "${PS1}"
\h:\W \u$
# inside .bash_profile
export PS1="\$(basename \${KUBECONFIG:=\"\"}) \h:\W \u$ "
# Reload your shell
exec bash
ktx
is pronounced as "k thanks"