ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • kubectl 보다 편리하게 사용하기 (2) krew, kubectx, konfig 설치하기
    Container/Kubernetes 2021. 12. 29. 19:30
    반응형

    개요

    이 문서는 쿠버네티스를 보다 쉽게 관리할 수 있는 도구들을 설치하는 방법을 기술한다. 다음의 도구들을 설치한다.

    • krew: kubectl 플러그인 매니저
    • kubectx: kubectxkubens의 집합체를 의미
      • kubectx: 쿠버네티스 컨텍스트를 보다 쉽게 관리해주는 플러그인
      • kubens: 쿠버네티스 네임스페이스를 보다 쉽게 관리해주는 도구
    • konfig:  쿠버네티스 설정 파일들을 보다 쉽게 관리할 수 있는 도구

    위의 도구들을 설치하기 이전에 gitkubectl이 설치되어 있어야 한다. 나의 개발 환경은 다음과 같다.

    • desktop: macbook pro 13 2020 (cpu: Intel Core i7 4core, memory: 32GB)
    • git: v2.34.1
    • kubectl: v1.22.4

    krew 설치

    터미널에 다음을 입력한다.

    $ 
    (
      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
    )
    # 다음이 출력됨.
    +-zsh:20> mktemp -d
    +-zsh:20> cd /var/folders/ft/gm_pnw093rb2yj7ymtmx5ygm0000gn/T/tmp.WDTeA4Rc
    +-zsh:21> OS=+-zsh:21> uname
    +-zsh:21> OS=+-zsh:21> tr '[:upper:]' '[:lower:]'
    +-zsh:21> OS=darwin
    +-zsh:22> ARCH=+-zsh:22> uname -m
    +-zsh:22> ARCH=+-zsh:22> sed -e s/x86_64/amd64/ -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/'
    +-zsh:22> ARCH=amd64
    +-zsh:23> KREW=krew-darwin_amd64
    +-zsh:24> curl -fsSLO https://github.com/kubernetes-sigs/krew/releases/latest/download/krew-darwin_amd64.tar.gz
    +-zsh:25> tar zxvf krew-darwin_amd64.tar.gz
    x ./LICENSE
    x ./krew-darwin_amd64
    +-zsh:26> ./krew-darwin_amd64 install krew
    Adding "default" plugin index from https://github.com/kubernetes-sigs/krew-index.git.
    Updated the local copy of plugin index.
    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
     |  |
     |  | You can find documentation at
     |  |   https://krew.sigs.k8s.io/docs/user-guide/quickstart/.
     | /
    /

     

    그 다음 ~/.bashrc혹은 ~/.zshrc 파일에 다음을 입력해준다. 필자의 경우 zsh를 터미널 쉘로 쓰기 때문에 ~/.zshrc에 작성하였다.

     

    ~/.zshrc

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

     

    그 후 쉘 재부팅 혹은 터미널에 다음을 입력하여, .zshrc 설정을 터미널에 적용한다.

    $ source ~/.zshrc

     

    그 다음 터미널에 kubectl krew라고 입력했을 때 다음이 출력되면 설치가 완료된 것이다.

    $ kubectl krew                                                                                               [10:54:31]
    krew is the kubectl plugin manager.
    You can invoke krew through kubectl: "kubectl krew [command]..."
    
    Usage:
      kubectl krew [command]
    
    Available Commands:
      completion  generate the autocompletion script for the specified shell
      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.

    kubectx 설치

    krew가 설치되어 있다면 kubectxkubens를 손쉽게 설치할 수 있다.

    kubectx 설치

    터미널에 다음을 입력하면 kubectx 플러그인이 설치된다.

    $ kubectl krew install ctx                                                                                         [10:55:31]
    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.

     

    다음을 입력하면 머신에 저장된 쿠버네티스 컨텍스트 내용을 조회할 수 있다. (kubectl config get-contexts와 동일하다.)

    $ kubectl ctx
    docker-desktop
    minikube

     

    만약 컨텍스트를 바꾸고 싶다면 다음처럼 입력하면 된다. (kubectl config set-context <컨텍스트 이름>와 동일하다.)

    # kubectl ctx <컨텍스트 이름>
    $ kubectl ctx minikube                                                                                       
    Switched to context "minikube".

     

    조금 더 편하게 쓰기 위해 ~/.zshrc에 다음 내용을 추가하자.

     

    ~/.zshrc

    # ...
    # kubectx
    alias kubectx='kubectl ctx'

     

    이후 쉘을 재부팅 해보면, 다음처럼 kubectx를 쓸 수 있다.

    $ kubectx
    docker-desktop
    minikube

    kubens 설치

    터미널에 다음을 입력하면 kubens 플러그인이 설치된다.

    $ kubectl krew install ns                                                                                          [11:00:09]
    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.

     

    다음을 입력하면 현재 컨텍스트에 생성된 네임스페이스 내용을 조회할 수 있다. (kubectl get namespaces와 동일하다.)

    $ kubectl ns
    default
    kube-node-lease
    kube-public
    kube-system

     

    만약 기본 네임스페이스를 바꾸고 싶다면 다음처럼 입력하면 된다. (kubectl config set-context --current --namespace=<네임스페이스 이름>과 동일하다.)

    # kubectl ns <네임스페이스 이름>
    $ kubectl ns kube-system
    Context "minikube" modified.
    Active namespace is "kube-system".

     

    조금 더 편하게 쓰기 위해 ~/.zshrc에 다음 내용을 추가하자.

     

    ~/.zshrc

    # ...
    # kubens
    alias kubens='kubectl ns'

     

    이후 쉘을 재부팅 해보면, 다음처럼 kubens를 쓸 수 있다.

    $ kubens
    default
    kube-node-lease
    kube-public
    kube-system

    인터렉티브 모드 활성화

    krew로 설치한 경우, 자동 완성 기능이 활성화되어 있지 않다. 물론 자동 완성 기능을 활성화해도 좋지만 개인적으로 조금 더 편한 인터렉티브 모드를 활성화해볼까 한다. 먼저 fzf가 설치되어 있어야 한다. 없다면 Homebrew를 통해서 설치한다.

    $ brew install fzf

     

    그 후 ~/.zshrc에 다음을 추가한다.

     

    ~/.zshrc

    # ...
    # kubectx interactive mode
    KUBECTX_IGNORE_FZF=1

     

    그 후 쉘을 재부팅한 후 kubectx 혹은 kubens를 입력하면 다음처럼 컨텍스트나 네임스페이스를 설정할 수 있는 인터렉티브 모드로 진입하게 된다.

    konfig

    역시 krew를 이용해 손쉽게 설치가 가능하다. 터미널에 다음을 입력한다.

    $ kubectl krew install konfig
    Updated the local copy of plugin index.
    Installing plugin: konfig
    Installed plugin: konfig
    \
     | Use this plugin:
     |     kubectl konfig
     | Documentation:
     |     https://github.com/corneliusweig/konfig
    /
    WARNING: You installed plugin "konfig" from the krew-index plugin repository.
       These plugins are not audited for security by the Krew maintainers.
       Run them at your own risk.

     

    정상적으로 설치되었다면 kubectl konfig를 입력하면 다음이 출력된다.

    $ kubectl konfig                                                                                             
    kubectl konfig helps to merge, split or import kubeconfig files
    
    USAGE:
      kubectl konfig merge [--preserve-structure,-p] <CONFIG>..
         Merge multiple kubeconfigs into one.
         -p  prevents flattening which will make the result less portable.
    
      kubectl konfig import [--preserve-structure,-p] [--save,-s] [--stdin,-i] <CONFIG>..
         Import the given configs into your current kubeconfig (respects KUBECONFIG env var).
         -s  writes the result to your ~/.kube/config
         -i  import kubeconfig string from stdin
    
      kubectl konfig split  <CONTEXT>.. [--kubeconfig,-k <CONFIG>]
      kubectl konfig export <CONTEXT>.. [--kubeconfig,-k <CONFIG>]
         Export/split off a minimal kubeconfig with the given contexts
          -k  may be repeated or contain a comma-delimited list of input configs.
              When omitted, export from the default kubeconfig.
    
    EXAMPLES:
      Merge new-cfg with your current kubeconfig
        $ kubectl konfig import new-cfg
      and save the result to ~/.kube/config
        $ kubectl konfig import --save new-cfg
      CAVEAT: due to how shells work, the following will lose your current ~/.kube/config
        WRONG $ kubectl konfig import new-cfg > ~/.kube/config
    
      Export ctx1 and ctx2 into combined.yaml
        $ kubectl konfig export -k ~/.kube/config -k k3s.yaml ctx1 ctx2 > combined.yaml
    
      Merge two configs
        $ kubectl konfig merge ~/.kube/config k3s.yaml > merged-and-flattened
        or
        $ kubectl konfig merge -p ~/.kube/config k3s.yaml > merged-not-flattened

     

    역시 보다 편하게 사용하기 위해서 ~/.zshrc에 다음 내용을 추가한다.

     

    ~/.zshrc

    # ...
    # konfig
    alias konfig='kubectl konfig'

     

    그 후 쉘을 재접속, konfig를 입력하면 다음이 출력된다.

    $ konfig
    kubectl konfig helps to merge, split or import kubeconfig files
    
    USAGE:
      kubectl konfig merge [--preserve-structure,-p] <CONFIG>..
         Merge multiple kubeconfigs into one.
         -p  prevents flattening which will make the result less portable.
    
      kubectl konfig import [--preserve-structure,-p] [--save,-s] [--stdin,-i] <CONFIG>..
         Import the given configs into your current kubeconfig (respects KUBECONFIG env var).
         -s  writes the result to your ~/.kube/config
         -i  import kubeconfig string from stdin
    
      kubectl konfig split  <CONTEXT>.. [--kubeconfig,-k <CONFIG>]
      kubectl konfig export <CONTEXT>.. [--kubeconfig,-k <CONFIG>]
         Export/split off a minimal kubeconfig with the given contexts
          -k  may be repeated or contain a comma-delimited list of input configs.
              When omitted, export from the default kubeconfig.
    
    EXAMPLES:
      Merge new-cfg with your current kubeconfig
        $ kubectl konfig import new-cfg
      and save the result to ~/.kube/config
        $ kubectl konfig import --save new-cfg
      CAVEAT: due to how shells work, the following will lose your current ~/.kube/config
        WRONG $ kubectl konfig import new-cfg > ~/.kube/config
    
      Export ctx1 and ctx2 into combined.yaml
        $ kubectl konfig export -k ~/.kube/config -k k3s.yaml ctx1 ctx2 > combined.yaml
    
      Merge two configs
        $ kubectl konfig merge ~/.kube/config k3s.yaml > merged-and-flattened
        or
        $ kubectl konfig merge -p ~/.kube/config k3s.yaml > merged-not-flattened

    참고

Designed by Tistory.