klg
klg copied to clipboard
Viewing logs of Kubernetes pods using lnav
klg
Viewing logs of Kubernetes pods using lnav. This is written in bash script.
Why this?
- I am sick of copying long auto-generated pod name before pasting into
kubectl logscommand. K9s can help here. - My eyes hurt when looking at the wall of log text in the terminal
- I want to filter/search the logs in a professional log viewer
- I want to play with shell script
DEMO

DEPENDENCIES
This tool depends on fzf, lnav and kubectl.
HOW TO USE
View pod logs with klg
View logs of your pod/container using lnav
Add this to your .bashrc or .zshrc
function klg () {
<Path-to-your-cloned-repo>/klg/klg.sh $@
}
Run shell command with ksh
Start /bin/sh inside your pod and let you run any shell command as you want.
Add this to your .bashrc or .zshrc
function ksh () {
<Path-to-your-cloned-repo>/klg/ksh.sh $@
}
Execute a shell command inside your pod with ke
Execute a shell command inside a k8s pod
Add this to your .bashrc or .zshrc
function ke () {
<Path-to-your-cloned-repo>/klg/ke.sh $@
}
Usage
klg [options]
ksh [options]
ke [options]
Available option:
-n: Keyword to fuzzy search for k8s namespace. Ignored when-Nis specified.-p: Keyword to fuzzy search for k8s pod. Ignored when-Pis specified.-c: Keyword to fuzzy search for container. Ignored when-Cis specified.-N: Exact namespace (no searching)-P: Exact pod name (no searching)-C: Exact container name (no searching)
If namespace options are not provided, $K8S_DEFAULT_NS will be used, by default, it is default.
If pod options are not provided, the user will be asked to select pod on a fuzzy search popup.
If container options are not provided for multi-container pods, the user will be asked to select container on a fuzzy search popup.
Example:
klg
klg -n my-nsp
klg -n my-namespace -p my-pod -c my-container
K8S_DEFAULT_NS=my-default-ns klg -p mypd -c myctn
ksh -p my-pod
ksh -n my-nsp -p my-pod -c my-container
ke -n my-nsp -p my-pod -- my-command the arg list
GOALS
Finish following features:
- [x] Fuzzy search for k8s namespace/pod/container
- [x] Viewing logs in lnav
- [x] Using default namespace
defaultor$K8S_DEFAULT_NS - [x] Handle error cases and return error code (e.g. pod not found, namespace not found)
- [x] Auto-select the only container in pod
ALTERNATIVE FOR FISH SHELL
If you are a fisher (as I am), please refer to this file.