Kubectx and Kubens

I want to introduce you two useful tools for kubernetes development, github link with detail.

Note: this is not an official Google product.

kubectx

kubectx helps you switching between clusters back and forth.

Install

Since kubectx/kubens are written in Bash, you should be able to install them to any POSIX environment that has Bash installed.

1
git clone https://github.com/ahmetb/kubectx /opt/kubectx

Make sure kubectx script is executable:

1
2
3
4
5
6
7
8
9
10
[root@myk8s1 ~] ls /opt/kubectx/ -ltr
total 40
-rw-r--r-- 1 root root 11357 Feb 17 21:25 LICENSE
-rw-r--r-- 1 root root 968 Feb 17 21:25 CONTRIBUTING.md
-rw-r--r-- 1 root root 7784 Feb 17 21:25 README.md
drwxr-xr-x 2 root root 121 Feb 17 21:25 completion
drwxr-xr-x 2 root root 84 Feb 17 21:25 img
drwxr-xr-x 3 root root 100 Feb 17 21:25 test
-rwxr-xr-x 1 root root 5273 Feb 17 21:25 kubens
-rwxr-xr-x 1 root root 5218 Feb 17 21:25 kubectx

Create symlinks to kubectx/kubens from somewhere in your PATH, like /usr/local/bin

1
ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx

Usage

You should first understand how to switch among different clusters by using configuration files. please investigate this link, actually kubectx is built on top of it.

For example, I have one cluster on AWS and one cluster on Fyre, in each cluster there is a ~/.kube/config file, rename it as config.aws and config.fyre and put them to another client machine ~/.kube/ folder with kubectl installed.

1
2
3
4
5
6
7
[root@centctl1 .kube]# ls -ltr
total 16
drwxr-xr-x 3 root root 23 Nov 26 16:38 cache
-rw-r--r-- 1 root root 2214 Dec 6 10:16 config.aws
drwxr-xr-x 2 root root 73 Dec 6 10:16 kubens
drwxr-xr-x 3 root root 4096 Feb 17 22:05 http-cache
-rw------- 1 root root 5474 Feb 17 22:22 config.fyre

Append config files to environment variable KUBECONFIG , you can add export to .bashrc file.

1
export KUBECONFIG=$KUBECONFIG:$HOME/.kube/config.aws:$HOME/.kube/config.fyre

Now if you run kubectx you will see there are 2 contexts:

1
2
3
[root@centctl1 .kube] kubectx
arn:aws:eks:us-west-2:296744932886:cluster/IIS-Test-Cluster
kubernetes-admin@kubernetes

Jump to kubernetes-admin@kubernetes:

1
2
[root@centctl1 .kube] kubectx kubernetes-admin@kubernetes
Switched to context "kubernetes-admin@kubernetes".

Jump back:

1
2
[root@centctl1 .kube] kubectx -
Switched to context "arn:aws:eks:us-west-2:296744932886:cluster/IIS-Test-Cluster".

It is the same as you run below commands:

1
2
kubectl config view
kubectl config --kubeconfig=config.fyre use-context kubernetes-admin@kubernetes

kubens

kubens helps you switch between Kubernetes namespaces smoothly, so you don’t need to add -n <namespace> in every command.

Install

Download the same Github repository as kubectx, add symlinks:

1
ln -s /opt/kubectx/kubens /usr/local/bin/kubens

Usage

1
2
3
4
5
6
7
8
9
10
[root@myk8s1 ~] kubens
default
kube-public
kube-system
[root@myk8s1 ~] kubens kube-system
Context "kubernetes-admin@kubernetes" modified.
Active namespace is "kube-system".
[root@myk8s1 ~] kubens -
Context "kubernetes-admin@kubernetes" modified.
Active namespace is "default".
0%