Four easy Kubernetes terminal settings to improve your productivity

I have managed the operation of large Kubernetes clusters for over three years, and I want to share my minimalistic approach to setting up the kubectl terminal, which has proven to be highly effective in everyday work. A well-configured command line terminal can greatly increase your productivity. It’s like bread and butter – a magic combination.

But unlike popular modifications and functional additions, I believe in simple configuration, which does not require installing new binaries, wrappers or modifications. Especially in the case of kubectl – a native, reasonably well-designed tool that has very few downsides.

Here are four simple add-ons to the kubectl terminal. With this configuration, I manage 20 large Kubernetes clusters of 400 machines every day.

Using kubetail to track logs of multiple pods

The first thing you notice when using kubectl is the difficulty of keeping track of logs across multiple pods. This is one of the most requested scenarios that has not yet been implemented in kubectl.

If you need to track the log of a single pod in kubectl, it looks like this:

kubectl logs -f <POD_NAME> -n <NAMESPACE>

To track multiple pods in kubetail, just write:

kubetail <POD_NAME_REGEX> -n <NAMESPACE>

You will start tracking all pods whose name matches the regular expression. Since multiple pods are logged at the same time, kubetail flags them in different colors to help you navigate.

kubetail is a simple wrapper around kubectl. It is easy to install, you can find instructions here

2. Dynamically changing the default namespace

Eventually you will get tired of setting the namespace flag for every kubectl operation over and over again. We need to figure out how to dynamically change the default namespaces.

For this I wrote a simple alias function ksn (name implies set namespace).

# Add the following to .zshrc/.bashrc...etc
# Allows setting default namespace while working with kubectl #

alias k='kubectl'
alias ksn='_f(){k get namespace $1 > /dev/null; if [ $? -eq 1 ]; then return $?; fi;  k config set-context $(k config current-context) --namespace=$1; echo "Namespace: $1"};_f'

#Usage:
#➜  ~ ksn dev1                                                       (dev-context/dev1)
#     Context "dev-context" modified.
#     Namespace: dev1

#➜  ~ ksn ff                                                         (dev-context/dev1)
#     Error from server (NotFound): namespaces "ff" not found

Usually we work with an array of namespaces depending on the current task. And since 80% of the problems are associated with Kafka namespaces, this feature has long been my default tool. It will save you a lot of time, especially when working on complex tasks that require many instructions to be entered into kubectl.

3. Displaying kube-context and namespace in the shell prompt

When using kubectl commands, you should always pay attention to the current kube-context and namespace… It’s not so easy when you simultaneously manage clusters in 20 different contexts and 50 namespaces. There is a risk of using the wrong command in the environment, especially when working on several tasks at the same time in different clusters and contexts.

To solve this problem, it is extremely useful to constantly display the active namespace and context in the adjacent window.

In the example below, my context is dev2–1, and the namespace is test… It is enough for me to just look at this information when I execute different commands in the cluster.

You will find installation and configuration instructions here

4. The most important kubectl aliases

Aliases are the fastest way to customize and speed up your work in the terminal. Here are my most used aliases, which have almost become part of my nature:

alias k='kubectl '
alias kcc="kubectl config current-context"
alias kdp='kubectl delete po'
alias kgc="kubectl config get-contexts"
alias kge="kubectl get events --sort-by="''{.lastTimestamp}''
alias kgp='kubectl get po'
alias kl="kubectl logs "
alias kpf="kubectl port-forward"
alias ksc="kubectl config use-context"

Less is better: don’t overload your terminal

We’ve covered four major terminal improvements for kubectl users:

  1. Using kubetail to track logs of multiple pods.
  2. Dynamically changing the default namespaces to significantly reduce the length of kubectl instructions.
  3. Displaying context and namespaces next to your terminal to avoid accidental errors.
  4. List of important kubectl aliases.

In contrast to this minimalist set, popular terminal upgrades (like K9s) are often overloaded, filled with features and candy wrappers, but they are slow, distracting, and, more importantly, unnecessary. The more features (sequins and bows) you add, the more time it takes to wade through all that extra information on the screen.

The elegant terminal configuration helps you stay focused like nothing else.

Simple command line based configuration will allow you to develop quickly and efficiently using native binaries and tools. This is very important, especially when working with a large infrastructure, logging in and out of different profiles on virtual machines.

Your terminal configuration should enrich you as a developer, not make you feel like you are without hands.

Similar Posts

Leave a Reply