본문 바로가기

Kubernetes

kubectl autocomplete 설정하기

kubectl autocompletion 기능은 bash 버전이 4.1 버전 이상부터 지원된다.


macOS bash 버전을 보니 너무 낮기 때문에 먼저 brew install bash 를 통해 5.0.16(1) 버전으로 업데이트를 해보겠습니다.

developui-MacBook-Pro:~ develop$ which -a bash #bash 위치 확인
/usr/local/bin/bash
/bin/bash
developui-MacBook-Pro:~ develop$ /usr/local/bin/bash --version # 설치된 bash version 확인
GNU bash, version 5.0.16(1)-release (x86_64-apple-darwin19.3.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
developui-MacBook-Pro:~ develop$ /bin/bash --version # 기존 bash version 확인
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)
Copyright (C) 2007 Free Software Foundation, Inc.

developui-MacBook-Pro:~ develop$ sudo vim /etc/shells
Password:
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
/usr/local/bin/bash # 추가하기
developui-MacBook-Pro:~ develop$ chsh -s /usr/local/bin/bash # 기본 default bash 지정
Changing shell for develop.
Password for develop: 


developui-MacBook-Pro:~ develop$ echo $BASH_VERSION # bash version 확인
5.0.16(1)-release

brew를 이용하여 bash-completion 설치 brew install bash-completion@2
developui-MacBook-Pro:~ develop$ brew install bash-completion@2

kubectl autocomplete 도움말 보기

developui-MacBook-Pro:~ develop$ kubectl completion -h 
Output shell completion code for the specified shell (bash or zsh). The shell
code must be evaluated to provide interactive completion of kubectl commands.
This can be done by sourcing it from the .bash_profile.

 Detailed instructions on how to do this are available here:
https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion

 Note for zsh users: [1] zsh completions are only supported in versions of zsh
>= 5.2

Examples:
  # Installing bash completion on macOS using homebrew
  ## If running Bash 3.2 included with macOS
  brew install bash-completion
  ## or, if running Bash 4.1+
  brew install bash-completion@2
  ## If kubectl is installed via homebrew, this should start working
immediately.
  ## If you've installed via other means, you may need add the completion to
your completion directory
  kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl


  # Installing bash completion on Linux
  ## If bash-completion is not installed on Linux, please install the
'bash-completion' package
  ## via your distribution's package manager.
  ## Load the kubectl completion code for bash into the current shell
  source <(kubectl completion bash)
  ## Write bash completion code to a file and source if from .bash_profile
  kubectl completion bash > ~/.kube/completion.bash.inc
  printf "
  # Kubectl shell completion
  source '$HOME/.kube/completion.bash.inc'
  " >> $HOME/.bash_profile
  source $HOME/.bash_profile

  # Load the kubectl completion code for zsh[1] into the current shell
  source <(kubectl completion zsh)
  # Set the kubectl completion code for zsh[1] to autoload on startup
  kubectl completion zsh > "${fpath[1]}/_kubectl"

Usage:
  kubectl completion SHELL [options]

Use "kubectl options" for a list of global command-line options (applies to all
commands).

developui-MacBook-Pro:~ develop$ kubectl completion bash > ~/.kube/kubectl_autocompletion

developui-MacBook-Pro:~ develop$ vi ~/.bash_profile

아래 내용 제일 밑에 추가
if [ -f /usr/local/share/bash-completion/bash_completion ]; then
  . /usr/local/share/bash-completion/bash_completion
fi

source ~/.kube/kubectl_autocompletion

shell을 다시 시작 하면 kubectl 명령어에서 tab을 사용가능 하다

'Kubernetes' 카테고리의 다른 글

쿠버네티스 기초 YAML 파일 작성 하기  (0) 2020.03.22
쿠버네티스 기초 Pods  (0) 2020.03.20
Minikube tutorial  (0) 2020.03.17
Kubectl 설치(mac os)  (0) 2020.03.17
Minikube 설치하기(Mac OS)  (0) 2020.03.17