Kubernetes is complex. Not only are there dozens and dozens of Kubernetes-specific tools in the Cloud Native Foundation Landscape, the base management of Kubernetes and its underlying structure itself can be daunting. While there are a number of web-based tools for monitoring your clusters, such as Octant, which we took a look at here, your day-to-day cluster management is likely to be on the command line. This post looks at a number of tools, from the essential to the nice-to-have that improve your day to day cluster management life. We will start with the main tool, kubectl, looking at how we can simplify the user experience, and then layer tools on top.

I am going to use bash as the shell for the guide, meaning any computer running a basic bash terminal should be able to take advantage of the tips. There are advantages to other shells, such as zsh, but I will leave that for you to experiment with.

If you want to put these tools to use, sign up to Civo!

Kubectl

Kubectl is the primary tool for dealing with and managing your clusters from the command line. Through kubectl you can see the status of individual nodes, pods on those nodes, policies and much more besides.

Kubectl installation instructions are here. All of our Kubernetes guides will presume you have it set up on your machine.

For a full list of statuses kubectl can show you, simply run kubectl api-resources. An overview of the structure of Kubernetes is outside the scope of this guide, but if you are curious as to how the parts relate to each other, take a look at this post.

Besides showing you information about an already-deployed cluster, kubectl is also used to deploy applications and services to a cluster.

As kubectl commands can get long, I recommend two things: First, setting up some aliases to increase typing efficiency, and second, making sure your kubectl command completion is set on. For the aliases, you can do anything you feel works for you, based on the following example. In ~/.bash_profile edit in:

alias k=kubectl
alias ka='kubectl apply -f'
alias kg='kubectl get'
alias kx='kubectl exec -i -t'

The first one will make the simple command k an alias for kubectl saving you 6 keystrokes every time! The following lines alias shorter forms for some of the most commonly-used commands that I have set up in my profile. That means I can type kg pods and get a listing all pods for the current context and/or namespace.

Kubectl functions through contexts, meaning you can manage multiple clusters by switching context. You can check the current context by running kubectl config current-context but if you are going to be working with clusters frequently, I recommend setting up a shell prompt display like this one I am running for bash. You would need to edit your ~/.bash_profile file and insert the following at the top:

get_kubernetes_context()
{
  CONTEXT=$(kubectl config current-context 2>/dev/null)
  KUBE_SYMBOL=$'\xE2\x8E\x88 '
  if [ -n "$CONTEXT" ]; then
    NAMESPACE=$(kubectl config view --minify --output 'jsonpath={..namespace}')
    if [ -n "$NAMESPACE" ]; then
      echo "(${KUBE_SYMBOL} ${CONTEXT} :: ${NAMESPACE})"
    else
      echo "(${KUBE_SYMBOL} ${CONTEXT}:None)"
    fi
 fi
}

Then, in the same file, find the line that starts export PS1=. You will add this new function to this line. As an example, mine looks like this:

export PS1='\[\033[01;32m\]\[\033[0m\033[0;32m\]\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] $(get_kubernetes_context) $ '

When you next load your terminal, you should see the current context appear in your prompt if you have one set. It will unobtrusively hide itself if you do not have a current cluster context. Mine looks like this, showing "None" as I do not have a currently-selected namespace in my cluster:

My Kubernetes Prompt

This way you will be far less likely to commit an action on a cluster you were not intending to touch!

Kubectx

A handy context-switching tool for clusters, saving you headaches of a complicated kubectl command, is to install kubectx. This will allow you to run a command such as kubectx my_cluster and have your context automatically changed. Ahmet Alp Balkan, the author of kubectx has also created kubens which functions similarly, but for namespace switching within a cluster. If you work with a cluster with multiple namespaces, it's worth installing alongside.

K9s

Another command-line tool with some really neat features is k9s. Essentially a fully-fledged Kubernetes management tool that functions on your command line, it is an impressive project. You can install it from here and run it by typing k9s once installed. It will automatically detect your current KUBECONFIG and show you various status screens. In the screenshot below, you can see my cluster provisioning Longhorn for persistent storage, and Jenkins for CI/CD.

k9s in action

k9s functions the same way queries using kubectl do - it connects to the cluster API using http, automatically updating with any changes saving you having to repeat commands. It's a really handy way to monitor pod health, but the strength of it comes in the ability to edit configurations on the fly. By selecting a pod, for example, you can press e to edit a pod's configuration YAML in a new window, and return to k9s to watch the changes be enacted.

You can switch from the default pod view to nodes by using the command :nodes<ENTER>. Any Kubernetes resource type can be viewed in this method, and k9s will automatically show you context-appropriate commands in the top part of the window. Want to scale a particular deployment to 3 replicas? Easy as:

:deployments
(select your deployment of choice using the arrow keys)
s
Enter number of replicas desired, followed by Enter.

Press enter and watch the "desired" column update, and the cluster scale the deployments to that number: Rescale Service

If you need to, you can even open a shell directly into a container running on your cluster directly from k9s by using the s command on a compatible resource.

The last part I will highlight for k9s is the logs command that allows you to view logs for any kind of resource on the cluster, either scrolling automatically with events, or navigable yourself.

If you ever get confused or stuck, the k9s help is always available through the command :help made on any screen.

Bonus: VSCode Kubernetes Snippets

If VSCode is your preferred IDE, this one is for you. Ivan Pedrazas created a handy Kubernetes code snippets extension that allows you to very rapidly create manifest files and configurations for pods, services deployments, etc. You can create deployment templates with just a few key presses:


You can find it on the VSCode extensions tab by searching for kubernetes-snippets.

With the increasingly complex YAML required by clusters, tools like this are worth their weight in gold as they sort indentation and basic structure for you, allowing you to concentrate on what matters. Once you have the structure of your YAML down, you can deploy it quickly using your pre-defined kubectl aliases such as ka my_app.yaml

Conclusion

We have looked at the basic tool of managing Kubernetes clusters, kubectl, and defined aliases and shell prompt helpers to increase our efficiency in using it. We have also installed kubectx and, optionally, kubens to manage context- and namespace-switching with ease. We then looked at a full terminal-based cluster monitoring and management solution in k9s, including how it allows us to easily scale deployments. We then took a look at an easy extension to the VSCode editor that provides code snippets for the most commonly-used Kubernetes YAML files.

What are your favourite Kubernetes tools to use? Let us know if you think we should include another one by commenting on Twitter @civocloud, or our community Slack!