본문 바로가기

Tools

[Tools]Kubernetes 운영을 도와주는 도구 모음 - kube-ps1, kubectx, kubens, k neat, kubetail

Concept

  • kube-ps1
    • shell에서 kubernetes 현재 context와 namespace를 나타낸다.
  • kubectx
    • context 전환을 도와주는 도구
  • kubens
    • namespace 전환을 도와주는 도구
  • k neat
    • Remove clutter from Kubernetes manifests to make them more readable.****
  • kubetail
    • aggregate logs from multiple pods into one stream

kube-ps1(kube prompt status)

kube-ps1 설치 참고 https://github.com/jonmosco/kube-ps1

  • kube cluster와 namespace 정보를 표시

Install (Linux, Mac os)

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

add bashrc

bash의 경우 ~/.bash rc, zsh의 경우 ~/.zshrc 에 아래 내용을 추가하고 source ~/.bashrc 명령어로 적용시킨다.

# for linux
source /[download location]/kube-ps1/kube-ps1.sh
PS1='[\\u@\\h \\W $(kube_ps1)]\\$ '
KUBE_PS1_SYMBOL_ENABLE=false

# for mac os
source /[download location]/kube-ps1/kube-ps1.sh
PROMPT='$(kube_ps1)'$PROMPT
KUBE_PS1_SYMBOL_ENABLE=false

Change context name for convenience

edit ~/.kube/config

- context:
    cluster: arn:aws:eks:ap-northeast-2:743647085036:cluster/jm-han-eks-1
    user: arn:aws:eks:ap-northeast-2:743647085036:cluster/jm-han-eks-1
  name: arn:aws:eks:ap-northeast-2:743647085036:cluster/jm-han-eks-1
- context:
    cluster: arn:aws:eks:ap-northeast-2:743647085036:cluster/jm-han-eks-1
    user: arn:aws:eks:ap-northeast-2:743647085036:cluster/jm-han-eks-1
  name: jm-han-eks-1
  current-context: jm-han-eks-1

verify

[dewble@ kube-ps1 (arn:aws:eks:ap-northeast-2:743647085036:cluster/jm-han-eks-1:default)]$
[dewble@ kube-ps1 (jm-han-eks-1:default)]$

 

 

kubectx + kubens

kubectx + kubens 참고 https://github.com/ahmetb/kubectx

  • kubectx is a tool to switch between contexts (clusters) on kubectl faster.
  • kubens is a tool to switch between Kubernetes namespaces (and configure them for kubectl) easily.

Install (Linux)

Completion scripts for bash https://github.com/ahmetb/kubectx#completion-scripts-for-bash

# install first
sudo apt install pkg-config

# install 
git clone https://github.com/ahmetb/kubectx.git ~/.kubectx
COMPDIR=$(pkg-config --variable=completionsdir bash-completion)
ln -sf ~/.kubectx/completion/kubens.bash $COMPDIR/kubens
ln -sf ~/.kubectx/completion/kubectx.bash $COMPDIR/kubectx
cat << EOF >> ~/.bashrc

#kubectx and kubens
export PATH=~/.kubectx:\\$PATH
EOF

Install (Mac os)

# install
sudo git clone https://github.com/ahmetb/kubectx /opt/kubectx
sudo mkdir /usr/local/bin
sudo ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx
sudo ln -s /opt/kubectx/kubens /usr/local/bin/kubens

# Completion scripts for plain zsh
mkdir -p ~/.oh-my-zsh/completions
chmod -R 755 ~/.oh-my-zsh/completions
ln -s /opt/kubectx/completion/_kubectx.zsh ~/.oh-my-zsh/completions/_kubectx.zsh
ln -s /opt/kubectx/completion/_kubens.zsh ~/.oh-my-zsh/completions/_kubens.zsh

Verify

[dewble@ completions (cms3-eks:default)]$ ll kubectx
lrwxrwxrwx 1 root root 45 Aug  3 11:22 kubectx -> /home/dewble/.kubectx/completion/kubectx.bash
[dewble@ completions (cms3-eks:default)]$ ll kubens
lrwxrwxrwx 1 root root 44 Aug  3 11:22 kubens -> /home/dewble/.kubectx/completion/kubens.bash

How to use kubectx

# get cluster list
[dewble@ kubectx (cms3-eks:default)]$ ./kubectx
cms3-eks
gke_jmhan-int-220517_asia-northeast3-a_prom-gke
jm-han-eks-1

# switch cluster
[dewble@ kubectx (cms3-eks:default)]$ ./kubectx jm-han-eks-1
Switched to context "jm-han-eks-1".

# switch back to previous cluster
[dewble@ kubectx (jm-han-eks-1:default)]$ ./kubectx -
Switched to context "cms3-eks".

How to use kubens

# get namespace list
[dewble@ kubectx (cms3-eks:default)]$ ./kubens
default
kube-node-lease
kube-public
kube-system

