Help

Vars editor

Variables in articles are noted {{myVar}}

Legend

A link to a page of this blog
A link to a section of this page
A link to a template of this guide. Templates are files in which you should replace your variables
A variable
A link to an external tool documentation
This page looks best with JavaScript enabled

Quality Of Life improvements to kubernetes

 ·  via commit 2f43074 (write: update kubernetes QOL) by Alexandre Germain  ·  ☕ 5 min read

Kubernetes is…. Quite a thing, to say the least ! 😅 Even if their conceptors did a great job at making the kubectl cli as usable as possible, it can sometimes be a pain to be productive with it, read outputs, or do repetitive tasks. That’s why I wrote this small Quality of life improvements post: to regroup some install steps you might have missed, give you some useful 3rd party tools or maybe even give you tips a step ahead.

kubectl auto-complete

Autocomplete is nice, and a real time saver. It avoids typos, and it’s quite satisfying to type a complete command in 4 keystrokes and a couple of tabs correctly placed. (even if I’m always unsure when relying on my browser’s autocomplete for https://analytics.google.com 😑).

But for this one, I can only say one thing, and you have no excuses:

 RTFM

So, short stories short, and depending on your shell, type in:

1
2
3
4
5
6
cat <<EOF | tee -a {{profileFile}}
autoload -Uz compinit
compinit
source <(kubectl completion zsh)
EOF
source {{profileFile}}

All the (bad) flavours come from the natural world.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Install the bash completion main script (assuming you're on a RHEL/CentOS/Fedora)
dnf install bash-completion
# Reload env
source ~/.bashrc
# Check if bash_completion is properly imported, or add it to your bashrc
if ! type _init_completion; then
    echo 'source /usr/share/bash-completion/bash_completion' >> {{profileFile}}
fi
# Source the completion script
echo 'source <(kubectl completion bash)' >> {{profileFile}}
source {{profileFile}}

kubecolor: prettier kubectl commands outputs with colors

1
2
3
go install github.com/hidetatz/kubecolor/cmd/kubecolor@latest
# Make sure kubecolor is found
which kubecolor

Finally, you could either use kubecolor instead of kubectl, or alias kubectl as kubecolor with the following code sample:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
cat <<EOF | tee -a {{profileFile}}
# Backup original "kubectl" command path. Supports subsequent imports of the file.
export KUBECTL_ORIG_PATH="\${KUBECTL_ORIG_PATH:-"\$(which kubectl)"}"
# Alias the real "kubectl" as "kubectll"
alias kubectll="\${KUBECTL_ORIG_PATH}"
# Alias kubectl to use colors by default
alias kubectl="kubecolor"
# Enable the autocompletion for the alias too (see auto-complete install above)
compdef kubecolor=kubectl
compdef kubectll=kubectl
EOF
source {{profileFile}}

helm: a kubernetes stack template repository

 Helm is a convinient way to use or share configurable kubernetes stacks. For example, it may allow to install easily a front-end, with its API and a database, in a single template, in which you can inject your specific configuration (PVC, ports, environment, etc…).

To install helm, run the following command:

1
2
# See https://helm.sh/docs/intro/install/
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash

krew: a kubectl plugins manager

 krew is a nice small plugin manager for your kubectl command. At the time of writing, it has  129 plugins available, including some pretty convinient to restart pods, login using OpenId, check the state of your cluster, and more.

To install krew, run the following: (taken from  the docs)

Think about replacing {{profileFile}} with your actual zsh or bash profile

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Install krew
(
  set -x; cd "$(mktemp -d)" &&
  OS="$(uname | tr '[:upper:]' '[:lower:]')" &&
  ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" &&
  KREW="krew-${OS}_${ARCH}" &&
  curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" &&
  tar zxvf "${KREW}.tar.gz" &&
  ./"${KREW}" install krew
)
# Add it to your $PATH and reload config
cat <<EOF | tee -a {{profileFile}}
export PATH="\${KREW_ROOT:-\$HOME/.krew}/bin:\$PATH"
EOF
source {{profileFile}}
# Check krew works
kubectl krew

One ring to rule them all

For this one, I plead guilty of not using it enough, but it contains a lot of useful knowledge and possible solutions of most of your problems.

You guessed it, I’m talking about documentation. (because it would be an insult to tell you that StackOverflow is a thing.)

Read it carefully. Take time to understand it and its underlying concepts. Don’t use tools you don’t know how they work. Because when things breaks, your knowledge of what and how it broke will help you to solve the problem quickly and without damages. So, read the documentation of your containers, your helm charts, your kubernetes network layer, and, of course, kubernetes and docker themselves.

Share on

GerkinDev
WRITTEN BY
GerkinDev
Fullstack developer, on its journey to DevOps.

 
What's on this Page