スポンサーリンク

kubectl getの表示を抜粋する

kubernetes
この記事は約14分で読めます。

はじめに

custom-columnsを使えば良いのだけどcustom-columnsという言葉が出てこず途方に暮れることが増えたのでメモ。

 

準備

以下kindで作ったkubernetesで試しています

kind version
kind v0.11.1 go1.16.4 linux/amd64

 

元の表示

デフォルトだとこんな感じ。

$ kubectl get pod -A 
NAMESPACE            NAME                                         READY   STATUS    RESTARTS   AGE
kube-system          coredns-558bd4d5db-2prbc                     1/1     Running   0          48m
kube-system          coredns-558bd4d5db-z7mws                     1/1     Running   0          48m
kube-system          etcd-kind-control-plane                      1/1     Running   0          48m
kube-system          kindnet-k4qsc                                1/1     Running   0          48m
kube-system          kindnet-ql56r                                1/1     Running   0          48m
kube-system          kube-apiserver-kind-control-plane            1/1     Running   0          48m
kube-system          kube-controller-manager-kind-control-plane   1/1     Running   0          48m
kube-system          kube-proxy-tmgqg                             1/1     Running   0          48m
kube-system          kube-proxy-wbhmc                             1/1     Running   0          48m
kube-system          kube-scheduler-kind-control-plane            1/1     Running   0          48m
local-path-storage   local-path-provisioner-547f784dff-h6lw9      1/1     Running   0          48m

$ kubectl get pod -A -o wide
NAMESPACE            NAME                                         READY   STATUS    RESTARTS   AGE   IP           NODE                 NOMINATED NODE   READINESS GATES
kube-system          coredns-558bd4d5db-2prbc                     1/1     Running   0          48m   10.244.0.3   kind-control-plane   <none>           <none>
kube-system          coredns-558bd4d5db-z7mws                     1/1     Running   0          48m   10.244.0.4   kind-control-plane   <none>           <none>
kube-system          etcd-kind-control-plane                      1/1     Running   0          49m   172.17.0.2   kind-control-plane   <none>           <none>
kube-system          kindnet-k4qsc                                1/1     Running   0          48m   172.17.0.3   kind-worker          <none>           <none>
kube-system          kindnet-ql56r                                1/1     Running   0          48m   172.17.0.2   kind-control-plane   <none>           <none>
kube-system          kube-apiserver-kind-control-plane            1/1     Running   0          49m   172.17.0.2   kind-control-plane   <none>           <none>
kube-system          kube-controller-manager-kind-control-plane   1/1     Running   0          49m   172.17.0.2   kind-control-plane   <none>           <none>
kube-system          kube-proxy-tmgqg                             1/1     Running   0          48m   172.17.0.3   kind-worker          <none>           <none>
kube-system          kube-proxy-wbhmc                             1/1     Running   0          48m   172.17.0.2   kind-control-plane   <none>           <none>
kube-system          kube-scheduler-kind-control-plane            1/1     Running   0          49m   172.17.0.2   kind-control-plane   <none>           <none>
local-path-storage   local-path-provisioner-547f784dff-h6lw9      1/1     Running   0          48m   10.244.0.2   kind-control-plane   <none>           <none>

$ kubectl get node
NAME                 STATUS   ROLES                  AGE   VERSION
kind-control-plane   Ready    control-plane,master   49m   v1.21.1
kind-worker          Ready    <none>                 49m   v1.21.1

$ kubectl get node -o wide
NAME                 STATUS   ROLES                  AGE   VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE       KERNEL-VERSION                CONTAINER-RUNTIME
kind-control-plane   Ready    control-plane,master   49m   v1.21.1   172.17.0.2    <none>        Ubuntu 21.04   3.10.0-1160.36.2.el7.x86_64   containerd://1.5.2
kind-worker          Ready    <none>                 49m   v1.21.1   172.17.0.3    <none>        Ubuntu 21.04   3.10.0-1160.36.2.el7.x86_64   containerd://1.5.2

 

kubectl getの一部だけ抜粋する

でもこんなに長い表示が不要なときがあるのでこんな感じで抜き出す

名前だけ抜粋

