마구잡

쿠버네티스 클러스터 플러그인 사용하기 본문

Kubernetes/작은팁-짧은글

쿠버네티스 클러스터 플러그인 사용하기

MAGUJOB 2024. 10. 29. 18:39
728x90

Kubectl 그리고 플러그인 

쿠버네티스 클러스터를 관리하기 위해 대시보드, 플러그인등 많은 써드파티 오픈소스들이 시장에 나와있다.

오늘 글에서는 클러스터를 관리하는 가장 기본적인 툴인 kubectl의 확장 플러그인에 대해 다뤄볼까 한다.

 

( 공식 사이트에서 발췌한 내용을 기반으로 작성하였으나, 명확하지 않은 부분은 경험을 토대로 작성하였습니다. 

이는 정확한 정보가 아닐 수 있음을 알려드립니다. )

공식 사이트

( 광고 클릭은 큰 힘이 됩니다! )

728x90
 

Krew – kubectl plugin manager

© 2024 The Kubernetes Authors. Krew is a Kubernetes SIG CLI project. Edit Page ·

krew.sigs.k8s.io

 


Krew란?

쿠버네티스 커맨드툴인 kubectl의 확장을 도와주는 플러그인 매니저


Krew 설치

전제조건: kubectl v1.12 이상이 필요하다.
전제조건: git 설치되어 있어야 한다.

 

1. 터미널 환경에서 하기 커맨드 실행

(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)

 

2. PATH 환경 변수에 추가

$HOME/.krew/bin 디렉터리를 환경 변수에 추가한다. • ~/.bashrc 파일에 다음 줄 추가:

export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"

 

3.  리로드 후 확인

kubectl krew 명령으로 설치를 확인한다.

kubectl 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.
쉘을 리로드 할때 꼭 로그아웃/로그인 절차를 밟을 필요는 없다.
exec bash 커맨드 자신이 로그인 한 쉘을 치환 하면 변수값이 로딩된다.

대표 플러그인 사용해보기

kubectl을 좀 더 편하게 사용가능한 대표 플러그인 2개정도를 설치해본다.

1. kube-ctx: 컨텍스트를 쉽게 변경 가능하게 해주는 플러그인
2. kube-ns: 네임스페이스를 쉽게 변경 가능하게 해주는 플러그인

번외 설치: kube-ps1: 현재 상태를 터미널상 보기 편하게 해주는 터미널 플러그인

kube-ctx

1. 터미널 환경에서 플러그인 검색

root@ubuntu-m1:~# kubectl krew search |grep ctx
allctx                          Run commands on contexts in your kubeconfig         no
ctx                             Switch between contexts in your kubeconfig          no
shell-ctx                       Shell independent context switching                 no

 

2. kube-ctx 설치

root@ubuntu-m1:~# 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.

 

3. krew ctx 확인 및 변경

root@ubuntu-m1:~# kubectl ctx
kubernetes-admin-2@kubernetes-2
kubernetes-admin@kubernetes

노란색 마크가 현재 사용중인 config
root@ubuntu-m1:~# kubectl ctx  kubernetes-admin-2@kubernetes-2
Switched to context "kubernetes-admin-2@kubernetes-2".
root@ubuntu-m1:~# kubectl get node
NAME          STATUS   ROLES           AGE    VERSION
ubuntu-2-m1   Ready    control-plane   7d8h   v1.31.1
ubuntu-2-w1   Ready    <none>          7d7h   v1.31.1
ubuntu-2-w2   Ready    <none>          7d7h   v1.31.1
다른 클러스터의 노드 확인

kube-ns

1. kube-ns 설치 

root@ubuntu-m1:~# kubectl krew install ns

Updated the local copy of plugin index.
Installing plugin: ns
Installed plugin: ns
\
 | Use this plugin:
 |      kubectl ns
 | 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.
 | /
/
WARNING: You installed plugin "ns" from the krew-index plugin repository.
   These plugins are not audited for security by the Krew maintainers.
   Run them at your own risk.

 

2. kube-ns 확인 및 변경

 

root@ubuntu-m1:~# kubectl ns 
default
kube-node-lease
kube-public
kube-system
root@ubuntu-m1:~# kubectl ns kube-system
Context "kubernetes-admin-2@kubernetes-2" modified.
Active namespace is "kube-system".
root@ubuntu-m1:~# kubectl get po
NAME                                       READY   STATUS    RESTARTS   AGE
calico-kube-controllers-695bcfd99c-rhjzn   1/1     Running   0          7d8h
calico-node-4p452                          1/1     Running   0          7d8h
calico-node-7lqls                          1/1     Running   1          7d8h
calico-node-8j8mw                          1/1     Running   0          7d8h
coredns-7c65d6cfc9-fgnwk                   1/1     Running   0          7d8h
coredns-7c65d6cfc9-xxtc9                   1/1     Running   0          7d8h
etcd-ubuntu-2-m1                           1/1     Running   2          7d8h
kube-apiserver-ubuntu-2-m1                 1/1     Running   0          7d8h
kube-controller-manager-ubuntu-2-m1        1/1     Running   1          7d8h
kube-proxy-gf4lk                           1/1     Running   1          7d8h
kube-proxy-sspzd                           1/1     Running   0          7d8h
kube-proxy-v4swb                           1/1     Running   0          7d8h
kube-scheduler-ubuntu-2-m1                 1/1     Running   1          7d8h

네임스페이스 옵션을 붙이지 않아도 kube-system의 POD를 조회

kube-ps

1. kube-ps 설치 

root@ubuntu-m1:~# git clone https://github.com/jonmosco/kube-ps1.git

Cloning into 'kube-ps1'...
remote: Enumerating objects: 760, done.
remote: Counting objects: 100% (195/195), done.
remote: Compressing objects: 100% (108/108), done.
remote: Total 760 (delta 107), reused 165 (delta 83), pack-reused 565 (from 1)
Receiving objects: 100% (760/760), 7.72 MiB | 13.63 MiB/s, done.
Resolving deltas: 100% (401/401), done.

 

2. kube-ps1 디렉터리에서 진행

root@ubuntu-m1:~# cd kube-ps1/
root@ubuntu-m1:~/kube-ps1#

root@ubuntu-m1:~/kube-ps1# chmod +x ./kube-ps1.sh
root@ubuntu-m1:~/kube-ps1# cp kube-ps1.sh /usr/bin/
root@ubuntu-m1:~/kube-ps1# vi ~/.bashrc
... 가장 하단에 삽입
source /usr/bin/kube-ps1.sh
PS1='[\u@\h \W $(kube_ps1)]\$ '
root@ubuntu-m1:~/kube-ps1# exec bash

[root@ubuntu-m1 kube-ps1 (⎈|kubernetes-admin-2@kubernetes-2:kube-system)]#

현재 context와 네임스페이스를 특별한 조회 없이 터미널 환경에서 확인 할 수 있다.

 

마무리

위처럼 3가지 플러그인을 사용하여 보다 나은 커맨드 환경 제공이 가능하며

현재 Context와 네임스페이스를 가시적으로 확인하여 휴면 에러를 줄일수 있는 환경을 구성할 수 있다.

 

잘못된 정보나, 문의등은 댓글로 메일과 함께 적어주시면 감사하겠습니다.

728x90