3개의 krew 플러그인에 대해서 알아봅시다.

  • kube-ctx 플러그인: 컨텍스트
  • kube-ns 플러그인: 네임스페이스
  • kube-ps1 플러그인: 프롬프트

--

1) kube-ctx 컨텍스트

실제 환경에서는 dev/stage/product 또는 alpha/beta/real 등으로
각 단계별로 클러스터를 분리해서 운영하거나 다양한 리전을 동시에 사용하는 것이 일반적입니다.

 

따라서 이럴 때 혼동이 올 수 있기 때문에 플러그인을 사용하면 유용합니다.

 

kube-ctx 플러그인: 컨텍스트. 내가 원하는 클러스터를 선택 가능

# 설치
kubectl krew install ctx

# 결과
node1:~$ k ctx
kubernetes-admin@cluster.local

로컬호스트에 등록한 클러스터 이름이 보입니다.

node1:~$ k ctx kubernetes-admin@cluster.local
Switched to context "kubernetes-admin@cluster.local".

k ctx {cluster-name} 으로 클러스터를 변경 가능합니다.

 

--

2) kube-ns 네임스페이스

kube-ns 플러그인: 네임스페이스.

# 설치
kubectl krew install ns

# 결과
node1:~$ k ns
default
kube-node-lease
kube-public
kube-system

현재 클러스터의 전체 네임스페이스 현황이 출력됩니다.

#현재 네임스페이스의 pod 조회
node1:~$ k get pod
No resources found in default namespace

# kube-system으로 네임스페이스 변경
node1:~$ k ns kube-system
Context "kubernetes-admin@cluster.local" modified.
Active namespace is "kube-system".

# kube-system 네임스페이스에서 pod 조회
node1:~$ k get pod
NAME                                      READY   STATUS        RESTARTS       AGE
calico-kube-controllers-56fd7b8dc-5jvpq   1/1     Running       14 (13h ago)   2d6h
calico-node-f6hvh                         1/1     Running       1 (13h ago)    2d6h
calico-node-f727b                         1/1     Running       0              2d6h
calico-node-mwf9w                         1/1     Running       1 (13h ago)    2d6h
coredns-74d6c5659f-qmwq4                  1/1     Running       0              13h
.
.

k get pod는 현재 네임스페이스에서 실행 중인 pod의 정보만 보여줍니다.

 

k ns 플러그인을 쓰지 않는다면 -n으로 네임스페이스를 지정해서 조회하면 됩니다.

 

# k get pod -n {namespace}

k get pod -n kube-system

 

--

3) kube-ps1 프롬프트

kube-ps1 플러그인: 프롬프트

커맨드라인의 프롬프트에서 클러스터와 네임스페이스 이름을 확인할 수 있는 플러그인입니다.

실수 방지용이라고 할 수 있습니다.

 

git clone https://github.com/jonmosco/kube-ps1.git

# git 소스 받기
git clone https://github.com/jonmosco/kube-ps1.git

# 실행 권한 부여
node1:$ chmod +x ~/kube-ps1/kube-ps1.sh

# bashrc 추가
source $HOME/kube-ps1/kube-ps1.sh
PS1='[\u@\h \W $(kube_ps1)]\$ '
KUBE_PS1_SYMBOL_ENABLE=false

# bashrc 파일 재실행
source ~/.bashrc

 

위 명령을 실행하면 이제 커맨드라인(터미널)에서 직관적으로 알 수 있습니다.

컨텍스트: kubernetes-admin@cluster.local

네임스페이스: kube-system

이라는 뜻이네요.

 

쿠버네티스는 kubectl을 이용해서 제어가 가능하다.

앞서 이전 글에서 kubectl get pods -o wide라고 쓰면 pod의 상태를 볼 수 있다.

그러나 엔지니어에게 너무 긴 명령어가 아닐까?

 

그래서 우리는 리눅스의 alias 기능을 활용해서 단축어 등록으로 쉽게 해결하려고 한다.

 

--

1) kubectl 단축 alias 등록

vi ~/.bashrc 셋팅

source <(kubectl completion bash)
alias k=kubectl
alias ka='kubectl get pods -o wide'
alias kgp='kubectl get pods -o wide'
alias kgd='kubectl get deploy -o wide'
alias kgs='kubectl get service -o wide'
alias kgn='kubectl get service -o wide'
alias kgn='kubectl get nodes -o wide'
alias kge='kubectl get events -w --field-selector type=Warning'
alias kgv='kubectl get pvc -o wide'
alias kgpa='kubectl get pods -o wide -A'
alias kgpw='kubectl get pods -o wide -w'
alias kgpaw='kubectl get pods -o wide -A -w'

