Architecturecoding Personal Blog Page

Kubectx

kubectx is a utility to manage and switch between kubectl(1) contexts. Thanks for this Ahmet Alp Balkan.

How to install on OSX

You can to install with manual or package manager. If you want to install with brew or macports, follow the command:

Homebrew
brew install kubectx

Macports

sudo port install kubectx

Kubectx Commands

kubectx -h
USAGE:
kubectx                       : list the contexts
kubectx <NAME>                : switch to context <NAME>
kubectx -                     : switch to the previous context
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 -u, --unset           : unset the current context
kubectx -d <NAME> [<NAME...>] : delete context <NAME> ('.' for current-context)
(this command won't delete the user/cluster entry
referenced by the context entry)
kubectx -h,--help             : show this message
kubectx -V,--version          : show version

How to merge clusters

If there are more clusters in config file, you can combine these files with the kubectl config view command.

For example, i have two kubeconfig file. These are stagging.yml and prod.yml. I merged with this command.

KUBECONFIG=stagging.yml:prod.yml kubectl config view --merge --flatten > ~/.kube/config

and don’t forget permissions

chmod go-r ~/.kube/config

Lets get to know Kubectx

  kubectx
kubernetes-admin@saturn
kubernetes-admin@saturn-prod
<b>kubernetes-admin@saturn-stagging</b>

for switch

  kubectx  kubernetes-admin@saturn-prod
 Switched to context "kubernetes-admin@saturn-prod".

and rename context name

  kubectx prod=kubernetes-admin@saturn-prod
 Context kubernetes-admin@saturn-prod renamed to prod.

I hope it was useful.

Happy Friday!