
쿠버네티스를 사용할 때 반드시 빼놓을 수 없는 것이 kubectl이라는 유틸리티다. 클라이언트가 쿠버네티스 클러스터 컨트롤 플레인의 kube-apiserver에 요청을 보낼 수 있게 해주는 유틸리티고 간단히 말해 클러스터의 상태를 파악하거나 리소스를 배포/수정/삭제 하는데에 사용하는 CLI도구라고 생각하면 된다.
kubectl 설치(MacOS)
1. kubectl을 다운로드 받기 위해서는 쿠버네티스 공식문서 혹은 AWS Documentation을 참고하면 된다. 먼저 아래 명령어를 실행해 kubectl 바이너리를 다운로드 받는다(쿠버네티스 1.31 버전).
curl -O <https://s3.us-west-2.amazonaws.com/amazon-eks/1.31.4/2025-01-10/bin/darwin/amd64/kubectl>
2. 다운로드 받은 바이너리에 실행 권한을 추가한다.
chmod +x ./kubectl
3. 바이너리를 PATH 폴더에 복사한다.
mkdir -p $HOME/bin && cp ./kubectl $HOME/bin/kubectl && export PATH=$HOME/bin:$PATH
4. 셸 초기화 파일에 $HOME/bin 경로를 추가한다.
[bash 사용자의 경우]
echo 'export PATH=$HOME/bin:$PATH' >> ~/.bash_profile
[zsh 사용자의 경우]
echo 'export PATH=$HOME/bin:$PATH' >> ~/.zshrc
5. 현재 쉘에서 전달된 파일의 내용을 실행해 시스템 환경변수로 등록할 수 있도록 source 명령어를 실행한다.
[bash 사용자의 경우]
source ~/.bash_profile
[zsh 사용자의 경우]
source >> ~/.zshrc
아래 명령어를 실행해 잘 설치되었는지 확인한다.
$ kubectl --help
kubectl controls the Kubernetes cluster manager.
Find more information at: <https://kubernetes.io/docs/reference/kubectl/>
Basic Commands (Beginner):
create Create a resource from a file or from stdin
expose Take a replication controller, service, deployment or pod and
expose it as a new Kubernetes service
run Run a particular image on the cluster
set Set specific features on objects
Basic Commands (Intermediate):
explain Get documentation for a resource
get Display one or many resources
edit Edit a resource on the server
delete Delete resources by file names, stdin, resources and names, or
by resources and label selector
(이하 생략)
kubectl 기본 설정(alias, 자동완성)
아래 작성한 kubectl 유틸리티는 필요에 따라 설치해서 사용하면 되지만 alias와 자동완성은 꼭 구성해서 사용하는 걸 추천한다.
kubectl 자동완성 설정
[bash 사용자의 경우]
source <(kubectl completion bash)
echo 'source <(kubectl completion bash)' >>~/.bashrc
[zsh 사용자의 경우]
source <(kubectl completion zsh)
echo 'source <(kubectl completion zsh)' >>~/.zshrc
위 명령어를 실행하고 나면 kubectl + tab 키 입력 시 추천 명령어 목록을 확인할 수 있다.
alias k 설정
이 설정을 적용하면 kubect 명령어를 k로 단축해줘 명령어를 칠 때 번거로움을 많이 줄여준다.
[bash 사용자의 경우]
echo 'alias k=kubectl' >>~/.bashrc
echo 'complete -o default -F __start_kubectl k' >>~/.bashrc
[zsh 사용자의 경우]
echo 'alias k=kubectl' >>~/.zshrc
echo 'complete -o default -F __start_kubectl k' >>~/.zshrc
kubectl 유틸리티
krew 설치
krew는 각종 kubectl 유틸리티를 공유하는 패키지 매니저고 kubectl krew 명령어를 실행해 간편하게 다양한 kubectl 유틸리티를 다운로드 받을 수 있다.
krew를 설치하려면 공식 홈페이지에 있는 스크립트를 실행하면 된다(prerequisite으로 git 설치가 필요하다).
(
set -x; cd "$(mktemp -d)" &&
OS="$(uname | tr '[:upper:]' '[:lower:]')" &&
ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\\(arm\\)\\(64\\)\\?.*/\\1\\2/' -e 's/aarch64$/arm64/')" &&
KREW="krew-${OS}_${ARCH}" &&
curl -fsSLO "<https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz>" &&
tar zxvf "${KREW}.tar.gz" &&
./"${KREW}" install krew
)
krew 바이너리 경로를 환경변수로 설정한다.
[bash 사용자의 경우]
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
source ~/.bash_profile
[zsh 사용자의 경우]
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
source >> ~/.zshrc
kubectl krew 명령어를 실행하면 krew를 활용할 수 있다. 위에서 alias를 설정했다면 아래처럼 k krew와 같이 명령어를 축약해 실행할 수 있다.
$ k krew
krew is the kubectl plugin manager.
You can invoke krew through kubectl: "kubectl krew [command]..."
Usage:
kubectl krew [command]
Available Commands:
help Help about any command
index Manage custom plugin indexes
info Show information about an available plugin
install Install kubectl plugins
list List installed kubectl plugins
search Discover kubectl plugins
uninstall Uninstall plugins
update Update the local copy of the plugin index
upgrade Upgrade installed plugins to newer versions
version Show krew version and diagnostics
Flags:
-h, --help help for krew
-v, --v Level number for the log level verbosity
Use "kubectl krew [command] --help" for more information about a command.
ctx 설치
ctx 플러그인로는 context를 조회, 변경할 수 있다. 여러 클러스터를 운영할 때 편리한 유틸리티이기 때문에 자주 활용하는 유틸리티이다.
ctx는 krew로 설치할 수 있다. 아래 명령어를 실행해 설치하면 된다.
$ kubectl krew install ctx
Updated the local copy of plugin index.
Installing plugin: ctx
Installed plugin: ctx
\\
| Use this plugin:
| kubectl ctx
| Documentation:
| <https://github.com/ahmetb/kubectx>
| Caveats:
| \\
| | If fzf is installed on your machine, you can interactively choose
| | between the entries using the arrow keys, or by fuzzy searching
| | as you type.
| | See <https://github.com/ahmetb/kubectx> for customization and details.
| /
/
WARNING: You installed plugin "ctx" from the krew-index plugin repository.
These plugins are not audited for security by the Krew maintainers.
Run them at your own risk.
WARNING이 뜨지만 krew쪽에서 보안 감사를 하지 않으니 알아서 조심하라는 뜻이라서 크게 문제가 있는 건 아니다.
kubectl ctx 명령어를 실행하면 여러 클러스터가 표시되고 이 중 하나를 선택해 컨텍스트를 변경할 수 있다.
$ k ctx
arn:aws:eks:ap-northeast-2:xxxxxxxxxxxx:cluster/eks-cluster1
arn:aws:eks:ap-northeast-2:xxxxxxxxxxxx:cluster/eks-cluster2
neat 설치
neat도 마찬가지로 krew로 설치할 수 있다. kubectl -oyaml 로 YAML파일을 출력할 때 각종 메타데이터를 필터링해서 보여주기 때문에 클러스터상의 YAML을 확인하거나 기존 YAML파일을 기반으로 새로운 매니페스트를 생성할때도 편리하다. 아래 명령어로 설치할 수 있다.
$ kubectl krew install neat
Updated the local copy of plugin index.
Installing plugin: neat
Installed plugin: neat
\\
| Use this plugin:
| kubectl neat
| Documentation:
| <https://github.com/itaysk/kubectl-neat>
/
WARNING: You installed plugin "neat" from the krew-index plugin repository.
These plugins are not audited for security by the Krew maintainers.
Run them at your own risk.
'DevOps' 카테고리의 다른 글
[Github]깃헙 액세스토큰 사용 시 refusing to allow a Personal Access Token to create or update workflow… 에러 (0) | 2025.04.01 |
---|---|
[RSS]SaaS 서비스 장애 상황 및 AWS 서비스 신규소식 Slack으로 받아보기 (0) | 2025.03.29 |
[Terraform]tfenv로 테라폼 버전 관리하기(테라폼 여러 버전 활용하기) (0) | 2025.03.20 |
[MacOS]SSH config로 간편하게 EC2에 SSH 연결하기 (0) | 2025.03.20 |
[AWS]Bedrock Inference Profile(추론 프로필)로 비용 분리해서 집계하기 (0) | 2025.03.19 |