Minimalistic kubectl context/namespace switcher
Find a file
2025-06-01 00:07:15 +02:00
ktx Tighten access rights even further 2025-06-01 00:07:15 +02:00
README.md Fill README with content 2025-05-31 22:07:02 +02:00

ktx

Plant a Tree, Have a Baby, Build a k8s context switcher

Motivation

Like every good YAML Engineer, I'm constantly switching between half a dozen k8s clusters with a big list of namespaces in each of them. This gets cumbersume with just plain kubectl, so a big ecosystem of context switchers has evolved. I tried some of them, but I had at least one problem with each of them, so I started my own to fullfill my needs. My personal requirements for a context switcher are:

  • When opening a new shell I want to be in the context I used last
  • When switching context or namespace in one shell, all other shells should not be affected

Installation

Requirements:

  • kubectl, fzf and jq are installed
  • Your global k8s config is in ~/.kube/config

Setup:

# Clone the repo somewhere
cd ~/repos && git clone https://git.rainbownerds.de/fleaz/ktx

# Source the ktx script in your shell config
echo "source ~/repos/ktx/ktx" >> .zshrc

# Create the required directory
mkdir ~/.kube/ktx

Now open a new shell and you should have ktx and kn available

Usage

# ktx alone to get a list of all your contexts to choose from
ktx
# ktx with the name of a context to directly switch to it
ktx prod-ams1
# ktx with the name of a context and a namespace
ktx prod-ams1 monitoring

# kn alone to get a list of all namespaces in the current cluster
kn
# kn with the name of a namespace to directly switch to it