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.
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:
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.
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:
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.
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.
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
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.
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.
Check out this blog post for a nice collection of K8s sample applications.
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 (@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?
— Daniele Polencic (@danielepolencic) January 28, 2021