kubectl on steroids. There is life beyond k9s.

Sergii Bieliaievskyi
5 min readJun 18, 2022

Forewords.

The text below is for those, who are in the beginning of the journey to kubernetes, however if you are senior, you might also find something interesting. For instance, fzf is a tool which works well not only with kubectl. Check out why the interactive “explain” command could become your everyday tool.

The article intends to get across a very simple thing, if you are new to kubernetes do not try to cheat the system at the very beginning of your journey. Choosing tools like Lens or k9s could be a big mistake. They are great, but the way, back to the basics could be painful. If you are asking yourself right now, what is “basics”, the answer is — kubectl. Working with kubectl could be tedious, it slows you down, however, you as a freshman must go through pain in the beginning, in order to be able to consciously choose your next tool.

Don’t be afraid, with little modifications “kubectl” will stop being so unpleasant. In some cases it might be even irreplaceable. ( you will know why it is irreplaceable in the end of the article)

Needless to say, that often, working with a cluster you run a sequence of kubectl commands and the more commands your type, the more irritating it is.

The most frequently used commands are “kubectl get OBJECT” and “kubectl describe OBJECT”. OBJECT is, kind of, variable — 10 OBJECTs, 10 times you need to type its name. Until this moment it was the problem, but now we can split the screen. On the left panel, there is the list of OBJECTS. On the right panel, OBJECT’s description.

This significantly speeds up the process. Furthermore, you can search the description (Ctrl+f), add “-o wide” (Ctrl+L) to the output and scroll the description (Shift+up/down).

Logs. 10 replicas, 10 logs, 10 times “kubectl logs POD_NAME”. How about that?

Imagine editing k8s manifest. You do not remember “apiVersion” or how to define pod anti affinity. “kubectl explain” is here to help you. And again, without any modifications “kubectl explain” is not an user friendly command. However the purpose of the article is to show that with little effort, “explain” will definitely be your everyday tool, even if you have worked with kubernetes for many years. Do you remember the given promise, kubectl is irreplaceable in some cases? Probably none of the tools like Lens or k9s can do what “kubectl explain” does, but interactive “explain” is even better.

So, how does it work?

The first well-known modification, which needs to be applied, is alias k for kubectl. However, it is not a simple alias k=kubectl but rather bash function k() {…}

The second modification. fzf (command-line fuzzy finder) — https://github.com/junegunn/fzf

Let’s explore how these tools work together.

k() {  ...  case "$@" in  ...  ?(-n | --namespace)?([a-z0-9-]*)get?( )+([a-z]*)?(-n | --namespace)?([0-9a-z-]*) )
NS=$(kubectl $@ -o jsonpath='{.items[*].metadata.namespace}' | sed 's/ /\n/g' | uniq)
NAMESPACE=${NS:-$(kubectl $@ -o jsonpath='{.metadata.namespace}' | sed 's/ /\n/g')}
__get_obj__ $OBJ
;;
*) kubectl $@;;
esac

“k get pod” calls the __get_obj__() function. The pattern “?(-n | — namespace)?([a-z0–9-]*)get?( )+([a-z]*)?(-n | — namespace)?([0–9a-z-]*)” covers all possible combinations of “… get pod …”.

  • k -n default get pod
  • k — namespace default get pod
  • k get pod — namespace default
  • k get pod -n default
__get_obj__(){export RS_TYPE=$(echo $1 | base64 -d)export FZF_DEFAULT_COMMAND="kubectl get ${RS_TYPE} -n ${NAMESPACE:-default}"export FZF_DEFAULT_COMMAND_WIDE="${FZF_DEFAULT_COMMAND} -o wide"fzf --header-lines=1 --info=inline \--prompt "CL: $(kubectl config current-context | sed 's/-context$//') NS: $(kubectl config get-contexts | grep "*" | awk '{print $5}')> " \--header $'>> Scrolling: SHIFT - up/down || CTRL-/ (change view) || CTRL-R (refresh. omit -o wide) || Ctrl-L (-o wide) || Ctrl-f (search word) <<\n\n' \--preview-window=right:50% \--bind 'ctrl-/:change-preview-window(70%|40%|50%)' \--bind 'ctrl-r:reload:$FZF_DEFAULT_COMMAND' \--bind 'ctrl-L:reload:$FZF_DEFAULT_COMMAND_WIDE' \--bind 'ctrl-f:execute:kubectl -n ${NAMESPACE:-default} describe $RS_TYPE {1} | less' \--bind 'enter:accept' \--preview 'kubectl -n ${NAMESPACE:-default} describe $RS_TYPE {1}'}

It might look complicated but what we’ve done here, is just kubectl get pod | fzf. All the rest is simply interface configuration.

You can find more details on the GitHub repo https://github.com/pymag09/kubecui for further exploration. (I strongly encourage you to go and see what is in the repo)

Several commands were not mentioned yet. They are not so great as the others (especially k explain OBJECT) but still reduce time spent for typing.

Here they are.

  • k config set ns

Set default namespace for a context. One of the best practices is to avoid using “default” namespace. The goal is to type less and work faster. Having set namespace, other than the default, you can omit -n/ — namespace if you work predominantly within one namespace.

  • k config use-context

Switch the context. If you have more than one cluster, “k config use-context” search and switch context way faster.

Final words.

Under any angle, k9s is way better and nicer than any kubectl + fzf, but working with k9s you mostly use hot keys, take k9s away from you and you are helpless.
On the other hand by using kubectl + fzf you are still in the kubectl space, nobody can take it away from you. Moreover, any exams — CKA, CKAD, CKS requires you to feel comfortable with kubectl. Nobody stops you from creating alias k=kubectl during the exam. Doesn’t matter k get pod or kubectl get pod it is still your territory.

--

--