1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

fzf + kubectlでデバッグ時に便利なalias

Posted at

kubectlを使ったデバッグ時に便利なaliasをいくつか紹介します。
最近はk9sを使うことも多くなってきましたが、使えない環境ではこちらのaliasも便利です。

ちなみに以前、GKEの快適なオペレーション という記事も書いたので参考にしてみてください。

fkl (fzf + kubectl logs): 選択したpodのlogを表示

履歴に残すために直接実行せずにprint -zでコマンドを表示してます。


function _fzf_kubectl_logs() {
  local selection=`kubectl get pods --all-namespaces -o wide | fzf --header-lines=1 --query="$*" --select-1 -e `
  if [ $selection == "" ]; then
    return 0
  fi
  local namespace=`echo $selection | awk '{ print $1 }'`
  local pod=`echo $selection | awk '{ print $2 }'`
  local containers=`kubectl -n $namespace get pods $pod -o jsonpath='{range .spec.containers[*]}{@.name}{"\n"}{end}'`
  local container_count=$((`echo "$containers" | wc -l`))

  local container
  if [ ${container_count} -gt "1" ]; then
    container=`echo "$containers" | fzf --header "Select a container..."`
  else
    container=$containers
  fi

  if [ $container == "" ]; then
    return 0
  fi

  print -z "kubectl logs -n ${namespace} ${pod} -c ${container}"
}
alias fkl=_fzf_kubectl_logs

fkeit (fzf + kubectl exec -it): 選択したpodに任意のコマンドを実行

こちらも、履歴に残すために直接実行せずにprint -zでコマンドを表示してます。

function _fzf_kubectl_exec_it() {
  local selection=`kubectl get pods --all-namespaces | fzf --header-lines=1 --query="$*" --select-1 -e `
  if [ $selection == "" ]; then
    return 0
  fi

  local namespace=`echo $selection | awk '{ print $1 }'`
  local pod=`echo $selection | awk '{ print $2 }'`
  local containers=`kubectl -n $namespace get pods $pod -o jsonpath='{range .spec.containers[*]}{@.name}{"\n"}{end}'`
  if [ $containers == "" ]; then
    return 0
  fi
  
  local container_count=$((`echo "$containers" | wc -l`))
  if [ ${container_count} -gt "1" ]; then
    container=`echo "$containers" | fzf --header "Select a container..."`
  else
    container=$containers
  fi

  if [ $containers == "" ]; then
    return 0
  fi

  print -z "kubectl exec -n ${namespace} -it ${pod} -c ${container} "
}
alias fkeit=_fzf_kubectl_exec_it

fkeitsh (fzf + kubectl exec -it sh): 選択したpodのshellを実行

shellの判別は出来ないので、ちょっと無理矢理です。
参考: https://gist.github.com/jondlm/35cbf0363eb925e2eff6ff86c0a30992


function _fzf_kubectl_exec_it_sh() {
  local selection=`kubectl get pods --all-namespaces | fzf --header-lines=1 --query="$*" --select-1 -e `
  if [ $selection == "" ]; then
    return 0
  fi
  local namespace=`echo $selection | awk '{ print $1 }'`
  local pod=`echo $selection | awk '{ print $2 }'`
  local containers=`kubectl -n $namespace get pods $pod -o jsonpath='{range .spec.containers[*]}{@.name}{"\n"}{end}'`
  if [ $containers == "" ]; then
    return 0
  fi
  local container_count=$((`echo "$containers" | wc -l`))
  
  if [ ${container_count} -gt "1" ]; then
    container=`echo "$containers" | fzf --header "Select a container..."`
  else
    container=$containers
  fi
  
  kubectl exec -n $namespace -it $pod -c $container ash ||\
  kubectl exec -n $namespace -it $pod -c $container bash ||\
  kubectl exec -n $namespace -it $pod -c $container sh
}

alias fkeitsh=_fzf_kubectl_exec_it_sh
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?