Quick tip: kubectx for easy switch between clusters

Depending on your company, maintaining more than one Kubernetes cluster can be quite usual. However, typing kubectl config get-contexts and kubectl config use-context to list the contexts and switch, respectively, (i) is boring, (ii) demands effort and (iii) is not intuitive. Thanks to kubectx you can easily switch between contexts.

At Onyo, we have to switch among three clusters. Therefore, the commands below are often used:

1
2
3
kubectx             : list the contexts
kubectx -           : switch to the previous context 
kubectx <NAME>      : switch to context <NAME>

The kubectx README presents other commands that can be helpful to you:

1
2
3
4
5
6
7
kubectx -c, --current     : show the current context name
kubectx <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
kubectx <NEW_NAME>=.      : rename current-context to <NEW_NAME>
kubectx -d <NAME>         : delete context <NAME> ('.' for current-context)
                            (this command won't delete the user/cluster entry
                            that is used by the context)
kubectx -u, --unset       : unset the current context

PS: If you have fzf installed you get a wonderful interactive menu with fuzzy searching by typing kubectx or kubens.

That’s all folks!


comments powered by Disqus