Blog

Managing Kubernetes Contexts with kubectx

By Jay

Aug 13, 2024 | 4 minutes read

Series: kubernetes

Tags: blog, tech, kubernetes

I’m doing a bit more work in Kubernetes again, which means switching between a number of ephemeral clusters. I’ve tried a number of different ways to do this in the past, but I keep coming back to kubectx and fzf as the easiest way.

To be clear, there are a great number of ways to go about this, of which this is but one very opinionated approach created by someone who is fine with “good enough”.

The first thing I do is to download the config file from my new environment and save it into a local file. I then check this file to ensure that the cluster address is not 127.0.01 - a number of the workstation based K8s distributions assume that you will be working on the same system that you are running your cluster on, so they go with localhost. If this is the case, get the ip address of the system hosting your cluster (ip -a works pretty well here), and replace the localhost references with that. (I have not had firewall issues with this in the past, and it seems like the clusters are listening on 0.0.0.0, but your mileage may vary.)

The next step is to merge that config in with our existing config file. The steps below walk you through this process:

# Create a backup
$ cp ~/.kube/config ~/.kube/config.bak

# Use the "--flatten" option to kubeconfig to merge the new config into the
# temporary file we are using
$ KUBECONFIG=~/.kube/config:/path/to/new/config kubectl config view --flatten > /tmp/config

# Move your new config into place
$ mv /tmp/config ~/.kube/config

# Cleanup
$ rm ~/.kube/config.bak 

Check to ensure your configuration has been added:

$ kubectl config get-contexts
CURRENT   NAME                  CLUSTER            AUTHINFO              NAMESPACE
          admin@talos-cluster   talos-cluster      admin@talos-cluster   default
          admin@talos-prox      talos-prox         admin@talos-prox      default
          docker-desktop        docker-desktop     docker-desktop
          kind-kind             kind-kind          kind-kind
*         microk8s              microk8s-cluster   admin

Now, you can use the kubectl context based commands to change your context, but that is somewhat painful. Fortunately, the kubectx command exists to make this much easier.

To install kubectx you first need to have the krew plugin manager instlaled. If you do have it installed, you can skip the next step. If you don’t have it installed, you can run the followiing code to pull down and install the version of the plugin for your architecture and operating system.

$ (
  set -x; cd "$(mktemp -d)" &&
  OS="$(uname | tr '[:upper:]' '[:lower:]')" &&
  ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" &&
  KREW="krew-${OS}_${ARCH}" &&
  curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" &&
  tar zxvf "${KREW}.tar.gz" &&
  ./"${KREW}" install krew
)
Add the $HOME/.krew/bin directory to your PATH environment variable. To do this, update your .bashrc or .zshrc file and append the following line:

export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"

Add the install location ~/.krew/bin to your PATH as instructed above, and then run kubectl krew to ensure that it works - you should see the usage screen.

Once this is installed, you can now add your plugins. To install kubectx simply run:

$ kubectl krew install ctx

This will install the plugin, which you can then use to pick the context to use. Howerver, the plugin requires that you pass the name of the context you want to change into. If you’re lazy like me you want to find an easier way; enter fzf (fuzzy-find) which is leverage by kubectx if it is installed.

If you are running Ubuntu or a derrivtive, you can install from the built in package manager:

$ sudo apt install fzf

The last step is to create an alias for the plugin to make things a bit easier:

$ alias kubectx="kubectl ctx"

From here out, you can switch between contexts by typing kubectx and using the arrow keys to choose the context you wish to connect to:

Using kubectx

To clean up contexts you no longer use, you can use kubectx -d <contextname> or, if you have the context you wish to delete selected, you can use kubectx -d ..

Finally, if you want to set a default nameapsce (which is something I rarely do), you can use kubens which is a sister utility to kubectx. This works the same way with namespaces that kubectx works with contexts.