kubernetesのyaml設定の特定パラメータを確認する。


はじめ

テストするのに便利そうだったから、メモ書き程度のに残します。

テスト

pod情報から以下の情報を得た時の情報から特定の設定情報だけを取り出す方法

$ kubectl get -n gf-space pod
NAME                         READY   STATUS    RESTARTS   AGE
cadvisor-4jngn               1/1     Running   0          16d
cadvisor-5wpgj               1/1     Running   1          16d
cadvisor-lzqt6               1/1     Running   2          16d
dockeri2cmoni-i2c-r5vnq      1/1     Running   0          16d
dockeri2cmoni-lbs6j          1/1     Running   1          16d
dockeri2cmoni-z54rr          1/1     Running   2          16d
exporter-4gn86               1/1     Running   5          36d
exporter-rtzhk               1/1     Running   12         54d
exporter-tfzj8               1/1     Running   8          54d
grafana-6f9df59584-mcfp7     1/1     Running   0          22d
loki-654fbc4c68-2pbb7        1/1     Running   0          12d
prometheus-cc69fd6b9-zm5p4   1/1     Running   112        22d
promtail-cg87t               1/1     Running   0          12d
promtail-fs67n               1/1     Running   0          12d
promtail-mqnkc               1/1     Running   0          12d

実行結果、grafanaの情報をdescribeで調べます。

$ kubectl describe -n gf-space pod/grafana-6f9df59584-mcfp7
Name:         grafana-6f9df59584-mcfp7
Namespace:    gf-space
Priority:     0
Node:         raspberrypi5/192.168.0.25
Start Time:   Tue, 13 Oct 2020 15:24:46 +0900
Labels:       app=grafana
              pod-template-hash=6f9df59584
Annotations:  <none>
Status:       Running
IP:           10.244.2.141
IPs:
  IP:           10.244.2.141
