フラミナル

考え方や調べたことを書き殴ります。IT技術系記事多め

【新規ツール探し】複数の Kubernetes クラスタにまとめて kubectl を実行できる「kubectl-foreach」が便利

Kubernetes

  • 記事作成日:2022/11/30

情報

名前 URL
Github https://github.com/ahmetb/kubectl-foreach
公式サイト
デモサイト
開発母体 ahmetb 氏
version 0.2.1
言語 Go
価格 無料
ライセンス Apache-2.0 license

何ができるもの?

複数の Kubernetes クラスタにまとめて kubectl が実行できます。

使い方

go install github.com/ahmetb/kubectl-foreach@latest

kind でクラスタを作っておく

kind create cluster --name 1
kind create cluster --name 2
kind create cluster --name 3

version チェック

❯ kubectl foreach kind-1 kind-2 kind-3 -- version --short
Will run command in context(s):
  - kind-1
  - kind-2
  - kind-3
Continue? [Y/n]: Y
kind-3 | Flag --short has been deprecated, and will be removed in the future. The --short output will become the default.
kind-2 | Flag --short has been deprecated, and will be removed in the future. The --short output will become the default.
kind-1 | Flag --short has been deprecated, and will be removed in the future. The --short output will become the default.
kind-1 | Client Version: v1.25.0
kind-1 | Kustomize Version: v4.5.7
kind-1 | Server Version: v1.25.3
kind-3 | Client Version: v1.25.0
kind-3 | Kustomize Version: v4.5.7
kind-3 | Server Version: v1.25.3
kind-2 | Client Version: v1.25.0
kind-2 | Kustomize Version: v4.5.7
kind-2 | Server Version: v1.25.3

3ついっぺんにみれている

正規表現

ホスト名を正規表現で引っ掛けることもできる。

❯ kubectl foreach "/^kind-[12]/" -- get pods -A
Will run command in context(s):
  - kind-1
  - kind-2
Continue? [Y/n]: 
kind-1 | NAMESPACE            NAME                                      READY   STATUS    RESTARTS   AGE
kind-1 | kube-system          coredns-565d847f94-4tz8g                  1/1     Running   0          3m3s
kind-1 | kube-system          coredns-565d847f94-blfkj                  1/1     Running   0          3m3s
kind-1 | kube-system          etcd-1-control-plane                      1/1     Running   0          3m17s
kind-1 | kube-system          kindnet-92c9l                             1/1     Running   0          3m4s
kind-1 | kube-system          kube-apiserver-1-control-plane            1/1     Running   0          3m17s
kind-1 | kube-system          kube-controller-manager-1-control-plane   1/1     Running   0          3m16s
kind-1 | kube-system          kube-proxy-6vkkz                          1/1     Running   0          3m4s
kind-1 | kube-system          kube-scheduler-1-control-plane            1/1     Running   0          3m18s
kind-1 | local-path-storage   local-path-provisioner-684f458cdd-gx5zb   1/1     Running   0          3m3s
kind-2 | NAMESPACE            NAME                                      READY   STATUS    RESTARTS   AGE
kind-2 | kube-system          coredns-565d847f94-ln8vk                  1/1     Running   0          2m46s
kind-2 | kube-system          coredns-565d847f94-twtg8                  1/1     Running   0          2m46s
kind-2 | kube-system          etcd-2-control-plane                      1/1     Running   0          2m59s
kind-2 | kube-system          kindnet-7pzzr                             1/1     Running   0          2m46s
kind-2 | kube-system          kube-apiserver-2-control-plane            1/1     Running   0          2m59s
kind-2 | kube-system          kube-controller-manager-2-control-plane   1/1     Running   0          3m
kind-2 | kube-system          kube-proxy-68xbn                          1/1     Running   0          2m46s
kind-2 | kube-system          kube-scheduler-2-control-plane            1/1     Running   0          3m1s
kind-2 | local-path-storage   local-path-provisioner-684f458cdd-njptd   1/1     Running   0          2m46s

特定のクラスタ以外

これで kind-3 以外が対象になる。

kubectl foreach ^kind-3 -- get pods -A

Nginx を起動する

-q は確認を出さない

❯ kubectl foreach -q /kind/ -- create ns nginx
Will run command in context(s):
  - kind-1
  - kind-2
  - kind-3
kind-1 | namespace/nginx created
kind-3 | namespace/nginx created
kind-2 | namespace/nginx created

❯ kubectl foreach -1 /kind/ -- create deployment nginx --image=nginx --namespace nginx
Will run command in context(s):
  - kind-1
  - kind-2
  - kind-3
kind-3 | deployment.apps/nginx created
kind-2 | deployment.apps/nginx created
kind-1 | deployment.apps/nginx created

❯ kubectl foreach -q /kind/ -- get deployments --namespace nginx
Will run command in context(s):
  - kind-1
  - kind-2
  - kind-3
kind-3 | NAME    READY   UP-TO-DATE   AVAILABLE   AGE
kind-3 | nginx   1/1     1            1           114s
kind-1 | NAME    READY   UP-TO-DATE   AVAILABLE   AGE
kind-1 | nginx   1/1     1            1           114s
kind-2 | NAME    READY   UP-TO-DATE   AVAILABLE   AGE
kind-2 | nginx   1/1     1            1           114s

ログをまとめてみる

❯ kubectl foreach -q /kind/ -- logs -f -n nginx -l app=nginx
Will run command in context(s):
  - kind-1
  - kind-2
  - kind-3
