「kubectl-neat(kubectl neat
コマンド)」を使うと Kubernetes のマニフェストから「冗長な」情報を削除して表示できる.知っておくと便利!GitHub リポジトリの README.md
を読むと「メタデータ」や「デフォルト設定」や「Admission Controllers によって作られた情報」によって読みにくくなると書いてある.さっそく「kubectl-neat」を紹介していく.
検証環境
- Docker Desktop for Mac
- Kubernetes 1.19.3
事前確認 : kubectl get pods xxx -o yaml
コマンド
「kubectl-neat」を試す前に事前確認として kubectl get pods xxx -o yaml
コマンドを確認する.kubectl get pods
コマンドに -o yaml
オプションを追加すると,Pod の状態を YAML 形式で詳細に確認できる.まず,以下のマニフェストを作る.
apiVersion: v1 kind: Pod metadata: name: sandbox-kubectl-neat-pod labels: app: sandbox-kubectl-neat spec: containers: - name: nginx image: nginx:1.19-alpine ports: - containerPort: 80
マニフェストを適用して kubectl get pods xxx -o yaml
コマンドを実行すると,以下のように表示される.metadata.*
も spec.*
も status.*
も非常に多くの情報が含まれている.全部載せると長すぎるため今回は割愛して載せる.ザッとスクロールをしてもらって OK!
$ kubectl get pods sandbox-kubectl-neat-pod -o yaml apiVersion: v1 kind: Pod metadata: annotations: (中略) creationTimestamp: "2021-01-01T13:00:00Z" labels: app: sandbox-kubectl-neat managedFields: (中略) name: sandbox-kubectl-neat-pod namespace: default (中略) spec: containers: - image: nginx:1.19-alpine imagePullPolicy: IfNotPresent name: nginx ports: - containerPort: 80 protocol: TCP resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - mountPath: /var/run/secrets/kubernetes.io/serviceaccount name: default-token-klk8p readOnly: true dnsPolicy: ClusterFirst enableServiceLinks: true nodeName: docker-desktop 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: default-token-klk8p secret: defaultMode: 420 secretName: default-token-klk8p status: (中略)
「kubectl-neat」をインストールする
「kubectl-neat」は kubectl
プラグインとして提供されているため,Krew を使えば簡単にインストールできる.kubectl krew install neat
コマンドでインストールすれば,kubectl neat help
コマンドが実行できるようになる.
$ kubectl krew install neat Installing plugin: neat Installed plugin: neat \ | Use this plugin: | kubectl neat | Documentation: | https://github.com/itaysk/kubectl-neat / $ kubectl neat help
なお,最初に Krew をインストールしておく必要があるため,はじめて使う場合は以下のドキュメントを参照する.
「kubectl-neat」を試す
さっきと同じ Pod に対して「kubectl-neat」を試す.実行する方法は大きく2種類あり「kubectl get pods xxx -o yaml | kubectl neat
のようにパイプをする方法」と「kubectl neat get pods xxx -o yaml
のようにラッパーとして使う方法」がある.
実際に「kubectl-neat」を使うと,以下のように表示される.非常にスッキリした!個人的に使うのに便利だし,デモをするときに「意図的に」情報量を落とす場合などに使えそう.実際にデモをする場合は「kubectl-neat」を使う意図を明確に伝えておく必要がありそう.
$ kubectl get pods sandbox-kubectl-neat-pod -o yaml | kubectl neat apiVersion: v1 kind: Pod metadata: labels: app: sandbox-kubectl-neat name: sandbox-kubectl-neat-pod namespace: default spec: containers: - image: nginx:1.19-alpine name: nginx ports: - containerPort: 80 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
マニフェストに ServiceAccount を指定する
GitHub リポジトリの README.md
を読むと「kubectl-neat」によって削除される条件(Admission Controllers 対応表)が載っている.例えば ServiceAccount だと,以下のように default-token-*
に該当するボリュームは削除されると書いてある.
Remove
default-token-*
volumes. Remove deprecatedspec.serviceAccount
さっきと同じ Pod のマニフェストに serviceAccountName
を指定して,適用しておく.
apiVersion: v1 kind: Pod metadata: name: sandbox-kubectl-neat-pod labels: app: sandbox-kubectl-neat spec: serviceAccountName: sandbox-kubectl-neat-service-account containers: - name: nginx image: nginx:1.19-alpine ports: - containerPort: 80
もう1度 kubectl get pods sandbox-kubectl-neat-pod -o yaml | kubectl neat
を実行して事前確認との差分を以下に載せる.今度はボリューム関連のマニフェストが追加されていた.これは「kubectl-neat」の仕様通り,default-token-*
に該当せずに表示されている.
@@ -12,9 +12,13 @@ name: nginx ports: - containerPort: 80 + volumeMounts: + - mountPath: /var/run/secrets/kubernetes.io/serviceaccount + name: sandbox-kubectl-neat-service-account-token-n59lc + readOnly: true preemptionPolicy: PreemptLowerPriority priority: 0 - serviceAccountName: default + serviceAccountName: sandbox-kubectl-neat-service-account tolerations: - effect: NoExecute key: node.kubernetes.io/not-ready @@ -24,3 +28,7 @@ key: node.kubernetes.io/unreachable operator: Exists tolerationSeconds: 300 + volumes: + - name: sandbox-kubectl-neat-service-account-token-n59lc + secret: + secretName: sandbox-kubectl-neat-service-account-token-n59lc
まとめ
「kubectl-neat(kubectl neat
コマンド)」を使うと Kubernetes のマニフェストから「冗長な」情報を削除して表示できる.知っておくと便利!デモをするときに「意図的に」情報量を落とす場合などに使えそう.