Why So Serious : Setting up a Serious Kubernetes Terminal
CLI tools a growing K8S team needs .
The blog covers tools that are used widely within K8S community .
Kubernetes comes pre-packaged with an outstanding CLI. For basic operations, it works wonderfully. But, when one needs to do something quickly, co mplexity increases.
Having a fully kitted terminal will rapidly speed up the time it takes to get to the root of an issue. It forms a fundamental part of your Swiss army knife to manage and administer the Kubernetes cluster.
ZSH
ZSH is an outstanding, open source wrapper around the standard OSX terminal. It is more feature rich and intuitive and the plugins you can install are fantastic. Some of these tools listed make the assumption you have ZSH installed.
ZSH Link :- https://ohmyz.sh/
K9S
K9s provides a curses based terminal UI to interact with your Kubernetes clusters. K9S makes it easier to navigate, observe and manage your applications in the wild. K9s continually watches Kubernetes for changes and offers subsequent commands to interact with observed resources.
You can SSH straight into pods with a single key press, view logs, delete resources , describe pods. K9S provides outstanding access for the most common operations you’ll be performing. This is a staple for any engineer using kubernetes.
K9S Link:- https://github.com/derailed/k9s
Kubectx and Kubens
Since it very rare that one will have one single cluster to work upon . Switching between these is as simple as
kubectl config use-context my-context
But with this, there are some prerequisites:
- You need to know the name of the cluster before you run.
- There is another, similar
set-context
command that could trip you up.
kubectx
presents a simpler alternative to this. If you run kubectx
on its own, it will list out all of the contexts in your .kube/config
file. You can then provide the name of the context you’re interested in:
kubectx my-context
No need to remember all the contexts, no need to manually check files and no possibility of getting the wrong command. Nice and simple. Combined with k9s
, this offers a lot of navigability from your CLI with minimal key presses.
The following image shows a good depiction of how kubectx works
Once you’re flitting around contexts, you may want to dig into a specific namespace. Once again, it’s very common to have more than a few namespaces in your cluster. for that we have another tool ( from the same repo )kubens
. It’s the same as kubectx
, only for namespaces.
kubens kube-system
Now all of your commands run against the kube-system
namespace, by default. You can also run kubens
without anything else to see a list of your namespaces.
The following image shows a good depiction of how kubens works
kubectx and Kubens Link :- https://github.com/ahmetb/kubectx
Kubeps1
So, you can switch between contexts and namespaces. But how do you know which one you’re currently aimed at? It’s a pain to keep checking. At the moment, to find out you’d need to run:
kubens
kubectx
kubectl <my-command>
To remove this, ps1
is a zsh plugin that will automatically show you your current context and namespace:
Now you can see which namespace and context you’re pointing out without running a single command. It’s also highly configurable too — you can turn off namespace or context, if you’re only interested in one of them, or you can use kubeoff
to disable the whole thing entirely.
The following image shows how kubeps1 works
Kubeps1 Link :- https://github.com/jonmosco/kube-ps1
Kubetail
Bash script that enables you to aggregate (tail/follow) logs from multiple pods into one stream. This is the same as running “kubectl logs -f “ but for multiple pods.
First find the names of all your pods:
$ kubectl get pods
This will return a list looking something like this:
NAME READY STATUS RESTARTS AGE
app1-v1-aba8y 1/1 Running 0 1d
app1-v1-gc4st 1/1 Running 0 1d
app1-v1-m8acl 1/1 Running 0 6d
app1-v1-s20d0 1/1 Running 0 1d
app2-v31-9pbpn 1/1 Running 0 1d
app2-v31-q74wg 1/1 Running 0 1d
my-demo-v5-0fa8o 1/1 Running 0 3h
my-demo-v5-yhren 1/1 Running 0 2h
To tail the logs of the two “app2” pods in one go simply do:
$ kubetail app2
To tail only a specific container from multiple pods specify the container like this:
$ kubetail app2 -c container1
You can repeat -c
to tail multiple specific containers:
$ kubetail app2 -c container1 -c container2
To tail multiple applications at the same time seperate them by comma:
$ kubetail app1,app2
For advanced matching you can use regular expressions:
$ kubetail "^app1|.*my-demo.*" --regex
Kubetail Link :- https://github.com/johanhaleby/kubetail
Popeye
Popeye is a utility that scans live Kubernetes cluster and reports potential issues with deployed resources and configurations. It sanitizes your cluster based on what’s deployed and not what’s sitting on disk. By scanning your cluster, it detects misconfigurations and ensure best practices are in place thus preventing potential future headaches. It aims at reducing the cognitive overload one faces when operating a Kubernetes cluster in the wild.
Popeye is a readonly tool, it does not alter any of your Kubernetes resources in any way!.
The following screenshots will help you understand what popeye is capable of
Popeye Link :- https://github.com/derailed/popeye