A simple tool to manage and switch between multiple Kubernetes configuration files. This tool allows you to dump all your kubeconfig files in a single directory and easily switch between them as well as configure the current namespace.
- Manage multiple kubeconfig files in a single directory.
- Easily switch between different Kubernetes contexts.
- Easily switch between different namespaces.
- Persistent context/namespace switching across different shells.
- Interactive prompts support fuzzy search.
- Non-destructive operations on your original kubeconfig files.
kubectl-switch
will never edit those. It will only work with copies of them.
kubectl-switch
is an alternative to tools like kubectx
, kubens
and kubie
. It has been created because I feel all of those fall short in certain regards:
kubectx
andkubens
assume all your contexts are defined in a single config file. Yes, there is some hackery you can do to yourKUBECONFIG
environment variable to make it work with multiple files, but it is (in my opinion) a sub-par workflow and I never really liked itkubie
spawns a new shell when you use it to change contexts, making it practically impossible to integrate into scripts or taskfile automation. Also I consider it to be too complicated of a solution for what is fundamentally a really simple problem
What I wanted was something very simple conceptually: I just want to dump all my kubeconfig
files in a single directory and then have my tool parse them and "physically" move over the config file to .kube/config
(or whatever is configured in my KUBECONFIG
env var) such that it is also persistent between different shells. Here is where kubectl-switch
comes in!
Precompiled binaries are available for various platforms. You can download the latest release from the GitHub Releases page.
-
Download the appropriate binary for your system and extract the archive.
-
Make the extracted binary executable:
chmod +x kubectl-switch
-
Move the binary to a directory in your PATH:
mv kubectl-switch /usr/local/bin/kubectl-switch
kubectl-switch
is also available as a Docker container:
docker pull ghcr.io/mirceanton/kubectl-switch
-
Add the tap
brew tap mirceanton/taps
-
Install
kubectl-switch
brew install kubectl-switch
-
Clone the repository:
git clone https://github.com/mirceanton/kubectl-switch cd kubectl-switch
-
Build the tool:
If you have Taskfile installed, run:
task build
Otherwise, simply run the
go build
command:go build -o kubectl-switch
-
Place all of your
kubeconfig
files in a single directory. I personally prefer~/.kube/configs/
but you can do whatever you fancy. -
Set the environment variable
KUBECONFIG_DIR
export KUBECONFIG_DIR="~/.kube/configs/" # <- put here whatever folder you decided on at step 1
-
Run
kubectl switch ctx
orkubectl switch context
to interactively select your context from a listvscode ➜ /workspaces/kubectl-switch $ kubectl switch context ? Choose a context: [Use arrows to move, type to filter] cluster1kubectl-switch cluster2 > cluster3kubectl-switch INFO[0102] Switched tkubectl-switchuster3' vscode ➜ /workspaces/kubectl-switch $ kubectl get nodes NAME STATUS ROLES AGE VERSION cluster3 Ready control-plane 21h v1.30.0
Alternatively, pass in a context name to the command to non-interactively switch to it:
vscode ➜ /workspaces/kubectl-switch $ kubectl switch ctx cluster3 INFO[0000] Switched to context 'cluster3' vscode ➜ /workspaces/kubectl-switch $ kubectl get nodes NAME STATUS ROLES AGE VERSION cluster3 Ready control-plane 21h v1.30.0
This will literally detect the file in which that context is defined and copy-paste it to the path defined in your
KUBECONFIG
. -
Run the
kubectl switch ns
orkubectl switch namespace
to interactively select your current namespace from a listvscode ➜ /workspaces/kubectl-switch $ kubectl switch namespace ? Choose a namespace: [Use arrows to move, type to filter] > default kube-node-lease kube-public kube-system INFO[0012] Switched to namespace 'kube-system' vscode ➜ /workspaces/kubectl-switch $ kubectl get pods NAME READY STATUS RESTARTS AGE coredns-7db6d8ff4d-nqmlf 1/1 Running 1 (69m ago) 21h etcd-cluster1 1/1 Running 1 (69m ago) 21h kube-apiserver-cluster1 1/1 Running 1 (69m ago) 21h kube-controller-manager-cluster1 1/1 Running 1 (69m ago) 21h kube-proxy-lxnvr 1/1 Running 1 (69m ago) 21h kube-scheduler-cluster1 1/1 Running 1 (69m ago) 21h storage-provisioner 1/1 Running 3 (68m ago) 21h
Alternatively, pass in a namespace name to the command to non-interactively switch to it:
vscode ➜ /workspaces/kubectl-switch $ kubectl get pods No resources found in default namespace. vscode ➜ /workspaces/kubectl-switch $ kubectl switch ns kube-public INFO[0000] Switched to namespace 'kube-public' vscode ➜ /workspaces/kubectl-switch $ kubectl get pods No resources found in kube-public namespace.
This will modify your currently active kubeconfig file (not the original one from
KUBECONFIGS_DIR
, but rather the copy fromKUBECONFIG
) to set the current namespace.
This project is licensed under the MIT License. See the LICENSE
file for details.