As you can see from my weekly Tweets, I am usually doing something that involves Kubernetes (K8s), whether that it is playing with Knative and our VMware Event Broker Application solution or doing some deep R&D research. I wanted to share some of the tips and tools that I have been using which has been helpful for me to better learn and interact with K8s whether that is using vSphere with Tanzu, Tanzu Kubernetes Grid (TKG), KinD or other K8s distributions like k3s.
By no means is this a comprehensive list and I know there are many other collections including the curated Awesome Kubernetes project. If there are other useful K8s tricks or tools that you would like to share, feel free to leave a comment below.
Alias kubectl
When you are debugging or troubleshooting, it can quickly get annoying when you have to type out "kubectl" each time along with the rest of the command. Instead, just create a simple alias for kubectl. You can also alias any CLI command and this is supported on macOS, Linux and Windows.
I am usually on a macOS or Linux system and you can use the alias command like the following:
alias k=kubectl
Instead of having to type:
kubectl get pods
You can now type:
k get pods
Note: Make sure to update your shell (zshrc/bashrc) with the alias command to ensure it persists.
Kubealias
We can take aliasing kubectl commands a step further because once you start to really use K8s, you will quickly realize there are MANY more longer commands. This is where the kubectl-aliases project can help and automatically generates hundreds of short hand kubectl commands that you can benefit from without having to figure out your own alias syntax. Once you incorporated the alias file into your shell, to retrieve all pods, you can just type:
kgpo -A
instead of
k get pods -A
Note: I will say, I have not memorized all the short hand aliases yet. Part of the reason, I am still learning about K8s and doing it the "hard/long" way helps me better understand the system.
Kubecolor
If you are new to K8s and in particularly during debugging and troubleshooting, the output from the various kubectl command can make you feel a little lost. I certainly felt and still feel that way, especially when you start to inspect the output in either YAML or JSON. This is why I highly recommend the kubecolor plugin which simply colorizes your kubectl output and makes it much easier to parse, especially if there are things to get your attention like errors or failed deployments.
As you can see from the screenshot below this plugin works with aliases, so whether I use "k" or the more extensive kubectl aliases, it will properly render the output. This is by far one of my favorite CLI tools!
Kubectl api-resources & explain
If you are like me and still learning about the various K8s resources and constructs, you definitely should get familiar with these two sub-commands that is built right into kubectl itself called api-resources and explain.
The api-resources will list all K8s resources that are available, this is useful if you forget any of the default resources but it is also handy to see what additional resources may have been installed to your K8s cluster as part of a custom K8s CRD.
k api-resources
To learn more about a given K8s resource, you can simply use the explain command and the name of the resources like a pod as shown in the example below.
k explain pod
In addition to the built-in documentation for each K8s resource, you can also inspect which properties are supported by providing the next object path. For example, if you wanted to see the properties for the Pod spec, simply run the following command:
k explain pod.spec
Kubeps1
When I first started working with Tanzu Kubernetes Grid (TKG), I realized that you were switching between different K8s context (Management and Workload Clusters). At the time, it was not always clear on which K8s cluster I was connected to and I am sure this is also true for any K8s beginners. While building my TKG Demo Appliance, which is a super easy and free way to get started with K8s, I came to learn about the kubeps1 project which simply displays the current K8s context and namespace right in the shell prompt, so you always know which K8s cluster you are currently authenticated into.
Note: If you find yourself working across a large number of K8s cluster, you may want to setup kubectx which I have seen many folks in the community recommend. I normally stay within a single K8s context, so switching context is usually not an issue for me personally to warrant an additional plugin installation.
Kubeshell
If you need to inspect and get shell access to a K8s pod, kubeshell is a simple little utility that accepts the K8s namespace and then allows you to quickly select the pod and get a shell.
More Kubectl Plugins
There are MANY more plugins that are available for kubectl. Check out awesome-kubectl-plugins which looks to be a pretty comprehensive collection of plugins that you can check out and setup to your hearts content.
Graphical UI Tools for K8s
I normally do not use a UI for interacting with K8s, but when I do, I still prefer Octant. It includes many of the capabilities provided by some of the mentioned plugins above and it is a great tool to orient yourself on the various K8s constructs if you are just getting started. You can check out my interactive terminal and graphical UI tools for K8s write-up if you are interested in learning about other UI-related tools for K8s.
Demo Applications
Check out this blog post for a nice collection of K8s sample applications.
Other Resources
I would be remiss not to mention KubeAcademy which is a free online resource where you can learn about K8s in a self-pace manner. The courses are not focused on vendor specific K8s offering, but upstream K8s and is taught by various K8s experts within the community. I personally have not used KubeAcademy but I know it has come highly recommended and it has been on my list to check out.
For a more VMware-focused online resources, our Modern Applications Business Unit (MAPBU) has launched the modernapps.ninja which includes free in-depth content across our Tanzu portfolio covering Level 101, 201, 301 and even non-VMware technologies as it pertains to developing, building and managing modern applications.
UPDATE (02/17/21) - Thanks to reader mageru who shared a note about official K8s cheat sheet that has a number of tips/tricks including tab auto-completition for Kubectl.
Lastly, I also wanted to mention two additional resources that I recently came across on my Twitter feed that I think folks might also find useful.
The Kubernetes Cheat Sheet by A Cloud Guru which is a nice resource that summarizes many of the common kubectl commands which you can print out and hang on your office wall.
The only Kubernetes cheat sheet you'll need! pic.twitter.com/VX9nGxClMA
— A Cloud Guru | A Pluralsight Company (@acloudguru) December 26, 2020
Thee Kubernetes Troubleshooting Flow Chart is a nice flowchart that you can follow to debug/troubleshoot K8s Cluster. In fact, I was able to quickly use this to identify some configuration issue I had with a deployment. If not thing else, it will at least give you an idea of the areas to take a look at when you do run into problems.
I updated the Kubernetes troubleshooting flowchart!
When something goes wrong in Kubernetes, how do you know what should be fixed?
In this blog post (and flowchart) you will find all the tips that I use to debug deployments.https://t.co/TWJAVggNyR pic.twitter.com/rOuPrYIrXs
— Daniele Polencic — @*protected email* (@danielepolencic) January 28, 2021
mageru says
To add to the alias pro-tip, you can enable bash tab completion with kubectl and it's k alias using the following commands.
Mentioned at the top of the official kubectl cheatsheet -> https://kubernetes.io/docs/reference/kubectl/cheatsheet/
source <(kubectl completion bash) # setup autocomplete in bash into the current shell, bash-completion package should be installed first.
echo "source > ~/.bashrc # add autocomplete permanently to your bash shell.
You can also use a shorthand alias for kubectl that also works with completion:
alias k=kubectl
complete -F __start_kubectl k
William Lam says
Thanks mageru and I didn't know there was an official K8s cheat sheet, will add that to post.
Sam McGeown says
Nice collection, I like the look of kubecolor (even if they've spelled colour incorrectly ;-)) - I've got a couple more suggestions for you that I have found to be really helpful:
kubectx and kubens - https://github.com/ahmetb/kubectx - really useful tools for switching between kubernetes contexts and namespaces! (brew install kubectx)
stern - https://github.com/wercker/stern - you can do the equivalent of "tail" and "kubectl logs ..." on all the pods in a deployment or namespace, with each pod's logs in a different colour - really useful for troubleshooting! (brew install stern)
William Lam says
Yea, I've already mentioned kubectx in the notes.
Stern looks really cool! Definitely will give that a try
Sam McGeown says
Whoops, missed your mention of kubectx!
The other thing that’s worth mentioning is is if you (or your readers) are considering doing the CKA/CKAD/CKS exams then the kube-alias shortcuts will not be available. I got to the point of muscle memory with the commands and then had to unlearn them during the exam, which in a time-pressured exam is...sub-optimal!