1.1. kubectl 유저에게 적용하기
1) 클러스터 설정 파일 확인
# 루트로 접속 vmadmin@ubu22-01:~$ sudo bash # kubespray의 config 파일 확인 root@ubu22-01:~/.kube# cat config apiVersion: v1 clusters: - cluster: certificate-authority-data: {key_value} server: https://127.0.0.1:6443 name: cluster.local contexts: - context: cluster: cluster.local user: kubernetes-admin name: kubernetes-admin@cluster.local current-context: kubernetes-admin@cluster.local kind: Config preferences: {} users: - name: kubernetes-admin user: client-certificate-data: {key_value} client-key-data: {Key_value} |
2) sudo 권한이 있는 유저 디렉토리에 복사
# .kube디렉토리 생성 root@ubu22-01:~/.kube# mkdir /home/vmadmin/.kube/ # root의 config를 복사 root@ubu22-01:~/.kube# cp config /home/vmadmin/.kube/ # sudo 권한이 있는 유저의 디렉토리로 이동 root@ubu22-01:~/.kube# cd /home/vmadmin/.kube # 복사한 config 확인 root@ubu22-01:/home/vmadmin/.kube# ll total 16 drwxr-xr-x 2 root root 4096 8월 8 14:40 ./ drwxr-x--- 20 vmadmin vmadmin 4096 8월 8 14:40 ../ -rw------- 1 root root 5653 8월 8 14:40 config # .kube의 소유자를 확인 root@ubu22-01:/home/vmadmin/.kube# cd .. root@ubu22-01:/home/vmadmin# ll total 96 drwxr-x--- 20 vmadmin vmadmin 4096 8월 8 14:40 ./ drwxr-xr-x 3 root root 4096 8월 8 09:49 ../ . drwxr-xr-x 2 root root 4096 8월 8 14:40 .kube/ drwxrwxr-x 18 vmadmin vmadmin 4096 8월 8 12:03 kubespray/ . |
3) 소유자 변경
root@ubu22-01:/home/vmadmin# chown -R vmadmin:vmadmin .kube . drwxr-xr-x 2 4096 vmadmin:vmadmin 8월 8 14:40 .kube/ . |
4) root가 아닌, 일반 유저에서도 kubectl이 동작한다.
vmadmin@ubu22-01:~$ kubectl get nodes -o wide NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME ubu22-01 Ready control-plane 92m v1.26.7 192.168.100.30 <none> Ubuntu 22.04.3 LTS 6.2.0-26-generic containerd://1.7.2 ubu22-02 Ready <none> 91m v1.26.7 192.168.100.40 <none> Ubuntu 22.04.3 LTS 6.2.0-26-generic containerd://1.7.2 ubu22-03 Ready <none> 91m v1.26.7 192.168.100.50 <none> Ubuntu 22.04.3 LTS 6.2.0-26-generic containerd://1.7.2 vmadmin@ubu22-01:~$ kubectl config view apiVersion: v1 clusters: - cluster: certificate-authority-data: DATA+OMITTED server: https://127.0.0.1:6443 name: cluster.local contexts: - context: cluster: cluster.local user: kubernetes-admin name: kubernetes-admin@cluster.local current-context: kubernetes-admin@cluster.local kind: Config preferences: {} users: - name: kubernetes-admin user: client-certificate-data: DATA+OMITTED client-key-data: DATA+OMITTED |
1.2. Bash_Completion 및 alias 설정
1) bash-completion 설치
vmadmin@ubu22-01:~$ sudo apt -y install bash-completion Reading package lists... Done Building dependency tree... Done Reading state information... Done bash-completion is already the newest version (1:2.11-5ubuntu1). 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. # bash-completion은 /etc/bash_completion.d에 있는 모든 자동 완성 스크립트를 소싱한다. |
2) bashrc에 적용
vmadmin@ubu22-01:~$ echo 'source <(kubectl completion bash)' >> ~/.bashrc vmadmin@ubu22-01:~$ echo 'alias k=kubectl' >> ~/.bashrc vmadmin@ubu22-01:~$ echo 'complete -F __start_kubectl k' >> ~/.bashrc vmadmin@ubu22-01:~$ tail ~/.bashrc if ! shopt -oq posix; then if [ -f /usr/share/bash-completion/bash_completion ]; then . /usr/share/bash-completion/bash_completion elif [ -f /etc/bash_completion ]; then . /etc/bash_completion fi fi source <(kubectl completion bash) alias k=kubectl complete -F __start_kubectl k # 적용 vmadmin@ubu22-01:~$ source ~/.bashrc |
3) 추가적인 alias 지정
vmadmin@ubu22-01:~$ vi ~/.bashrc # 현재 위치 아래 모든 yaml 파일을 적용한다. alias ka='kubectl apply --recursive -f' # 현재 ns의 pod을 자세히 본다 alias kgp='kubectl get pods -o wide' # 현재 ns의 deployment를 자세히 본다 alias kgd='kubectl get deploy -o wide' # 현재 ns의 service를 자세히 본다 alias kgs='kubectl get service -o wide' # 현재 cluster의 node를 자세히 본다 alias kgn='kubectl get nodes -o wide' # 지금까지 발생한 event 중 Warning만 모아서 본다. alias kge='kubectl get events -w --field-selector type=Warning' # 현재 ns의 persistent volume claim을 자세히 본다 alias kgv='kubectl get pvc -o wide' # 모든 ns의 pod을 자세히 본다 alias kgpa='kubectl get pods -o wide -A' # 현재 ns의 pod을 자세히 보고 상태 변하는 것을 계속 지켜본다. alias kgpw='kubectl get pods -o wide -w' # 모든 ns의 pod을 자세히 보고 상태 변하는 것을 계속 지켜본다. alias kgpaw='kubectl get pods -o wide -A -w' # 이름이 nginx이고, nginx 이미지를 사용하는 Pod를 생성한다. alias krn='kubectl run nginx --image=nginx --restart=Never' # 이름이 nginx이고, nginx 이미지를 사용하는 deployment를 생성한다. alias kcn='kubectl create deployment nginx --image=nginx' # 이름이 busybox이고, busybox 이미지를 사용하며, sleep 1d 커맨드가 들어있는 Pod를 생성한다. alias krb='kubectl run busybox --image=busybox --restart=Never -- sleep 1d' |
4) alias 명령어 동작 확인
vmadmin@ubu22-01:~$ kgp No resources found in default namespace. |
1.3. Krew install
vmadmin@ubu22-01:~$ ( 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: kubectl 플러그인을 관리하는 도구 . . . Installing plugin: krew Installed plugin: krew \ | Use this plugin: | kubectl krew | Documentation: | https://krew.sigs.k8s.io/ | Caveats: | \ | | krew is now installed! To start using kubectl plugins, you need to add | | krew's installation directory to your PATH: | | | | * macOS/Linux: | | - Add the following to your ~/.bashrc or ~/.zshrc: | | export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH" | | - Restart your shell. | | | | * Windows: Add %USERPROFILE%\.krew\bin to your PATH environment variable | | | | To list krew commands and to get help, run: | | $ kubectl krew | | For a full list of available plugins, run: | | $ kubectl krew search | | default | | You can find documentation at | | https://krew.sigs.k8s.io/docs/user-guide/quickstart/. | / / |
# 유저의 쉘 설정을 추가 vmadmin@ubu22-01:~$ vi .bashrc ... export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH" # 적용 vmadmin@ubu22-01:~$ source ~/.bashrc # krew plugin 명령어 확인 vmadmin@ubu22-01:~$ 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. vmadmin@ubu22-01:~$ echo $PATH /home/vmadmin/.krew/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin |
1.4. CTX install
vmadmin@ubu22-01:~$ k 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. # 컨텍스트 아이디 확인 kevin@ubu22-01:~$ k ctx kubernetes-admin@cluster.local |
# 컨텍스트 목록 확인 vmadmin@ubu22-01:~$ k config get-contexts CURRENT NAME CLUSTER AUTHINFO NAMESPACE * kubernetes-admin@cluster.local cluster.local kubernetes-admin |
# 현재 컨텍스트 확인 admin1@ubu22-01:~$ k config current-context kubernetes-admin@cluster.local |
# 지정된 컨텍스트 사용 admin1@ubu22-01:~$ k config use-context kubernetes-admin@cluster.local Switched to context "kubernetes-admin@cluster.local". |
1.5. NS install
1) 설치
kevin@ubu22-01:~$ k 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. # 현재 네임 스페이스 확인 kevin@ubu22-01:~$ k ns default kube-node-lease kube-public kube-system |
2) ns 사용
# 현재 네임 스페이스(default)의 pod를 확인 kevin@ubu22-01:~$ k get pod No resources found in default namespace. # 네임 스페이스 kube-system의 포드를 확인 kevin@ubu22-01:~$ k get pod -n kube-system NAME READY STATUS RESTARTS AGE calico-kube-controllers-6dfcdfb99-s8rks 1/1 Running 0 112m calico-node-87jt9 1/1 Running 0 113m calico-node-bv6t2 1/1 Running 0 113m calico-node-vlw8g 1/1 Running 0 113m coredns-645b46f4b6-c6dqn 1/1 Running 0 112m coredns-645b46f4b6-tl8td 1/1 Running 0 112m dns-autoscaler-659b8c48cb-tzllr 1/1 Running 0 112m kube-apiserver-ubu22-01 1/1 Running 1 115m kube-controller-manager-ubu22-01 1/1 Running 2 115m kube-proxy-4tcm9 1/1 Running 0 113m kube-proxy-bdbgn 1/1 Running 0 113m kube-proxy-ps42j 1/1 Running 0 113m kube-scheduler-ubu22-01 1/1 Running 1 115m nginx-proxy-ubu22-02 1/1 Running 0 112m nginx-proxy-ubu22-03 1/1 Running 0 112m nodelocaldns-fbcgk 1/1 Running 0 112m nodelocaldns-h2k2f 1/1 Running 0 112m nodelocaldns-mp7pk 1/1 Running 0 112m # 현재 네임 스페이스(default)를 kube-system으로 변경 kevin@ubu22-01:~$ k ns kube-system Context "kubernetes-admin@cluster.local" modified. Active namespace is "kube-system". k ns # 현재 네임 스페이스 확인 kevin@ubu22-01:~$ k ns default kube-node-lease kube-public kube-system # 현재 네임 스페이스의 포드를 확인 kevin@ubu22-01:~$ k get pod NAME READY STATUS RESTARTS AGE calico-kube-controllers-6dfcdfb99-s8rks 1/1 Running 0 113m calico-node-87jt9 1/1 Running 0 114m calico-node-bv6t2 1/1 Running 0 114m calico-node-vlw8g 1/1 Running 0 114m coredns-645b46f4b6-c6dqn 1/1 Running 0 113m coredns-645b46f4b6-tl8td 1/1 Running 0 113m dns-autoscaler-659b8c48cb-tzllr 1/1 Running 0 113m kube-apiserver-ubu22-01 1/1 Running 1 116m kube-controller-manager-ubu22-01 1/1 Running 2 116m kube-proxy-4tcm9 1/1 Running 0 114m kube-proxy-bdbgn 1/1 Running 0 114m kube-proxy-ps42j 1/1 Running 0 114m kube-scheduler-ubu22-01 1/1 Running 1 116m nginx-proxy-ubu22-02 1/1 Running 0 113m nginx-proxy-ubu22-03 1/1 Running 0 113m nodelocaldns-fbcgk 1/1 Running 0 113m nodelocaldns-h2k2f 1/1 Running 0 113m nodelocaldns-mp7pk 1/1 Running 0 113m |
1.6. 프롬프트 표시 플러그인 설치
1) git clone을 활용하여 설치
# 프롬프트 플러그인 다운로드 kevin@ubu22-01:~$ git clone https://github.com/jonmosco/kube-ps1.git # git으로 다운받은 플러그인에 실행 권한 부여 kevin@ubu22-01:~$ chmod +x ./kube-ps1/kube-ps1.sh # 유저의 쉘 설정을 추가로 작성 kevin@ubu22-01:~$ vi .bashrc --- . . . source $HOME/kube-ps1/kube-ps1.sh PS1='[\u@\h \w $(kube_ps1)]\$ ' KUBE_PS1_SYMBOL_ENABLE=false ... |
2) 플러그인 적용
kevin@ubu22-01:~$ source ~/.bashrc |
3) 확인
[kevin@ubu22-01 ~ (kubernetes-admin@cluster.local:kube-system)]$ k ns default Context "kubernetes-admin@cluster.local" modified. Active namespace is "default". |