Controlled By:  ReplicaSet/grafana-6f9df59584
Containers:
  grafana:
    Container ID:   docker://662136dbe45bf671a46f2de4d36c2779c049ca2969461697fde8eb12cebf076c
    Image:          grafana/grafana
    Image ID:       docker-pullable://grafana/grafana@sha256:a24849c210ebea3ec754594302faa49d5a21460cdc14ab30dd38d8938a865f09
    Port:           3000/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Tue, 13 Oct 2020 15:24:49 +0900
    Ready:          True
    Restart Count:  0
    Environment:
      TZ:  Asia/Tokyo
    Mounts:
      /var/lib/grafana from grafana-vol (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-lb8hf (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  grafana-vol:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  grafana-pv-claim
    ReadOnly:   false
  default-token-lb8hf:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-lb8hf
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:          <none>

Image情報だけ取り出したいときで、スクロールして調べるのがめんどくさいので、

以下のコマンドでjson情報が取り出せる

$ kubectl get -n gf-space pod/grafana-6f9df59584-mcfp7 -o jsonpath={}
{"apiVersion":"v1","kind":"Pod","metadata":{"creationTimestamp":"2020-10-13T06:24:46Z","generateName":"grafana-6f9df59584-","labels":{"app":"grafana","pod-template-hash":"6f9df59584"},"managedFields":[{"apiVersion":"v1","fieldsType":"FieldsV1","fieldsV1":{"f:metadata":{"f:generateName":{},"f:labels":{".":{},"f:app":{},"f:pod-template-hash":{}},"f:ownerReferences":{".":{},"k:{\"uid\":\"3d95d8ce-2ffd-4c70-b844-cb9be15a49d0\"}":{".":{},"f:apiVersion":{},"f:blockOwnerDeletion":{},"f:controller":{},"f:kind":{},"f:name":{},"f:uid":{}}}},"f:spec":{"f:containers":{"k:{\"name\":\"grafana\"}":{".":{},"f:env":{".":{},"k:{\"name\":\"TZ\"}":{".":{},"f:name":{},"f:value":{}}},"f:image":{},"f:imagePullPolicy":{},"f:name":{},"f:ports":{".":{},"k:{\"containerPort\":3000,\"protocol\":\"TCP\"}":{".":{},"f:containerPort":{},"f:protocol":{}}},"f:resources":{},"f:terminationMessagePath":{},"f:terminationMessagePolicy":{},"f:volumeMounts":{".":{},"k:{\"mountPath\":\"/var/lib/grafana\"}":{".":{},"f:mountPath":{},"f:name":{}}}}},"f:dnsPolicy":{},"f:enableServiceLinks":{},"f:restartPolicy":{},"f:schedulerName":{},"f:securityContext":{},"f:terminationGracePeriodSeconds":{},"f:volumes":{".":{},"k:{\"name\":\"grafana-vol\"}":{".":{},"f:name":{},"f:persistentVolumeClaim":{".":{},"f:claimName":{}}}}}},"manager":"kube-controller-manager","operation":"Update","time":"2020-10-26T23:43:09Z"},{"apiVersion":"v1","fieldsType":"FieldsV1","fieldsV1":{"f:status":{"f:conditions":{"k:{\"type\":\"ContainersReady\"}":{".":{},"f:lastProbeTime":{},"f:lastTransitionTime":{},"f:status":{},"f:type":{}},"k:{\"type\":\"Initialized\"}":{".":{},"f:lastProbeTime":{},"f:lastTransitionTime":{},"f:status":{},"f:type":{}},"k:{\"type\":\"Ready\"}":{".":{},"f:lastProbeTime":{},"f:lastTransitionTime":{},"f:status":{},"f:type":{}}},"f:containerStatuses":{},"f:hostIP":{},"f:phase":{},"f:podIP":{},"f:podIPs":{".":{},"k:{\"ip\":\"10.244.2.141\"}":{".":{},"f:ip":{}}},"f:startTime":{}}},"manager":"kubelet","operation":"Update","time":"2020-10-26T23:43:38Z"}],"name":"grafana-6f9df59584-mcfp7","namespace":"gf-space","ownerReferences":[{"apiVersion":"apps/v1","blockOwnerDeletion":true,"controller":true,"kind":"ReplicaSet","name":"grafana-6f9df59584","uid":"3d95d8ce-2ffd-4c70-b844-cb9be15a49d0"}],"resourceVersion":"10135232","selfLink":"/api/v1/namespaces/gf-space/pods/grafana-6f9df59584-mcfp7","uid":"2988d8f3-ada0-4ea0-bf32-22f0f54149bf"},"spec":{"containers":[{"env":[{"name":"TZ","value":"Asia/Tokyo"}],"image":"grafana/grafana","imagePullPolicy":"IfNotPresent","name":"grafana","ports":[{"containerPort":3000,"protocol":"TCP"}],"resources":{},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File","volumeMounts":[{"mountPath":"/var/lib/grafana","name":"grafana-vol"},{"mountPath":"/var/run/secrets/kubernetes.io/serviceaccount","name":"default-token-lb8hf","readOnly":true}]}],"dnsPolicy":"ClusterFirst","enableServiceLinks":true,"nodeName":"raspberrypi5","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":"grafana-vol","persistentVolumeClaim":{"claimName":"grafana-pv-claim"}},{"name":"default-token-lb8hf","secret":{"defaultMode":420,"secretName":"default-token-lb8hf"}}]},"status":{"conditions":[{"lastProbeTime":null,"lastTransitionTime":"2020-10-13T06:24:46Z","status":"True","type":"Initialized"},{"lastProbeTime":null,"lastTransitionTime":"2020-10-13T06:24:50Z","status":"True","type":"Ready"},{"lastProbeTime":null,"lastTransitionTime":"2020-10-13T06:24:50Z","status":"True","type":"ContainersReady"},{"lastProbeTime":null,"lastTransitionTime":"2020-10-13T06:24:46Z","status":"True","type":"PodScheduled"}],"containerStatuses":[{"containerID":"docker://662136dbe45bf671a46f2de4d36c2779c049ca2969461697fde8eb12cebf076c","image":"grafana/grafana:latest","imageID":"docker-pullable://grafana/grafana@sha256:a24849c210ebea3ec754594302faa49d5a21460cdc14ab30dd38d8938a865f09","lastState":{},"name":"grafana","ready":true,"restartCount":0,"started":true,"state":{"running":{"startedAt":"2020-10-13T06:24:49Z"}}}],"hostIP":"192.168.0.25","phase":"Running","podIP":"10.244.2.141","podIPs":[{"ip":"10.244.2.141"}],"qosClass":"BestEffort","startTime":"2020-10-13T06:24:46Z"}}

欲しいのは、spec内のcontainers配列1番目のimageの情報のため以下のコマンドで取り出せる。

$ kubectl get -n gf-space pod/grafana-6f9df59584-mcfp7 -o jsonpath={.spec.containers[0].image}
grafana/grafana