kind-3 | 2022/11/30 14:10:10 [notice] 1#1: nginx/1.23.2
kind-3 | 2022/11/30 14:10:10 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
kind-3 | 2022/11/30 14:10:10 [notice] 1#1: OS: Linux 5.10.124-linuxkit
kind-3 | 2022/11/30 14:10:10 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
kind-3 | 2022/11/30 14:10:10 [notice] 1#1: start worker processes
kind-3 | 2022/11/30 14:10:10 [notice] 1#1: start worker process 32
kind-3 | 2022/11/30 14:10:10 [notice] 1#1: start worker process 33
kind-3 | 2022/11/30 14:10:10 [notice] 1#1: start worker process 34
kind-3 | 2022/11/30 14:10:10 [notice] 1#1: start worker process 35
kind-3 | 2022/11/30 14:10:10 [notice] 1#1: start worker process 36
kind-2 | 2022/11/30 14:10:04 [notice] 1#1: nginx/1.23.2
kind-2 | 2022/11/30 14:10:04 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
kind-2 | 2022/11/30 14:10:04 [notice] 1#1: OS: Linux 5.10.124-linuxkit
kind-2 | 2022/11/30 14:10:04 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
kind-2 | 2022/11/30 14:10:04 [notice] 1#1: start worker processes
kind-2 | 2022/11/30 14:10:04 [notice] 1#1: start worker process 32
kind-2 | 2022/11/30 14:10:04 [notice] 1#1: start worker process 33
kind-2 | 2022/11/30 14:10:04 [notice] 1#1: start worker process 34
kind-2 | 2022/11/30 14:10:04 [notice] 1#1: start worker process 35
kind-2 | 2022/11/30 14:10:04 [notice] 1#1: start worker process 36
kind-1 | 2022/11/30 14:09:46 [notice] 1#1: nginx/1.23.2
kind-1 | 2022/11/30 14:09:46 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
kind-1 | 2022/11/30 14:09:46 [notice] 1#1: OS: Linux 5.10.124-linuxkit
kind-1 | 2022/11/30 14:09:46 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
kind-1 | 2022/11/30 14:09:46 [notice] 1#1: start worker processes
kind-1 | 2022/11/30 14:09:46 [notice] 1#1: start worker process 32
kind-1 | 2022/11/30 14:09:46 [notice] 1#1: start worker process 33
kind-1 | 2022/11/30 14:09:46 [notice] 1#1: start worker process 34
kind-1 | 2022/11/30 14:09:46 [notice] 1#1: start worker process 35
kind-1 | 2022/11/30 14:09:46 [notice] 1#1: start worker process 36
kind-3 | 2022/11/30 14:19:13 [notice] 1#1: signal 15 (SIGTERM) received from 49, exiting

ここで Pod を kill してみた

kind-3 | 2022/11/30 14:19:13 [notice] 35#35: exiting
kind-3 | 2022/11/30 14:19:13 [notice] 36#36: exiting
kind-3 | 2022/11/30 14:19:13 [notice] 35#35: exit
kind-3 | 2022/11/30 14:19:13 [notice] 36#36: exit
kind-3 | 2022/11/30 14:19:13 [notice] 34#34: exiting
kind-3 | 2022/11/30 14:19:13 [notice] 33#33: exiting
kind-3 | 2022/11/30 14:19:13 [notice] 34#34: exit
kind-3 | 2022/11/30 14:19:13 [notice] 33#33: exit
kind-3 | 2022/11/30 14:19:13 [notice] 32#32: exiting
kind-3 | 2022/11/30 14:19:13 [notice] 32#32: exit
kind-3 | 2022/11/30 14:19:13 [notice] 1#1: signal 17 (SIGCHLD) received from 35
kind-3 | 2022/11/30 14:19:13 [notice] 1#1: worker process 35 exited with code 0
kind-3 | 2022/11/30 14:19:13 [notice] 1#1: signal 29 (SIGIO) received
kind-3 | 2022/11/30 14:19:13 [notice] 1#1: signal 17 (SIGCHLD) received from 33
kind-3 | 2022/11/30 14:19:13 [notice] 1#1: worker process 33 exited with code 0
kind-3 | 2022/11/30 14:19:13 [notice] 1#1: worker process 34 exited with code 0
kind-3 | 2022/11/30 14:19:13 [notice] 1#1: worker process 36 exited with code 0
kind-3 | 2022/11/30 14:19:13 [notice] 1#1: signal 29 (SIGIO) received
kind-3 | 2022/11/30 14:19:13 [notice] 1#1: signal 17 (SIGCHLD) received from 36
kind-3 | 2022/11/30 14:19:13 [notice] 1#1: signal 17 (SIGCHLD) received from 32
kind-3 | 2022/11/30 14:19:13 [notice] 1#1: worker process 32 exited with code 0
kind-3 | 2022/11/30 14:19:13 [notice] 1#1: exit

利用シーン

  • 複数の Kubernetes クラスタを運用しており、情報をまとめて確認したい場合

登場背景

気にすること

  • Write系の処理は非常に怖いので、-q はまずつけないこと
  • 公式より

This tool is not intended for deploying workloads to clusters, or using programmatically. Therefore, it does not provide a structured output format or ordered printing that is meant to be parsed by or piped to other programs (maybe except for grep).

このツールは、クラスタへのワークロードの展開や、プログラムによる使用を意図したものではありません。したがって、他のプログラムによって解析されたり、パイプでつながれたりすることを意図した、構造化された出力形式や順序付き印刷を提供しない(多分、grepは除く)。 by DeepL