# switch namespace
[dewble@ kubectx (cms3-eks:default)]$ ./kubens kube-public
Context "cms3-eks" modified.
Active namespace is "kube-public".

# go back to the previous namespace
[dewble@ kubectx (cms3-eks:kube-public)]$ ./kubens -
Context "cms3-eks" modified.
Active namespace is "default".

k neat

kubectl-neat 참고 https://github.com/itaysk/kubectl-neat

  • -o yaml 로 기존 리소스를 저장하면 아래와 같은 불필요한 정보도 추가되어 저장된다.
  • 1. 생성 타임스탬프 또는 일부 내부 ID와 같은 메타데이터 2. 누락된 속성을 기본값으로 채우기 3. 서비스 계정 토큰과 같은 승인 컨트롤러가 생성한 추가 시스템 속성 4. 상태 정보
  • k neat를 사용하여 정리된 yaml 을 얻을 수 있다.

Install krew first

  • Krew is a tool that makes it easy to use kubectl plugins

Install krew 참고 https://github.com/kubernetes-sigs/krew/

Install

# git clone first
git clone https://github.com/itaysk/kubectl-neat.git

# copy & paste
(
  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
)

# result
>>> 
$ (
>   set -x; cd "$(mktemp -d)" &&
"$(un>   OS="$(uname | tr '[:upper:]' '[:lower:]')" &&
 ARCH>   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
> )
++ mktemp -d
+ cd /tmp/tmp.lwvGwS9wha
++ uname
++ tr '[:upper:]' '[:lower:]'
+ OS=linux
++ uname -m
++ sed -e s/x86_64/amd64/ -e 's/\\(arm\\)\\(64\\)\\?.*/\\1\\2/' -e 's/aarch64$/arm64/'
+ ARCH=amd64
+ KREW=krew-linux_amd64
+ curl -fsSLO <https://github.com/kubernetes-sigs/krew/releases/latest/download/krew-linux_amd64.tar.gz>

+ tar zxvf krew-linux_amd64.tar.gz
./LICENSE
./krew-linux_amd64
+ ./krew-linux_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/>.
 | /
/

Add Env to bashrc or zshrc

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

verify

$ k krew
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.

Install neat with krew

kubectl krew install neat

>>>
$ 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.

How to use k neat

k get pod [pod-name] -o yaml | k neat
  • k neat를 끝에 붙여준다.

Verify

AS-IS

$ k get pod k-neat-nginx -o yaml
apiVersion: v1
kind: Pod
metadata:
  annotations:
    kubernetes.io/psp: eks.privileged
  creationTimestamp: "2022-08-03T02:46:43Z"
  labels:
    run: k-neat-nginx
  name: k-neat-nginx
  namespace: default
  resourceVersion: "12381"
  uid: 6e0adf67-a9d4-422f-9667-6073b9b73252
spec:
  containers:
  - image: nginx
    imagePullPolicy: Always
    name: k-neat-nginx
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: kube-api-access-2rdlw
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: ip-192-168-255-211.ap-northeast-2.compute.internal
  preemptionPolicy: PreemptLowerPriority
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: kube-api-access-2rdlw
    projected:
      defaultMode: 420
      sources:
      - serviceAccountToken:
          expirationSeconds: 3607
          path: token
      - configMap:
          items:
          - key: ca.crt
            path: ca.crt
          name: kube-root-ca.crt
      - downwardAPI:
          items:
          - fieldRef:
              apiVersion: v1
              fieldPath: metadata.namespace
            path: namespace
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2022-08-03T02:46:43Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2022-08-03T02:46:53Z"
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2022-08-03T02:46:53Z"
    status: "True"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2022-08-03T02:46:43Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://046a2e97fcec7b0b3ac09f92ace5b267b399edca0a2b1be2abc0d90d19dd3686
    image: nginx:latest
    imageID: docker-pullable://nginx@sha256:691eecfa41f219b32acea5a3561a8d8691d8320e5a00e1cb4574de5827e077a7
    lastState: {}
    name: k-neat-nginx
    ready: true
    restartCount: 0
    started: true
    state:
      running:
        startedAt: "2022-08-03T02:46:52Z"
  hostIP: 192.168.255.211
  phase: Running
  podIP: 192.168.230.113
  podIPs:
  - ip: 192.168.230.113
  qosClass: BestEffort
  startTime: "2022-08-03T02:46:43Z"

TO-BE (use k neat)

$ k get pod k-neat-nginx -o yaml | k neat
apiVersion: v1
kind: Pod
metadata:
  annotations:
    kubernetes.io/psp: eks.privileged
  labels:
    run: k-neat-nginx
  name: k-neat-nginx
  namespace: default