alias krn='kubectl run nginx --image=nginx --restart=Never'
alias kcn='kubectl create deployment nginx --image=nginx'
alias krb='kubectl run busybox --image=busybox --restart=Never -- sleep 1d'
complete -F __start_kubectl k

셋팅이 되었다면 source ~/.bashrc 로 다시 로딩한다.

 

이제 kubectl get pods 가 아니라

 

k get pods로 가능하고

kgp로 kubectl get pods 전체를 요약해서 쓸 수 있다.

 

 

--

2) krew 설치

쿠버네티스에서 krew라는 플러그인 매니저를 활용한다.

mac os는 brew install krew로 손쉽게 설치가 가능한데

 

linux에서는 아래 메뉴얼을 따라 설치한다.

https://krew.sigs.k8s.io/docs/user-guide/setup/install/

 

Installing · Krew

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

krew.sigs.k8s.io

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


# .bashrc 추가
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"

# bashrc 로딩
source ~/.bashrc

 

krew가 설치되었으면 

k krew instasll

k krew search

k krew list 

등으로 사용이 가능하다.

cp -R ~/kubespray/inventory/sample ~/kubespray/inventory/mycluster

 

mycluster/inventory.ini 파일 수정

 

control-plane은 node1이 담당

etcd는 node1, node2, node3에 설치

worker-node는 node2, node3에 설치

# ## Configure 'ip' variable to bind kubernetes services on a
# ## different ip than the default iface
# ## We should set etcd_member_name for etcd cluster. The node that is not a etcd member do not need to set the value, or can set the empty string value.
[all]
node1 ansible_host=192.168.50.61 ip=192.168.50.61
node2 ansible_host=192.168.50.62 ip=192.168.50.62
node3 ansible_host=192.168.50.63 ip=192.168.50.63

# ## configure a bastion host if your nodes are not directly reachable
# [bastion]
# bastion ansible_host=x.x.x.x ansible_user=some_user

[kube_control_plane]
node1

[etcd]
node1
node2
node3

[kube_node]
node2
node3

[calico_rr]

[k8s_cluster:children]
kube_control_plane
kube_node
calico_rr

 

~/kubespray/inventory/mycluster/group_vars/k8s_cluster/k8s-cluster.yml

파일 수정

## Change this to use another Kubernetes version, e.g. a current beta release
kube_version: v1.24.6

# configure arp_ignore and arp_announce to avoid answering ARP queries from kube-ipvs0 interface
# must be set to true for MetalLB, kube-vip(ARP enabled) to work
kube_proxy_strict_arp: true

# audit log for kubernetes
kubernetes_audit: true

 

설치

ansible-playbook -i inventory/mycluster/inventory.ini --become --become-user=root cluster.yml

 

리셋

ansible-playbook -i inventory/mycluster/inventory.ini --become --become-user=root reset.yml

 

설치 완료 로그 10분 걸렸다.

 

로컬에서 kubectl 사용하려면

cat /root/.kube/config 내용을 카피

로컬 계정에 ~/.kube/config 에 작성한다.

그러면 로컬 계정에서도 사용 가능하다.

 

설치 완료된 모습이다.

node1:~/kubespray/inventory/mycluster$ kubectl get nodes
NAME    STATUS   ROLES           AGE   VERSION
node1   Ready    control-plane   13m   v1.24.6
node2   Ready    <none>          12m   v1.24.6
node3   Ready    <none>          12m   v1.24.6

node1:~/kubespray/inventory/mycluster$ kubectl get nodes -o wide
NAME    STATUS   ROLES           AGE   VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
node1   Ready    control-plane   13m   v1.24.6   192.168.50.61   <none>        Ubuntu 20.04.6 LTS   5.4.0-166-generic   containerd://1.6.8
node2   Ready    <none>          12m   v1.24.6   192.168.50.62   <none>        Ubuntu 20.04.6 LTS   5.4.0-166-generic   containerd://1.6.8
node3   Ready    <none>          12m   v1.24.6   192.168.50.63   <none>        Ubuntu 20.04.6 LTS   5.4.0-162-generic   containerd://1.6.8

 

+ Recent posts