kubectl get pod -A -o=custom-columns='NAME:.metadata.name'
$ kubectl get pod -A -o=custom-columns='NAME:.metadata.name'
NAME
coredns-558bd4d5db-2prbc
coredns-558bd4d5db-z7mws
etcd-kind-control-plane
kindnet-k4qsc
kindnet-ql56r
kube-apiserver-kind-control-plane
kube-controller-manager-kind-control-plane
kube-proxy-tmgqg
kube-proxy-wbhmc
kube-scheduler-kind-control-plane
local-path-provisioner-547f784dff-h6lw9

 

NODE一覧

kubectl get pod -A -o=custom-columns='NAME:.metadata.name,NODE:.spec.nodeName'
$ kubectl get pod -A -o=custom-columns='NAME:.metadata.name,NODE:.spec.nodeName'
NAME                                         NODE
coredns-558bd4d5db-2prbc                     kind-control-plane
coredns-558bd4d5db-z7mws                     kind-control-plane
etcd-kind-control-plane                      kind-control-plane
kindnet-k4qsc                                kind-worker
kindnet-ql56r                                kind-control-plane
kube-apiserver-kind-control-plane            kind-control-plane
kube-controller-manager-kind-control-plane   kind-control-plane
kube-proxy-tmgqg                             kind-worker
kube-proxy-wbhmc                             kind-control-plane
kube-scheduler-kind-control-plane            kind-control-plane
local-path-provisioner-547f784dff-h6lw9      kind-control-plane

 

STATUS抜粋

kubectl get pod -A -o=custom-columns='NAME:.metadata.name,STATUS:.status.phase'
$ kubectl get pod -A -o=custom-columns='NAME:.metadata.name,STATUS:.status.phase'
NAME                                         STATUS
coredns-558bd4d5db-2prbc                     Running
coredns-558bd4d5db-z7mws                     Running
etcd-kind-control-plane                      Running
kindnet-k4qsc                                Running
kindnet-ql56r                                Running
kube-apiserver-kind-control-plane            Running
kube-controller-manager-kind-control-plane   Running
kube-proxy-tmgqg                             Running
kube-proxy-wbhmc                             Running
kube-scheduler-kind-control-plane            Running
local-path-provisioner-547f784dff-h6lw9      Running

 

cpu,memoryのrequests

kubectl get pod -A -o=custom-columns='NAME:.metadata.name,requests-cpu:.spec.containers[0].resources.requests.cpu,requests-memory:.spec.containers[0].resources.requests.memory'
$ kubectl get pod -A -o=custom-columns='NAME:.metadata.name,requests-cpu:.spec.containers[0].resources.requests.cpu,requests-memory:.spec.containers[0].resources.requests.memory'
NAME                                         requests-cpu   requests-memory
coredns-558bd4d5db-2prbc                     100m           70Mi
coredns-558bd4d5db-z7mws                     100m           70Mi
etcd-kind-control-plane                      100m           100Mi
kindnet-k4qsc                                100m           50Mi
kindnet-ql56r                                100m           50Mi
kube-apiserver-kind-control-plane            250m           <none>
kube-controller-manager-kind-control-plane   200m           <none>
kube-proxy-tmgqg                             <none>         <none>
kube-proxy-wbhmc                             <none>         <none>
kube-scheduler-kind-control-plane            100m           <none>
local-path-provisioner-547f784dff-h6lw9      <none>         <none>

 

nodeも見れる

kubectl get node -o=custom-columns='NAME:.metadata.name,INTERNAL-IP:.status.addresses[0].address'
$ kubectl get node -o=custom-columns='NAME:.metadata.name,INTERNAL-IP:.status.addresses[0].address'
NAME INTERNAL-IP
kind-control-plane 172.17.0.2
kind-worker 172.17.0.3

 

custom-columnsに設定する値

-o yamlや-o jsonで表示されているものを読み解く。

個人的には-o yamlの方が読み取りやすい

 

参考

kubectl Cheat Sheet
This page contains a list of commonly used kubectl commands and flags. Kubectl autocomplete BASH source > ~/.bashrc # add autocomplete permanently to your bash ...

 

スポンサーリンク
kubernetes
スポンサーリンク
hakenをフォローする
定年まで泣くんじゃない
タイトルとURLをコピーしました