/klg

Viewing logs of Kubernetes pods using lnav

Primary LanguageShellGNU General Public License v3.0GPL-3.0

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 logs command. 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

klg

DEPENDENCIES

This tool depends on fzf, lnav and kubectl.

  • Please make sure that you installed lnav
  • Your kubectl must work
  • Please install fzf for fuzzy search

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 -N is specified.
  • -p: Keyword to fuzzy search for k8s pod. Ignored when -P is specified.
  • -c: Keyword to fuzzy search for container. Ignored when -C is 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:

  • Fuzzy search for k8s namespace/pod/container
  • Viewing logs in lnav
  • Using default namespace default or $K8S_DEFAULT_NS
  • Handle error cases and return error code (e.g. pod not found, namespace not found)
  • Auto-select the only container in pod

ALTERNATIVE FOR FISH SHELL

If you are a fisher (as I am), please refer to this file.