Use kubie to simplify changes between kubernetes contexts and namespaces

2 min read | by Jordi Prats

On another post we talked about how to change kubernetes context using kubectl and there's another post for setting a default namespace for a given context. kubie is a tool that helps trying to make it easier

You'll be able to get kubie from it's github repo. The installation process is quite straightforward. For the latest version at the time of this writing it would be:

sudo wget https://github.com/sbstp/kubie/releases/download/v0.13.1/kubie-linux-amd64 -O /usr/local/bin/kubie
sudo chmod +x /usr/local/bin/kubie

Once we have it installed we will be able to get the list of available contexts by using kubie ctx:

$ kubie ctx
arn:aws:eks:us-west-2:249563919847:cluster/cluster-a
arn:aws:eks:us-west-2:111938684653:cluster/cluster-b
arn:aws:eks:us-west-2:473329064472:cluster/cluster-c
arn:aws:eks:us-west-2:580756436539:cluster/cluster-d
arn:aws:eks:us-west-2:111938684653:cluster/spinnaker
minikube

The kibie's nicest feature is it's ability to spawn a new shell and setting it's prompt in a way that we can see the context and the namespace we are working on. To be able to use it we will need to use the -r flag when we tell kubie to switch context:

jprats@croscat:~$ kubie ctx arn:aws:eks:us-west-2:580756436539:cluster/cluster-d -r
[arn:aws:eks:us-west-2:580756436539:cluster/cluster-d|default] jprats@croscat:~$ 

It works in a similar fashion for the switching to a different default namespace. The command we'll need to use is kubie ns:

[arn:aws:eks:us-west-2:580756436539:cluster/cluster-d|default] jprats@croscat:~$ kubie ns
autoscaler
default
kube-node-lease
kube-public
kube-system
sonarqube
spinnaker
[arn:aws:eks:us-west-2:580756436539:cluster/cluster-d|default] jprats@croscat:~$ kubie ns spinnaker
[arn:aws:eks:us-west-2:580756436539:cluster/cluster-d|spinnaker] jprats@croscat:~$ 

We can also combine both switches by using the -n flag to also set the default namespace at the same time we switch context:

jprats@croscat:~$ kubie  ctx arn:aws:eks:us-west-2:580756436539:cluster/cluster-b -r -n sonarqube
[arn:aws:eks:us-west-2:580756436539:cluster/cluster-b|sonarqube] jprats@croscat:~$ 

Posted on 17/03/2021