Let us get started with a few questions:
Have you ever been staring at the terminal window but don’t remember which Kubernetes cluster it is set up for?
How about multiple terminal windows for multiple Kubernetes clusters?
Are you tired of typing the export KUBECONFIG
or kubectl get pod -n demo
commands over and over again?
If the answer is yes, please continue reading because we can solve these problems and make it a lot more fun!
In our environment, we always have several different Kuberenetes clusters at the same time, with different flavors of course:
- Multiple Kubernetes versions: v1.11, v1.12, and v1.13
- Different network solutions: Flannel, Calico, AWS-VPC-CNI
- KubeProxy with iptables mode vs. IPVS mode
- KubeProxy IPVS mode with different load balancing algorithms: round-robin, least-connection, shortest expected delay, and etc.
- Common environments: Prod, Staging, QA, Dev, and POC
Switching between these clusters and working efficiently can be a real challenge. It is very easy to lose track of which terminal is set up for which cluster. I have to type kubectl cluster-info
all the time to show the current cluster name.
After a while, I got really tired of this. Besides, I am too lazy to type the same commands over and over. And that is when I started to pimp my k8s shell.
Use kubectl alias
# Step 1: kubectl has 7 characters, make it 1
alias k=kubectl# Step 2: follow your own preferred way
alias kg='kubectl get'
alias kl='kubectl logs '
alias kx='kubectl exec -i -t'
...
You can start to get really creative and make it completely customized for your own style. Here is a good example from Ahmet Alp Balkan’s article with his alias naming conventions as below. I used this as a template and tweaked it to follow my own system and habit.
Export KUBECONFIG is slow and boring
# Save this in bash.rc or zsh.rc, update the file path if needed.
# Usage: kube clusterAkube() { export KUBECONFIG=~/k8sconfig/${1}/kubeconfig}
Install this K8S highlighter and see the difference
Before: where cluster am I in?
After: I am in goku
cluster with namespace kube-system
as admin
As shown above, both cluster name and namespace are shown and highlighted, very visually pleasing. This plugin is called kube-ps1 and can be found here.
Stop typing namespace in every command
We can save more typing by letting our terminal remember the current namespace. Here are two convenient helper tools: kubens and kubectl change-ns plugin. Pick your favorite.
kubens can be installed together with kubectx, this package has both binaries bundled in. If you want to make it more fun and interactive, try installing fzf. From the demo below, you can see it working seamlessly with both kubectx and kubens.
If you have multiple contexts in the same k8skubeconfig
file, kubectx can be convenient.
In my case, each cluster has only one context which is admin@${cluster}.
To constantly switch between different clusters, kube ${cluster}
is pretty helpful.
There is another kubectl plugin called change-ns which can achieve the same goal. If you are already using krew to manage your kubectl plugins, it can be installed quite easily.
kubectl krew install change-ns
Recap
This recap picture above shows some of my common usages:
- First, choose a cluster or switch to a different one using
kube
- Second, use
kubens
orchange-ns
to select a namespace to work on; use the interactive mode if preferred. - Use kubectl alias
kg
to get services, pods, and endpoints
That’s all, Hope this helps to save you some typing time and make it more enjoyable to interact with Kubernetes in the terminal.
PS: Just in case you are wondering, the cluster names I am using are coming from the Dragon Ball characters.
References
https://github.com/ahmetb/kubectx
https://github.com/jonmosco/kube-ps1
https://github.com/GoogleContainerTools/krew
https://ahmet.im/blog/kubectl-aliases/