spec:
  containers:
  - image: nginx
    name: k-neat-nginx
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: kube-api-access-2rdlw
      readOnly: true
  preemptionPolicy: PreemptLowerPriority
  priority: 0
  serviceAccountName: default
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: kube-api-access-2rdlw
    projected:
      sources:
      - serviceAccountToken:
          expirationSeconds: 3607
          path: token
      - configMap:
          items:
          - key: ca.crt
            path: ca.crt
          name: kube-root-ca.crt
      - downwardAPI:
          items:
          - fieldRef:
              fieldPath: metadata.namespace
            path: namespace

kubetail

kubetail 참고 https://github.com/johanhaleby/kubetail

  • 여러 POD의 로그를 동시에 조회하는 기능
  • "kubectl logs -f "를 실행하는 것과 동일하지만 여러 포드에 적용

Install

git clone https://github.com/johanhaleby/kubetail.git

Link script

ln -s /[path]/kubetail /usr/local/bin/kubetail

ln -s /home/dewble/workspace/tools/kubetail/kubetail /usr/local/bin/kubetail

Completion

Add env to bashrc or zsh

source [path]/kubetail/completion/kubetail.bash
source [path]/kubetail/completion/kubetail.zsh

verify

$ kubetail
kubetail <search term> [-h] [-c] [-n] [-t] [-l] [-f] [-d] [-P] [-p] [-s] [-b] [-e] [-j] [-k] [-z] [-v] [-r] [-i] -- tail multiple Kubernetes pod logs at the same time

where:
    -h, --help              Show this help text.
    -c, --container         The name of the container to tail in the pod (if multiple containers are defined in the pod).
                            Defaults to all containers in the pod. Can be used multiple times.
    -t, --context           The k8s context. ex. int1-context. Relies on ~/.kube/config for the contexts.
    -l, --selector          Label selector. If used the pod name is ignored.
    -n, --namespace         The Kubernetes namespace where the pods are located. Defaults to "default".
    -f, --follow            Specify if the logs should be streamed. (true|false) Defaults to true.
    -d, --dry-run           Print the names of the matched pods and containers, then exit.
    -P, --prefix            Specify if add the pod name prefix before each line. (true|false) Defaults to true.
    -p, --previous          Return logs for the previous instances of the pods, if available. (true|false) Defaults to false.
    -s, --since             Only return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to 10s.
    -b, --line-buffered     This flags indicates to use line-buffered. (true|false) Defaults to false.
    -e, --regex             The type of name matching to use (regex|substring). Defaults to substring.
    -j, --jq                If your output is json - use this jq-selector to parse it. Defaults to "".
                            example: --jq ".logger + \\" \\" + .message"
    -k, --colored-output    Use colored output (pod|line|false).
                            pod = only color pod name, line = color entire line, false = don't use any colors.
                            Defaults to line.
    -z, --skip-colors       Comma-separated list of colors to not use in output.
                            If you have green foreground on black, this will skip dark grey and some greens: -z 2,8,10
                            Defaults to: 7,8.
        --timestamps        Show timestamps for each log line. (true|false) Defaults to false.
        --tail              Lines of recent log file to display. Defaults to -1, showing all log lines.
    -v, --version           Prints the kubetail version.
    -r, --cluster           The name of the kubeconfig cluster to use.
    -i, --show-color-index  Show the color index before the pod name prefix that is shown before each log line.
                            Normally only the pod name is added as a prefix before each line, for example "[app-5b7ff6cbcd-bjv8n]",
                            but if "show-color-index" is true then color index is added as well: "[1:app-5b7ff6cbcd-bjv8n]".
                            This is useful if you have color blindness or if you want to know which colors to exclude (see "--skip-colors").
                            Defaults to false.

examples:
    kubetail my-pod-v1
    kubetail my-pod-v1 -c my-container
    kubetail my-pod-v1 -t int1-context -c my-container
    kubetail '(service|consumer|thing)' -e regex
    kubetail -l service=my-service
    kubetail --selector service=my-service --since 10m
    kubetail --tail 1

How to use

container or multiple containers

$ kubetail app2 -c container1
$ kubetail app2 -c container1 -c container2

multiple apps(pods)

$ kubetail app1,app2

 

deployment or daemonSet

kubetail "coredns-556f6dffc4-*" -n kube-system
Will tail 2 logs...
coredns-556f6dffc4-bd2mr
coredns-556f6dffc4-hbvdt

Label selector.

kubetail --selector service=my-service --since 10m
kubetail --selector release=p-jm-han

Regex

$ kubetail "^app1|.*my-demo.*" --regex

$ kubetail '.*loki-read-*|.*loki-write-*' -n monitoring --regex
Using regex '.*loki-read-*|.*loki-write-*' to match pods
Will tail 6 logs...
loki-read-0
loki-read-1
loki-read-2
loki-write-0
loki-write-1
loki-write-2

help

$ kubetail -h

 

https://jerryljh.tistory.com/17