더듬이

mac m1에서 kubectl 자동완성 설정하기 (feat. m1은 bash path가 달라요~ ) 본문

Kubernetes

mac m1에서 kubectl 자동완성 설정하기 (feat. m1은 bash path가 달라요~ )

dbhang 2022. 4. 18. 19:40

 

이전 맥버전에서는 bash 를 새로 설치하면 /usr/local/bin/bash 에 깔렸는데 빅서에서는 /opt/homebrew/bin/bash 에깔렷다. 근데 kubernets 공식문서에서는

/usr/local/bin을 prefix로 생각하기 때문에 기존꺼 그대로 따라하면 적용안되는 경우가 좀 있다 ㅎ...

역시 무작정 명령어 따라치지말고 설치 path확인 설치로그를 꼼꼼히 보자~ 

1. Homebrew 설치

 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

위 명령어로 brew 설치를 하고 나면 아래와 같은 로그가 표기된다. (언제나 install이든 뭐든 로그는 한번씩 보는 습관을 들이면 좋은것 같다.)

==> Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
    echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/whatiy/.bash_profile
    eval "$(/opt/homebrew/bin/brew shellenv)"
- Run brew help to get started

위에서 명령어 내리라는거 두개를 치고 brew help 결과가 표기되면 성공이다.

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/whatiy/.bash_profile
eval "$(/opt/homebrew/bin/brew shellenv)"
brew help

2.bash 버전 업데이트

이제 bash를 건들여보자 .

 

기본적으로 mac에깔려있는 bash 버전은 3점대이다 . 

(base) whatiy@iyui-MacBookAir ~ % /bin/bash --version
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin20)
Copyright (C) 2007 Free Software Foundation, Inc.

자동완성이 하고 싶으면  일단 bash를 4.1이상으로 업데이트 하자. 이미 4.1이상인 분은 그냥 3번으로 가세욤 

brew install bash 명령어를 이용하면 자동으로 4.1보다 높은 버전으로 새로 깔린다.

 

만약 아래와 같이 isntall error가 발생하면  아래 펼치기 내용을 따라하라.

brew install bash
Error:
  homebrew-core is a shallow clone.
  homebrew-cask is a shallow clone.
To `brew update`, first run:
  git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
  git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask fetch --unshallow
This restriction has been made on GitHub's request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you
automatically to avoid repeatedly performing an expensive unshallow operation in
CI systems (which should instead be fixed to not use shallow clones). Sorry for
the inconvenience!
더보기
git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow

git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask fetch --unshallow

brew update

을 쳐서

...
==> Processing wxmac formula rename to wxwidgets
==> Unlinking wxmac
==> Moving wxmac versions to /usr/local/Cellar/wxwidgets
==> Relinking wxwidgets
Warning: wxwidgets is outdated!
To avoid broken installations, as soon as possible please run:
  brew upgrade
Or, if you're OK with a less reliable fix:
  brew upgrade wxwidgets

You have 58 outdated formulae installed.
You can upgrade them with brew upgrade
or list them with brew outdated.

 가 나오면 성공이다. 

brew install bash

휴 이제 설치했다. 이제 설치된 bash를 shell에 등록해보자

먼저 설치를 진행한 터미널에서 wich bash를 통해서 현재 어디 위치에 bash 실행파일이 있는지 알아야한다. (아직 다른 터미널X )

(base) whatiy@iyui-MacBookAir ~ % which bash
/opt/homebrew/bin/bash

이전 맥버전에서는 /usr/local/bin/bash 에 깔렸는데 빅서에서는 /opt/homebrew/bin/bash 에깔렷다. 이제 /etc/shells 파일에 새로운 bash shell을 등록해준다.  

 

sudo su
echo "/opt/homebrew/bin/bash" >> /etc/shells

그리고 default 쉘을 변경해준다.

chsh -s /opt/homebrew/bin/bash

 

3.  kubectl 자동완성 등록하기

brew install bash-completion@2
export BASH_COMPLETION_COMPAT_DIR="/opt/homebrew/etc/bash_completion.d"
kubectl completion bash >/opt/homebrew/etc/bash_completion.d/kubectl
echo 'export BASH_COMPLETION_COMPAT_DIR="/opt/homebrew/etc/bash_completion.d"'
echo 'alias k=kubectl' >>~/.bash_profile
echo 'complete -F __start_kubectl k' >>~/.bash_profile

이전 mac버전에서는 아래와 같이 사용했었다.

[[ -r "/opt/homebrew/etc/profile.d/bash_completion.sh" ]] && . "/opt/homebrew/etc/profile.d/bash_completion.sh" source <(kubectl completion bash) alias k=kubectl complete -F __start_kubectl k

더보기
brew install bash-completion@2
kubectl completion bash >/usr/local/etc/bash_completion.d/kubectl


echo '[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"' >>~/.bash_profile 
echo 'source <(kubectl completion bash)' >>~/.bash_profile
echo 'alias k=kubectl' >>~/.bash_profile
echo 'complete -F __start_kubectl k' >>~/.bash_profile

 

 

쨘 이제 진짜 된다~ 아래처럼 kubectl object가 뜨고 하면 성공~ 

(base) iyui-MacBookAir:~ whatiy$ k get p
persistentvolumeclaims
persistentvolumes
poddisruptionbudgets.policy
pods
podsecuritypolicies.policy
podtemplates
priorityclasses.scheduling.k8s.io
prioritylevelconfigurations.flowcontrol.apiserver.k8s.io
(base) iyui-MacBookAir:~ whatiy$ k get p
persistentvolumeclaims
persistentvolumes
poddisruptionbudgets.policy
pods
podsecuritypolicies.policy
podtemplates
priorityclasses.scheduling.k8s.io
prioritylevelconfigurations.flowcontrol.apiserver.k8s.io
(base) iyui-MacBookAir:~ whatiy$ k get p
persistentvolumeclaims
persistentvolumes
poddisruptionbudgets.policy
pods
podsecuritypolicies.policy
podtemplates
priorityclasses.scheduling.k8s.io
prioritylevelconfigurations.flowcontrol.apiserver.k8s.io

m1은 homebrew path가 달라서 기존 블로그 글로는 안됐던 사람들이 있을거 같아서 정리해봤다. 

참고는 언제나 공식 문서

https://kubernetes.io/ko/docs/tasks/tools/included/optional-kubectl-configs-bash-mac/