DEV Community

Joseph Matthias Goh
Joseph Matthias Goh

Posted on

A Kubectl Alias Dotfile & Other Life-Enhancing Kubectl Tools

TL;DR

It's here: https://gist.github.com/zephinzer/f0a321e9bcb204debd45160b890eb6a3


The backstory

I began actively using Kubernetes some time back, and the commands were getting too lengthy to type out. Imagine having a pod on fire and you're still typing kubectl get pods -n somenamespace | grep deploymentname, finding the pod with the dreaded CrashLoopBackOff, and finally running kubectl log -f -n somenamespace podname.

So. I created a set of aliases that worked using abbreviations of Kubernetes's inbuilt verbs and resource types:

Verbs

  • apply (a)
  • config (conf)
  • describe (d)
  • delete (del)
  • edit (e)
  • exec (exec)
  • expose (exp)
  • get (g)
  • logs (l)
  • port-forward (pf)
  • run (r)
  • top (t)

Resources

  • config (c)
  • configmap (cm)
  • cronjob (cj)
  • clusterrole (cr)
  • clusterrolebinding (crb)
  • deployment (d)
  • daemonset (ds)
  • endpoint (e)
  • ingress (i)
  • job (j)
  • pod (p)
  • persistentvolume (pv)
  • persistentvolumeclaim (pvc)
  • node (n)
  • namespace (ns)
  • role (r)
  • rolebinding (rb)
  • secret (sec)
  • service (s)
  • serviceaccount (sa)

Lazy Utility Exports

This set of aliases also exports two functions I've found really useful to avoid typing the -n <NAMESPACE> which happens quite often if you've correctly set up your cluster. Presenting..

kcsetns ${YOUR_NAMESPACE} - this sets your namespace to a string value which all future kubectl commands will respect.

kcgetns - this retrieves the current namespace context of your kubectl commands.


OK, so I promised some other life-enhancing tools as well. I've found these extremely useful when administering a Kubenretes cluster without a UI:

  1. derailed/k9s - a really cool curses-based interface that imo works better than the Kubernetes Dashboard for administering a cluster
  2. wercker/stern - a logs collater that allows you to view logs from all selected pods at once

Till next time!

Top comments (0)