k8s tools for watching log and login to multiple containers

k8s 사용하면서 개인 취향에 맞게 작성한 자작 스크립트 몇가지 소개해 드리려고 합니다.

watching log

k8s 에서 pod들의 로그 볼일 들이 많다보니 command 일일치다보니 불편했는데 kubetail이란 프로젝트가 있었습니다.
덕분에 잘 쓰고 있었는데 인터페이스가 개인취향에 안맞아서 약간 수정해서 쓰고 있습니다.
전반적으로 pod 선택 방법의 변경과 비슷한 이름으로 auto tailing 기능을 추가 했습니다.

아래들이 대표적인 기능 입니다.

준비물은 아래와 같습니다.

설치 방법은 아래와 같습니다.

$ brew tap leoh0/kt && brew install kt

code는 아래에서 확인 가능합니다.

https://github.com/leoh0/kt

login container

pod들 여러개에 동시에 login(bash, sh등) 하여 shell command를 사용하고 싶어서 기존의 cssh 같은 비슷한 메커니즘으로 스크립트 작성해서 사용하고 있습니다.

설치 방법은 아래와 같이 rc나 profile에 등록해서 사용하시면 됩니다.

curl -s 'https://gist.githubusercontent.com/leoh0/'\
'c47dca1c98f998f0d0884c3560afac54/raw/'\
'1e1e9fb085d2c1a94293ae87e3922519d8342adb/k8s_login.sh' | \
    tee -a ~/.bash_profile && source ~/.bash_profile

아래는 전체 소스입니다. https://gist.github.com/leoh0/c47dca1c98f998f0d0884c3560afac54

function kl() {
  chkcommand() {
    command -v $1 >/dev/null 2>&1 || { echo >&2 "Plz install $1 first. Aborting."; return 1; }
  }
  chkcommand fzf || return 1
  chkcommand tmux || return 1
  chkcommand kubectl || return 1

  pods=$(kubectl get pods --all-namespaces | sed '1d' | fzf -x -m -e +s --reverse --bind=left:page-up,right:page-down --no-mouse | awk '{print $1","$2}');
  if [[ $pods != "" ]]; then
      init="true"
      tmuxname=k8s-ns-$(date +%s)
      tmux kill-session -t $tmuxname > /dev/null 2> /dev/null || true
      while read line ;do
        NS=$(echo $line | cut -d',' -f1)
        NAME=$(echo $line | cut -d',' -f2)
        connect="kubectl exec -ti $NAME -n $NS -- bash || kubectl exec -ti $NAME -n $NS -- sh "
        tmux new-session -d -s $tmuxname "export KUBECONFIG=${KUBECONFIG}; $connect"
        if [ "${init}" == "true" ]; then
          if (( $(tmux ls 2> /dev/null | grep "${tmuxname}" | wc -l) > 0 )) ; then
            init="false"
          fi
        else
          tmux split-window -t "${tmuxname}" "${connect}" && \
          tmux select-layout -t "${tmuxname}" "tiled"
        fi
      done <<< "$pods"
      tmux set-window-option -t "${tmuxname}" synchronize-panes on
      tmux -2 a -t $tmuxname
  fi
}

참고: original kubetail

comments powered by Disqus